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
1 changed files with 7 additions and 0 deletions

View File

@ -15,6 +15,8 @@
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <cerrno>
#include <cstring>
#include "config.h"
#include "PlayerAttributes.h"
#include "TileGenerator.h"
@ -630,6 +632,11 @@ void TileGenerator::writeImage(const std::string &output)
{
FILE *out;
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);
fclose(out);
gdImageDestroy(m_image);