From 5eb47bed81de1326843e1e3bad41addcfed08d4e Mon Sep 17 00:00:00 2001
From: Bent Bisballe Nyeng <deva@aasimon.org>
Date: Wed, 6 Mar 2019 17:28:57 +0100
Subject: Fix crash if opening Resource with a directory instead of a file.

---
 plugingui/resource.cc | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

(limited to 'plugingui')

diff --git a/plugingui/resource.cc b/plugingui/resource.cc
index 2435668..432f25d 100644
--- a/plugingui/resource.cc
+++ b/plugingui/resource.cc
@@ -28,11 +28,13 @@
 
 #include <hugin.hpp>
 #include <cstdio>
+#include <climits>
 
 // rcgen generated file containing rc_data declaration.
 #include "resource_data.h"
 
-namespace GUI {
+namespace GUI
+{
 
 // Internal resources start with a colon.
 static bool nameIsInternal(const std::string& name)
@@ -85,7 +87,17 @@ Resource::Resource(const std::string& name)
 			std::fclose(fp);
 			return;
 		}
-		size_t filesize = ftell(fp);
+
+		long filesize = std::ftell(fp);
+
+		// Apparently fseek doesn't fail if fp points to a directory that has been
+		// opened (which doesn't fail either!!) and ftell will then fail by either
+		// returning -1 or LONG_MAX
+		if(filesize == -1L || filesize == LONG_MAX)
+		{
+			std::fclose(fp);
+			return;
+		}
 
 		// Reserve space in the string for the data.
 		externalData.reserve(filesize);
-- 
cgit v1.2.3