comparison glxdragon.cpp @ 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 294c4c7943b5
comparison
equal deleted inserted replaced
17:0fa9302e324d 18:b1e75c65016d
40 #include <cstdio> 40 #include <cstdio>
41 41
42 42
43 /* Default settings etc. constants 43 /* Default settings etc. constants
44 */ 44 */
45 #define SET_DEF_WIDTH 1280 45 #define SET_DEF_WIDTH 1280
46 #define SET_DEF_HEIGHT 960 46 #define SET_DEF_HEIGHT 960
47 #define SET_FRAMES (180) 47 #define SET_FRAMES (180)
48 #define SET_MAX_SHADER_SIZE (128 * 1024)
48 49
49 50
50 /* Structures 51 /* Structures
51 */ 52 */
52 struct Mesh 53 struct Mesh
270 271
271 glEnable(GL_DEPTH_TEST); 272 glEnable(GL_DEPTH_TEST);
272 } 273 }
273 274
274 275
275 bool dmReadText(const std::string &filename, std::string &tstr) 276 bool dmReadText(const std::string &filename, std::string &buf, const int maxSize)
276 { 277 {
277 std::ifstream in(filename.c_str()); 278 std::ifstream in(filename.c_str());
278 279
279 if (!in.is_open()) 280 if (!in.is_open())
280 { 281 {
282 filename.c_str()); 283 filename.c_str());
283 return false; 284 return false;
284 } 285 }
285 286
286 in.seekg(0, std::ios::end); 287 in.seekg(0, std::ios::end);
287 tstr.reserve(in.tellg()); 288
289 if (in.tellg() > maxSize)
290 {
291 printf("ERROR: File '%s' is too large.\n",
292 filename.c_str());
293 return false;
294 }
295
296 buf.reserve(in.tellg());
288 in.seekg(0, std::ios::beg); 297 in.seekg(0, std::ios::beg);
289 298
290 tstr.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); 299 buf.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
291 300
292 return true; 301 return true;
293 } 302 }
294 303
295 304
509 goto exit; 518 goto exit;
510 519
511 if (optUseShaders) 520 if (optUseShaders)
512 { 521 {
513 // Read shader files 522 // Read shader files
514 if (!dmReadText(optModelPrefix + ".frag", modelFragStr) || 523 if (!dmReadText(optModelPrefix + ".frag", modelFragStr, SET_MAX_SHADER_SIZE) ||
515 !dmReadText(optModelPrefix + ".vert", modelVertStr)) 524 !dmReadText(optModelPrefix + ".vert", modelVertStr, SET_MAX_SHADER_SIZE))
516 goto exit; 525 goto exit;
517 } 526 }
518 527
519 // Initialize SDL + OpenGL 528 // Initialize SDL + OpenGL
520 if (!dmInitSDLGL(optWidth, optHeight, "GLXDragon2")) 529 if (!dmInitSDLGL(optWidth, optHeight, "GLXDragon2"))