summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2019-03-06 17:28:57 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2019-03-06 17:28:57 +0100
commit5eb47bed81de1326843e1e3bad41addcfed08d4e (patch)
tree215bdc06a67ca880c1b286a098ba6f6e86b1b7a5
parentf536d8479e37b1515ffb5737a9f276976bef3cf0 (diff)
Fix crash if opening Resource with a directory instead of a file.
-rw-r--r--plugingui/resource.cc16
-rw-r--r--test/resource_test.cc23
2 files changed, 34 insertions, 5 deletions
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);
diff --git a/test/resource_test.cc b/test/resource_test.cc
index 2e88c45..28a244e 100644
--- a/test/resource_test.cc
+++ b/test/resource_test.cc
@@ -30,10 +30,12 @@
#include "drumkit_creator.h"
-class ResourceTester : public GUI::Resource {
+class ResourceTester
+ : public GUI::Resource
+{
public:
ResourceTester(const std::string& name)
- : Resource(name)
+ : GUI::Resource(name)
{}
bool probeIsInternal()
@@ -42,13 +44,15 @@ public:
}
};
-class ResourceTest : public DGUnit
+class ResourceTest
+ : public DGUnit
{
public:
ResourceTest()
{
DGUNIT_TEST(ResourceTest::externalReadTest);
DGUNIT_TEST(ResourceTest::internalReadTest);
+ DGUNIT_TEST(ResourceTest::failTest);
}
DrumkitCreator drumkit_creator;
@@ -70,6 +74,19 @@ public:
DGUNIT_ASSERT(rc.valid());
DGUNIT_ASSERT_EQUAL((size_t)1123, rc.size());
}
+
+ void failTest()
+ {
+ {
+ ResourceTester rc("/tmp/");
+ DGUNIT_ASSERT(!rc.valid());
+ }
+
+ {
+ ResourceTester rc("no_such_file");
+ DGUNIT_ASSERT(!rc.valid());
+ }
+ }
};
// Registers the fixture into the 'registry'