summaryrefslogtreecommitdiffstats
path: root/Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c
diff options
context:
space:
mode:
Diffstat (limited to 'Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c')
-rw-r--r--Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c109
1 files changed, 84 insertions, 25 deletions
diff --git a/Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c b/Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c
index 204a213..13a5df1 100644
--- a/Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c
+++ b/Source/ThirdParty/ANGLE/src/compiler/preprocessor/cpp.c
@@ -53,6 +53,12 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "compiler/preprocessor/slglobals.h"
+#if defined(_MSC_VER)
+#pragma warning(disable: 4054)
+#pragma warning(disable: 4152)
+#pragma warning(disable: 4706)
+#endif
+
static int CPPif(yystypepp * yylvalpp);
/* Don't use memory.c's replacements, as we clean up properly here */
@@ -84,7 +90,6 @@ static int extensionAtom = 0;
static Scope *macros = 0;
#define MAX_MACRO_ARGS 64
-#define MAX_IF_NESTING 64
static SourceLoc ifloc; /* outermost #if */
@@ -285,18 +290,30 @@ static int CPPelse(int matchelse, yystypepp * yylvalpp)
atom = yylvalpp->sc_ident;
if (atom == ifAtom || atom == ifdefAtom || atom == ifndefAtom){
depth++; cpp->ifdepth++; cpp->elsetracker++;
+ if (cpp->ifdepth > MAX_IF_NESTING) {
+ CPPErrorToInfoLog("max #if nesting depth exceeded");
+ cpp->CompileError = 1;
+ return 0;
+ }
+ // sanity check elsetracker
+ if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
+ CPPErrorToInfoLog("mismatched #if/#endif statements");
+ cpp->CompileError = 1;
+ return 0;
+ }
cpp->elsedepth[cpp->elsetracker] = 0;
- }
- else if (atom == endifAtom) {
+ }
+ else if (atom == endifAtom) {
if(--depth<0){
- --cpp->elsetracker;
+ if (cpp->elsetracker)
+ --cpp->elsetracker;
if (cpp->ifdepth)
--cpp->ifdepth;
break;
}
- --cpp->elsetracker;
- --cpp->ifdepth;
- }
+ --cpp->elsetracker;
+ --cpp->ifdepth;
+ }
else if (((int)(matchelse) != 0)&& depth==0) {
if (atom == elseAtom ) {
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
@@ -325,6 +342,7 @@ static int CPPelse(int matchelse, yystypepp * yylvalpp)
else if((atom==elseAtom) && (!ChkCorrectElseNesting())){
CPPErrorToInfoLog("#else after a #else");
cpp->CompileError=1;
+ return 0;
}
};
return token;
@@ -456,6 +474,17 @@ static int eval(int token, int prec, int *res, int *err, yystypepp * yylvalpp)
val = *res;
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
token = eval(token, binop[i].prec, res, err, yylvalpp);
+
+ if (binop[i].op == op_div || binop[i].op == op_mod)
+ {
+ if (*res == 0)
+ {
+ CPPErrorToInfoLog("preprocessor divide or modulo by zero");
+ *err = 1;
+ return token;
+ }
+ }
+
*res = binop[i].op(val, *res);
}
return token;
@@ -469,15 +498,24 @@ error:
static int CPPif(yystypepp * yylvalpp) {
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
int res = 0, err = 0;
- cpp->elsetracker++;
- cpp->elsedepth[cpp->elsetracker] = 0;
+
if (!cpp->ifdepth++)
ifloc = *cpp->tokenLoc;
- if(cpp->ifdepth >MAX_IF_NESTING){
+ if(cpp->ifdepth > MAX_IF_NESTING){
CPPErrorToInfoLog("max #if nesting depth exceeded");
- return 0;
- }
- token = eval(token, MIN_PREC, &res, &err, yylvalpp);
+ cpp->CompileError = 1;
+ return 0;
+ }
+ cpp->elsetracker++;
+ // sanity check elsetracker
+ if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
+ CPPErrorToInfoLog("mismatched #if/#endif statements");
+ cpp->CompileError = 1;
+ return 0;
+ }
+ cpp->elsedepth[cpp->elsetracker] = 0;
+
+ token = eval(token, MIN_PREC, &res, &err, yylvalpp);
if (token != '\n') {
CPPWarningToInfoLog("unexpected tokens following #if preprocessor directive - expected a newline");
while (token != '\n') {
@@ -499,12 +537,20 @@ static int CPPifdef(int defined, yystypepp * yylvalpp)
{
int token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
int name = yylvalpp->sc_ident;
- if(++cpp->ifdepth >MAX_IF_NESTING){
- CPPErrorToInfoLog("max #if nesting depth exceeded");
- return 0;
- }
- cpp->elsetracker++;
+ if(++cpp->ifdepth > MAX_IF_NESTING){
+ CPPErrorToInfoLog("max #if nesting depth exceeded");
+ cpp->CompileError = 1;
+ return 0;
+ }
+ cpp->elsetracker++;
+ // sanity check elsetracker
+ if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
+ CPPErrorToInfoLog("mismatched #if/#endif statements");
+ cpp->CompileError = 1;
+ return 0;
+ }
cpp->elsedepth[cpp->elsetracker] = 0;
+
if (token != CPP_IDENTIFIER) {
defined ? CPPErrorToInfoLog("ifdef"):CPPErrorToInfoLog("ifndef");
} else {
@@ -748,6 +794,7 @@ int readCPPline(yystypepp * yylvalpp)
if (!cpp->ifdepth ){
CPPErrorToInfoLog("#else mismatch");
cpp->CompileError=1;
+ return 0;
}
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
if (token != '\n') {
@@ -763,14 +810,17 @@ int readCPPline(yystypepp * yylvalpp)
token = CPPelse(0, yylvalpp);
}else{
CPPErrorToInfoLog("#else after a #else");
- cpp->ifdepth=0;
+ cpp->ifdepth = 0;
+ cpp->elsetracker = 0;
cpp->pastFirstStatement = 1;
+ cpp->CompileError = 1;
return 0;
}
} else if (yylvalpp->sc_ident == elifAtom) {
if (!cpp->ifdepth){
CPPErrorToInfoLog("#elif mismatch");
cpp->CompileError=1;
+ return 0;
}
// this token is really a dont care, but we still need to eat the tokens
token = cpp->currentInput->scan(cpp->currentInput, yylvalpp);
@@ -779,18 +829,22 @@ int readCPPline(yystypepp * yylvalpp)
if (token <= 0) { // EOF or error
CPPErrorToInfoLog("unexpect tokens following #elif preprocessor directive - expected a newline");
cpp->CompileError = 1;
- break;
+ return 0;
}
}
token = CPPelse(0, yylvalpp);
} else if (yylvalpp->sc_ident == endifAtom) {
- --cpp->elsetracker;
if (!cpp->ifdepth){
CPPErrorToInfoLog("#endif mismatch");
cpp->CompileError=1;
+ return 0;
}
else
- --cpp->ifdepth;
+ --cpp->ifdepth;
+
+ if (cpp->elsetracker)
+ --cpp->elsetracker;
+
} else if (yylvalpp->sc_ident == ifAtom) {
token = CPPif(yylvalpp);
} else if (yylvalpp->sc_ident == ifdefAtom) {
@@ -1051,9 +1105,14 @@ int MacroExpand(int atom, yystypepp * yylvalpp)
int ChkCorrectElseNesting(void)
{
- if(cpp->elsedepth[cpp->elsetracker]==0){
- cpp->elsedepth[cpp->elsetracker]=1;
- return 1;
+ // sanity check to make sure elsetracker is in a valid range
+ if (cpp->elsetracker < 0 || cpp->elsetracker >= MAX_IF_NESTING) {
+ return 0;
+ }
+
+ if (cpp->elsedepth[cpp->elsetracker] == 0) {
+ cpp->elsedepth[cpp->elsetracker] = 1;
+ return 1;
}
return 0;
}