changeset 18:b1e75c65016d

Check for "too large" shader files.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 05 Nov 2019 11:46:31 +0200
parents 0fa9302e324d
children a329f0216491
files glxdragon.cpp
diffstat 1 files changed, 17 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/glxdragon.cpp	Tue Nov 05 10:48:22 2019 +0200
+++ b/glxdragon.cpp	Tue Nov 05 11:46:31 2019 +0200
@@ -42,9 +42,10 @@
 
 /* Default settings etc. constants
  */
-#define SET_DEF_WIDTH      1280
-#define SET_DEF_HEIGHT     960
-#define SET_FRAMES         (180)
+#define SET_DEF_WIDTH        1280
+#define SET_DEF_HEIGHT       960
+#define SET_FRAMES           (180)
+#define SET_MAX_SHADER_SIZE  (128 * 1024)
 
 
 /* Structures
@@ -272,7 +273,7 @@
 }
 
 
-bool dmReadText(const std::string &filename, std::string &tstr)
+bool dmReadText(const std::string &filename, std::string &buf, const int maxSize)
 {
     std::ifstream in(filename.c_str());
 
@@ -284,10 +285,18 @@
     }
 
     in.seekg(0, std::ios::end);
-    tstr.reserve(in.tellg());
+
+    if (in.tellg() > maxSize)
+    {
+        printf("ERROR: File '%s' is too large.\n",
+            filename.c_str());
+        return false;
+    }
+
+    buf.reserve(in.tellg());
     in.seekg(0, std::ios::beg);
 
-    tstr.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
+    buf.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
 
     return true;
 }
@@ -511,8 +520,8 @@
     if (optUseShaders)
     {
         // Read shader files
-        if (!dmReadText(optModelPrefix + ".frag", modelFragStr) ||
-            !dmReadText(optModelPrefix + ".vert", modelVertStr))
+        if (!dmReadText(optModelPrefix + ".frag", modelFragStr, SET_MAX_SHADER_SIZE) ||
+            !dmReadText(optModelPrefix + ".vert", modelVertStr, SET_MAX_SHADER_SIZE))
             goto exit;
     }