Improve handling of bad octal and binary numbers

SWIG now gives an error for digits 8 and 9 in octal constants -
previously these were quietly accepted resulting in a bogus value.

C++11 binary constants are now treated similarly - only digits 0
and 1 were allowed before, but trying to use other digits now gives
a clearer error.
This commit is contained in:
Olly Betts 2024-08-15 13:47:47 +12:00
parent d4a80967ca
commit 16680f59da
8 changed files with 37 additions and 5 deletions

View File

@ -7,6 +7,13 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.3.0 (in progress)
===========================
2024-08-15: olly
SWIG now gives an error for digits 8 and 9 in octal constants -
previously these were quietly accepted resulting in a bogus value.
C++11 binary constants are now treated similarly - only digits 0
and 1 were allowed before, but trying to use other digits now gives
a clearer error.
2024-08-12: olly
#657 Allow unmatched ' and " in #error and #warning.

View File

@ -2,6 +2,8 @@
enum stuff {
FOO = 'x',
BAR = 3.14159
BAR = 3.14159,
BADBIN = 0b121,
BADOCTAL = 018118055
};

View File

@ -1 +1,4 @@
c_enum_badvalue.i:6: Error: Type error. Expecting an integral type
c_enum_badvalue.i:5: Error: Type error. Expecting an integral type
c_enum_badvalue.i:6: Error: Invalid digit '2' in binary constant
c_enum_badvalue.i:7: Error: Invalid digit '8' in octal constant
c_enum_badvalue.i:7: Error: Invalid digit '8' in octal constant

View File

@ -50,3 +50,6 @@ comment */
%constant int ggg=;
// Bad binary and octal constants
%constant int badbin = 0b121;
%constant int badoct = 018118055;

View File

@ -6,3 +6,6 @@ pp_constant.i:37: Warning 305: Bad constant value (ignored).
pp_constant.i:44: Warning 305: Bad constant value (ignored).
pp_constant.i:48: Warning 305: Bad constant value (ignored).
pp_constant.i:51: Warning 305: Bad constant value (ignored).
pp_constant.i:54: Error: Invalid digit '2' in binary constant
pp_constant.i:55: Error: Invalid digit '8' in octal constant
pp_constant.i:55: Error: Invalid digit '8' in octal constant

View File

@ -77,3 +77,9 @@
#if MY_VERSION_AT_LEAST(1,2,3)
#warning This should not warn
#endif
/* Test errors for bad digits in binary and octal constants. */
#if 0b01210
#endif
#if 018118055
#endif

View File

@ -35,3 +35,6 @@ pp_expressions_bad.i:73: Warning 202: Could not evaluate expression '(4 <=> 2) <
pp_expressions_bad.i:73: Warning 202: Syntax error
pp_expressions_bad.i:77: Warning 202: Could not evaluate expression 'MY_VERSION_AT_LEAST(1,2,3)'
pp_expressions_bad.i:77: Warning 202: Use of undefined function-like macro
pp_expressions_bad.i:82: Error: Invalid digit '2' in binary constant
pp_expressions_bad.i:84: Error: Invalid digit '8' in octal constant
pp_expressions_bad.i:84: Error: Invalid digit '8' in octal constant

View File

@ -612,7 +612,7 @@ static int look(Scanner *s) {
else if (c == ':')
state = 5; /* maybe double colon */
else if (c == '0')
state = 83; /* An octal or hex value */
state = 83; /* Maybe a hex, octal or binary number */
else if (c == '\"') {
state = 2; /* A string constant */
s->start_line = s->line;
@ -1175,7 +1175,7 @@ static int look(Scanner *s) {
}
break;
case 83:
/* Might be a hexadecimal or octal number */
/* Might be a hexadecimal, octal or binary number */
if ((c = nextchar(s)) == 0)
return SWIG_TOKEN_INT;
if (isdigit(c))
@ -1199,6 +1199,9 @@ static int look(Scanner *s) {
break;
case 84:
/* This is an octal number */
if (c == '8' || c == '9') {
Swig_error(Scanner_file(s), Scanner_line(s), "Invalid digit '%c' in octal constant\n", c);
}
if ((c = nextchar(s)) == 0)
return SWIG_TOKEN_INT;
if (isdigit(c))
@ -1241,7 +1244,9 @@ static int look(Scanner *s) {
return SWIG_TOKEN_INT;
if ((c == '0') || (c == '1'))
state = 850;
else if ((c == 'l') || (c == 'L')) {
else if (isdigit(c)) {
Swig_error(Scanner_file(s), Scanner_line(s), "Invalid digit '%c' in binary constant\n", c);
} else if ((c == 'l') || (c == 'L')) {
state = 87;
} else if ((c == 'u') || (c == 'U')) {
state = 88;