A round of increasing robustness against buffer overruns.

git-svn-id: https://cvs.khronos.org/svn/repos/ogl/trunk/ecosystem/public/sdk/tools/glslang@21917 e7fa87d3-cd2b-0410-9028-fcbf551c1848
This commit is contained in:
John Kessenich 2013-06-10 07:36:26 +00:00
parent 7408a69952
commit 00f605b930
11 changed files with 98 additions and 32 deletions

View file

@ -268,7 +268,7 @@ static int lFloatConst(char *str, int len, int ch, yystypepp * yylvalpp)
if (ch == 'e' || ch == 'E') {
if (len >= MAX_SYMBOL_NAME_LEN) {
ShPpErrorToInfoLog("floating-point literal too long");
ShPpErrorToInfoLog("floating-point literal too long");
len = 1,str_len=1;
} else {
ExpSign = 1;
@ -311,11 +311,11 @@ static int lFloatConst(char *str, int len, int ch, yystypepp * yylvalpp)
cpp->currentInput->ungetch(cpp->currentInput, ch2, yylvalpp);
cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
} else {
if (len < MAX_SYMBOL_NAME_LEN-1) {
if (len < MAX_SYMBOL_NAME_LEN) {
str[len++] = ch;
str[len++] = ch2;
} else {
ShPpErrorToInfoLog("floating-point literal too long");
ShPpErrorToInfoLog("floating-point literal too long");
len = 1,str_len=1;
}
}
@ -348,7 +348,7 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
{
char symbol_name[MAX_SYMBOL_NAME_LEN + 1];
char string_val[MAX_STRING_LEN + 1];
int AlreadyComplained;
int AlreadyComplained = 0;
int len, ch, ii;
unsigned ival = 0;
@ -387,14 +387,15 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
len++;
ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
} else {
ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
ShPpErrorToInfoLog("name too long");
break;
}
} while ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '_');
if (len >= MAX_SYMBOL_NAME_LEN)
len = MAX_SYMBOL_NAME_LEN - 1;
if (len > MAX_SYMBOL_NAME_LEN)
len = MAX_SYMBOL_NAME_LEN;
symbol_name[len] = '\0';
cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
yylvalpp->sc_ident = LookUpAddString(atable, symbol_name);
@ -410,23 +411,26 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
(ch >= 'A' && ch <= 'F') ||
(ch >= 'a' && ch <= 'f'))
{
AlreadyComplained = 0;
ival = 0;
do {
if (len >= MAX_SYMBOL_NAME_LEN)
break;
yylvalpp->symbol_name[len++] = ch;
if (ival <= 0x0fffffff) {
if (ch >= '0' && ch <= '9') {
ii = ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
ii = ch - 'A' + 10;
} else {
} else if (ch >= 'a' && ch <= 'f') {
ii = ch - 'a' + 10;
}
} else
ShPpErrorToInfoLog("bad digit in hexidecimal literal");
ival = (ival << 4) | ii;
} else {
if (!AlreadyComplained)
if (! AlreadyComplained) {
ShPpErrorToInfoLog("hexidecimal literal too long");
AlreadyComplained = 1;
AlreadyComplained = 1;
}
}
ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
} while ((ch >= '0' && ch <= '9') ||
@ -441,19 +445,22 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
yylvalpp->symbol_name[len] = '\0';
yylvalpp->sc_int = (int)ival;
return CPP_INTCONSTANT;
} else if (ch >= '0' && ch <= '7') { // octal integer constants
AlreadyComplained = 0;
ival = 0;
do {
if (len >= MAX_SYMBOL_NAME_LEN)
break;
yylvalpp->symbol_name[len++] = ch;
if (ival <= 0x1fffffff) {
ii = ch - '0';
ival = (ival << 3) | ii;
} else {
if (!AlreadyComplained)
ShPpErrorToInfoLog("octal literal too long");
AlreadyComplained = 1;
if (!AlreadyComplained) {
ShPpErrorToInfoLog("octal literal too long");
AlreadyComplained = 1;
}
}
ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
} while (ch >= '0' && ch <= '7');
@ -462,6 +469,7 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
yylvalpp->symbol_name[len] = '\0';
cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
yylvalpp->sc_int = (int)ival;
return CPP_INTCONSTANT;
} else {
cpp->currentInput->ungetch(cpp->currentInput, ch, yylvalpp);
@ -477,6 +485,9 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
len++;
}
ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
} else {
ShPpErrorToInfoLog("token too long");
break;
}
} while (ch >= '0' && ch <= '9');
if (ch == '.' || ch == 'e' || ch == 'f' || ch == 'h' || ch == 'x'|| ch == 'E' || ch == 'F' || ch == 'l' || ch == 'L') {
@ -491,13 +502,13 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
yylvalpp->symbol_name[len] = '\0';
ival = 0;
AlreadyComplained = 0;
for (ii = 0; ii < numericLen; ii++) {
ch = yylvalpp->symbol_name[ii] - '0';
if ((ival > 429496729) || (ival == 429496729 && ch >= 6)) {
if (! AlreadyComplained)
if (! AlreadyComplained) {
ShPpErrorToInfoLog("integral literal too long");
AlreadyComplained = 1;
AlreadyComplained = 1;
}
}
ival = ival * 10 + ch;
}
@ -711,7 +722,8 @@ static int byte_scan(InputSrc *in, yystypepp * yylvalpp)
string_val[len] = ch;
len++;
ch = cpp->currentInput->getch(cpp->currentInput, yylvalpp);
}
} else
break;
};
string_val[len] = '\0';
if (ch == '"') {

View file

@ -81,8 +81,8 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if !defined(__SCANNER_H)
#define __SCANNER_H 1
#define MAX_SYMBOL_NAME_LEN 128
#define MAX_STRING_LEN 512
#define MAX_SYMBOL_NAME_LEN 1025
#define MAX_STRING_LEN 1025
#include "parser.h"

View file

@ -316,6 +316,9 @@ int ReadToken(TokenStream *pTok, yystypepp * yylvalpp)
symbol_name[len] = ch;
len++;
ch = lReadByte(pTok);
} else {
ShPpErrorToInfoLog("token too long");
break;
}
}
symbol_name[len] = '\0';
@ -325,9 +328,13 @@ int ReadToken(TokenStream *pTok, yystypepp * yylvalpp)
break;
case CPP_STRCONSTANT:
len = 0;
while ((ch = lReadByte(pTok)) != 0)
while ((ch = lReadByte(pTok)) != 0) {
if (len < MAX_STRING_LEN)
string_val[len++] = ch;
else
break;
}
string_val[len] = 0;
yylvalpp->sc_ident = LookUpAddString(atable, string_val);
break;
@ -340,6 +347,9 @@ int ReadToken(TokenStream *pTok, yystypepp * yylvalpp)
symbol_name[len] = ch;
len++;
ch = lReadByte(pTok);
} else {
ShPpErrorToInfoLog("token too long");
break;
}
}
symbol_name[len] = '\0';
@ -356,6 +366,9 @@ int ReadToken(TokenStream *pTok, yystypepp * yylvalpp)
symbol_name[len] = ch;
len++;
ch = lReadByte(pTok);
} else {
ShPpErrorToInfoLog("token too long");
break;
}
}
symbol_name[len] = '\0';
@ -443,7 +456,7 @@ void UngetToken(int token, yystypepp * yylvalpp) {
void DumpTokenStream(FILE *fp, TokenStream *s, yystypepp * yylvalpp) {
int token;
const int maxSize = 100;
const int maxSize = MAX_SYMBOL_NAME_LEN + 5;
char str[100];
if (fp == 0) fp = stdout;