From d92ef319f10db7485443733487f1ad340bfc913f Mon Sep 17 00:00:00 2001 From: Rogier Date: Sun, 16 Feb 2014 22:13:31 +0100 Subject: [PATCH] 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. --- TileGenerator.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/TileGenerator.cpp b/TileGenerator.cpp index c726385..7167a34 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include #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);