From faa720f14c4657de0e54f20ff46a9170268d3c09 Mon Sep 17 00:00:00 2001
From: John Kessenich <cepheus@frii.com>
Date: Mon, 2 Jan 2017 17:56:08 -0700
Subject: [PATCH] PP: Fix issue #426, recover from bad-source macro expansion.

---
 Test/baseResults/cppBad2.vert.out             | 19 +++++++++++++++++++
 Test/cppBad2.vert                             |  3 +++
 glslang/Include/revision.h                    |  2 +-
 .../MachineIndependent/preprocessor/Pp.cpp    | 14 +++++++++++---
 gtests/AST.FromFile.cpp                       |  1 +
 5 files changed, 35 insertions(+), 4 deletions(-)
 create mode 100755 Test/baseResults/cppBad2.vert.out
 create mode 100755 Test/cppBad2.vert

diff --git a/Test/baseResults/cppBad2.vert.out b/Test/baseResults/cppBad2.vert.out
new file mode 100755
index 000000000..0398e5e48
--- /dev/null
+++ b/Test/baseResults/cppBad2.vert.out
@@ -0,0 +1,19 @@
+cppBad2.vert
+ERROR: 0:3: 'macro expansion' : End of input in macro b
+ERROR: 0:3: '' : compilation terminated 
+ERROR: 2 compilation errors.  No code generated.
+
+
+Shader version: 100
+ERROR: node is still EOpNull!
+0:?   Linker Objects
+
+
+Linked vertex stage:
+
+ERROR: Linking vertex stage: Missing entry point: Each stage requires one entry point
+
+Shader version: 100
+ERROR: node is still EOpNull!
+0:?   Linker Objects
+
diff --git a/Test/cppBad2.vert b/Test/cppBad2.vert
new file mode 100755
index 000000000..5e61b49ef
--- /dev/null
+++ b/Test/cppBad2.vert
@@ -0,0 +1,3 @@
+#define a b(
+#define b(x)
+b(a)
\ No newline at end of file
diff --git a/glslang/Include/revision.h b/glslang/Include/revision.h
index 4d9682526..cccc79677 100644
--- a/glslang/Include/revision.h
+++ b/glslang/Include/revision.h
@@ -2,5 +2,5 @@
 // For the version, it uses the latest git tag followed by the number of commits.
 // For the date, it uses the current date (when then script is run).
 
-#define GLSLANG_REVISION "Overload400-PrecQual.1727"
+#define GLSLANG_REVISION "Overload400-PrecQual.1728"
 #define GLSLANG_DATE "02-Jan-2017"
diff --git a/glslang/MachineIndependent/preprocessor/Pp.cpp b/glslang/MachineIndependent/preprocessor/Pp.cpp
index 595b7295d..eb2f3dfaf 100644
--- a/glslang/MachineIndependent/preprocessor/Pp.cpp
+++ b/glslang/MachineIndependent/preprocessor/Pp.cpp
@@ -934,12 +934,20 @@ TPpContext::TokenStream* TPpContext::PrescanMacroArg(TokenStream& arg, TPpToken*
     TokenStream* expandedArg = new TokenStream;
     pushInput(new tMarkerInput(this));
     pushTokenStreamInput(arg);
-    while ((token = scanToken(ppToken)) != tMarkerInput::marker) {
+    while ((token = scanToken(ppToken)) != tMarkerInput::marker && token != EndOfInput) {
         if (token == PpAtomIdentifier && MacroExpand(ppToken, false, newLineOkay) != 0)
             continue;
         RecordToken(*expandedArg, token, ppToken);
     }
-    popInput();
+
+    if (token == EndOfInput) {
+        // MacroExpand ate the marker, so had bad input, recover
+        delete expandedArg;
+        expandedArg = nullptr;
+    } else {
+        // remove the marker
+        popInput();
+    }
 
     return expandedArg;
 }
@@ -1133,7 +1141,7 @@ int TPpContext::MacroExpand(TPpToken* ppToken, bool expandUndef, bool newLineOka
             depth = 0;
             while (1) {
                 token = scanToken(ppToken);
-                if (token == EndOfInput) {
+                if (token == EndOfInput || token == tMarkerInput::marker) {
                     parseContext.ppError(loc, "End of input in macro", "macro expansion", atomStrings.getString(macroAtom));
                     delete in;
                     return 0;
diff --git a/gtests/AST.FromFile.cpp b/gtests/AST.FromFile.cpp
index 70705337f..d8a5ef94c 100644
--- a/gtests/AST.FromFile.cpp
+++ b/gtests/AST.FromFile.cpp
@@ -79,6 +79,7 @@ INSTANTIATE_TEST_CASE_P(
         "cppIndent.vert",
         "cppNest.vert",
         "cppBad.vert",
+        "cppBad2.vert",
         "cppComplexExpr.vert",
         "badChars.frag",
         "pointCoord.frag",
-- 
GitLab