From d2762564dce131339df576721c0156b35e78ea58 Mon Sep 17 00:00:00 2001 From: John Kessenich Date: Mon, 20 Jul 2015 12:29:41 -0600 Subject: [PATCH] Preprocessor: Prevent (and give an error on) expression division by 0. --- Test/baseResults/cppComplexExpr.vert.out | 6 +++++- Test/cppComplexExpr.vert | 7 +++++++ glslang/MachineIndependent/preprocessor/Pp.cpp | 7 +++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Test/baseResults/cppComplexExpr.vert.out b/Test/baseResults/cppComplexExpr.vert.out index e8e3eb4f..3d881d3b 100644 --- a/Test/baseResults/cppComplexExpr.vert.out +++ b/Test/baseResults/cppComplexExpr.vert.out @@ -42,8 +42,12 @@ ERROR: 0:3006: '#undef' : predefined names can't be (un)defined: __LINE__ ERROR: 0:3007: '#undef' : predefined names can't be (un)defined: __FILE__ ERROR: 0:3008: '#undef' : predefined names can't be (un)defined: __VERSION__ ERROR: 0:3009: '#undef' : names beginning with "GL_" can't be (un)defined: GL_SOME_EXTENSION +ERROR: 0:4000: 'preprocessor evaluation' : division by 0 +ERROR: 0:0: 'preprocessor evaluation' : division by 0 +ERROR: 0:3: 'preprocessor evaluation' : bad expression +ERROR: 0:3: 'preprocessor evaluation' : division by 0 ERROR: 0:10001: '' : missing #endif -ERROR: 44 compilation errors. No code generated. +ERROR: 48 compilation errors. No code generated. Shader version: 300 diff --git a/Test/cppComplexExpr.vert b/Test/cppComplexExpr.vert index 3f15b5ef..bce5ffa3 100644 --- a/Test/cppComplexExpr.vert +++ b/Test/cppComplexExpr.vert @@ -171,6 +171,13 @@ float c = foobar(1.1, 2.2 #undef __VERSION__ #undef GL_SOME_EXTENSION +#line 4000 +#line 200 % 0 // ERROR, div by 0 +#if __LINE__ / 0 // ERROR, div by 0 +#endif + +#if 7% // ERROR incomplete expression + #line 10000 #if 0 // ERROR, EOF \ No newline at end of file diff --git a/glslang/MachineIndependent/preprocessor/Pp.cpp b/glslang/MachineIndependent/preprocessor/Pp.cpp index e246a6cf..8c3dfeb8 100644 --- a/glslang/MachineIndependent/preprocessor/Pp.cpp +++ b/glslang/MachineIndependent/preprocessor/Pp.cpp @@ -505,6 +505,13 @@ int TPpContext::eval(int token, int precedence, bool shortCircuit, int& res, boo token = scanToken(ppToken); token = eval(token, binop[op].precedence, shortCircuit, res, err, ppToken); + + if (binop[op].op == op_div || binop[op].op == op_mod) { + if (res == 0) { + parseContext.ppError(loc, "division by 0", "preprocessor evaluation", ""); + res = 1; + } + } res = binop[op].op(leftSide, res); }