2
0
Эх сурвалжийг харах

Added code to manage integer overflow at compile time, only the basics, need to be extended.

mingodad 13 жил өмнө
parent
commit
ac4e7c738a
1 өөрчлөгдсөн 17 нэмэгдсэн , 7 устгасан
  1. 17 7
      squirrel/sqlexer.cpp

+ 17 - 7
squirrel/sqlexer.cpp

@@ -4,6 +4,7 @@
 #include "sqpcheader.h"
 #include <ctype.h>
 #include <stdlib.h>
+#include <limits.h>
 #include "sqtable.h"
 #include "sqstring.h"
 #include "sqcompiler.h"
@@ -473,7 +474,7 @@ void LexInteger(const SQChar *s,SQUnsignedInteger *res)
 	while(*s != 0)
 	{
 		*res = (*res)*10+((*s++)-'0');
-	}
+	}
 }
 
 SQInteger scisodigit(SQInteger c) { return c >= _SC('0') && c <= _SC('7'); }
@@ -499,7 +500,8 @@ SQInteger SQLexer::ReadNumber()
 #define THEX 3
 #define TSCIENTIFIC 4
 #define TOCTAL 5
-	SQInteger type = TINT, firstchar = CUR_CHAR;
+	SQInteger type = TINT, firstchar = CUR_CHAR;
+	SQUnsignedInteger itmp;
 	SQChar *sTemp;
 	INIT_TEMP_STRING();
 	NEXT();
@@ -549,13 +551,21 @@ SQInteger SQLexer::ReadNumber()
 		_fvalue = (SQFloat)scstrtod(&_longstr[0],&sTemp);
 		return TK_FLOAT;
 	case TINT:
-		LexInteger(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
-		return TK_INTEGER;
+		LexInteger(&_longstr[0],&itmp);
+		break;
 	case THEX:
-		LexHexadecimal(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
-		return TK_INTEGER;
+		LexHexadecimal(&_longstr[0],&itmp);
+		break;
 	case TOCTAL:
-		LexOctal(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
+		LexOctal(&_longstr[0],&itmp);
+		break;
+	}
+	switch(type) {
+	case TINT:
+	case THEX:
+	case TOCTAL:
+        if(itmp > INT_MAX) Error(_SC("integer overflow"));
+        _nvalue = (SQInteger) itmp;
 		return TK_INTEGER;
 	}
 	return 0;