# HG changeset patch # User Matti Hamalainen # Date 1572947191 -7200 # Node ID b1e75c65016d52012dcc4744ba687ebe4e8f941d # Parent 0fa9302e324d3d1bc13f70c84553593cd5fb0b2b Check for "too large" shader files. diff -r 0fa9302e324d -r b1e75c65016d glxdragon.cpp --- 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(in)), std::istreambuf_iterator()); + buf.assign((std::istreambuf_iterator(in)), std::istreambuf_iterator()); 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; }