Fix array out of bounds bug in processing if-else-endif macros.
authorLei Zhang <antiagainst@google.com>
Wed, 9 Mar 2016 19:53:57 +0000 (14:53 -0500)
committerLei Zhang <antiagainst@google.com>
Wed, 9 Mar 2016 20:11:56 +0000 (15:11 -0500)
If we are not inside an if macro, we cannot simply decrease
elsetracker.

Fixes https://github.com/KhronosGroup/glslang/issues/29.

Test/baseResults/preprocessor.many.endif.vert.err [new file with mode: 0644]
Test/baseResults/preprocessor.many.endif.vert.out [new file with mode: 0644]
Test/preprocessor.many.endif.vert [new file with mode: 0644]
Test/test-preprocessor-list
glslang/MachineIndependent/preprocessor/Pp.cpp

diff --git a/Test/baseResults/preprocessor.many.endif.vert.err b/Test/baseResults/preprocessor.many.endif.vert.err
new file mode 100644 (file)
index 0000000..49aafc5
--- /dev/null
@@ -0,0 +1,12 @@
+ERROR: 0:1: '#endif' : mismatched statements 
+ERROR: 0:2: '#endif' : mismatched statements 
+ERROR: 0:3: '#endif' : mismatched statements 
+ERROR: 0:4: '#endif' : mismatched statements 
+ERROR: 0:5: '#endif' : mismatched statements 
+ERROR: 0:6: '#endif' : mismatched statements 
+ERROR: 0:7: '#endif' : mismatched statements 
+ERROR: 0:10: 'preprocessor evaluation' : bad expression 
+ERROR: 0:11: '' : missing #endif 
+ERROR: 9 compilation errors.  No code generated.
+
+
diff --git a/Test/baseResults/preprocessor.many.endif.vert.out b/Test/baseResults/preprocessor.many.endif.vert.out
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/Test/preprocessor.many.endif.vert b/Test/preprocessor.many.endif.vert
new file mode 100644 (file)
index 0000000..7b04962
--- /dev/null
@@ -0,0 +1,10 @@
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+
+#if
+#else
index ac8f8be..80d4b22 100644 (file)
@@ -12,3 +12,4 @@ preprocessor.pragma.vert
 preprocessor.simple.vert
 preprocessor.success_if_parse_would_fail.vert
 preprocessor.defined.vert
+preprocessor.many.endif.vert
index 9a33a77..da70942 100644 (file)
@@ -867,12 +867,13 @@ int TPpContext::readCPPline(TPpToken* ppToken)
             token = CPPelse(0, ppToken);
             break;
         case PpAtomEndif:
-            elseSeen[elsetracker] = false;
-            --elsetracker;
             if (! ifdepth)
                 parseContext.ppError(ppToken->loc, "mismatched statements", "#endif", "");
-            else
+            else {
+                elseSeen[elsetracker] = false;
+                --elsetracker;
                 --ifdepth;
+            }
             token = extraTokenCheck(PpAtomEndif, ppToken, scanToken(ppToken));
             break;
         case PpAtomIf: