Fix SEGV after failing to open output file: throw runtime error

The result of opening the file was not checked, resulting in a
NULL pointer dereference if it failed.
This commit is contained in:
Rogier 2014-02-16 22:13:31 +01:00 committed by Sfan5
parent a15bc30071
commit d92ef319f1

@ -15,6 +15,8 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <cerrno>
#include <cstring>
#include "config.h" #include "config.h"
#include "PlayerAttributes.h" #include "PlayerAttributes.h"
#include "TileGenerator.h" #include "TileGenerator.h"
@ -630,6 +632,11 @@ void TileGenerator::writeImage(const std::string &output)
{ {
FILE *out; FILE *out;
out = fopen(output.c_str(), "wb"); out = fopen(output.c_str(), "wb");
if (!out) {
std::ostringstream oss;
oss << "Error opening '" << output.c_str() << "': " << std::strerror(errno);
throw std::runtime_error(oss.str());
}
gdImagePng(m_image, out); gdImagePng(m_image, out);
fclose(out); fclose(out);
gdImageDestroy(m_image); gdImageDestroy(m_image);