Skip to content

Commit 204a5f7

Browse files
committed
Fix a preprocessor expression evaluation bug
A subexpression in parentheses lost its string/int type flag and instead used whatever type was left in the stack entry from previous use. In practice we mostly got away with this because most preprocessor expressions are integer, but it could have resulted in a preprocessor expression incorrectly evaluating as zero. If -Wextra was in use you got a warning: Warning 202: Error: 'Can't mix strings and integers in expression' Fixes swig#1384
1 parent 471b9f7 commit 204a5f7

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGES.current

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
77
Version 4.1.0 (in progress)
88
===========================
99

10+
2022-02-23: olly
11+
#1384 Fix a preprocessor expression evaluation bug. A
12+
subexpression in parentheses lost its string/int type flag and
13+
instead used whatever type was left in the stack entry from
14+
previous use. In practice we mostly got away with this because
15+
most preprocessor expressions are integer, but it could have
16+
resulted in a preprocessor expression incorrectly evaluating as
17+
zero. If -Wextra was in use you got a warning:
18+
19+
Warning 202: Error: 'Can't mix strings and integers in expression'
20+
1021
2022-02-20: wsfulton
1122
Fix %warnfilter warning suppress for warning 315 SWIGWARN_PARSE_USING_UNDEF.
1223

Examples/test-suite/preproc.i

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@
1111
#pragma SWIG nowarn=890 /* lots of Go name conflicts */
1212
#pragma SWIG nowarn=206 /* Unexpected tokens after #endif directive. */
1313

14+
/* Regression test: in SWIG < 4.1.0 this triggered the two #error cases.
15+
* Github issue #1384
16+
*/
17+
#if "" != ""
18+
#endif
19+
#if 1 && (!0)
20+
// Should go here
21+
#else
22+
# error BUG
23+
#endif
24+
#if ((("" == ""))) || (1 && (!1))
25+
// Should go here
26+
#else
27+
# error BUG
28+
#endif
29+
1430
%{
1531
#if defined(__clang__)
1632
/*Suppress: warning: use of logical '&&' with constant operand [-Wconstant-logical-operand]*/

Source/Preprocessor/expr.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ int Preprocessor_expr(DOH *s, int *error) {
406406
goto extra_rparen;
407407
stack[sp - 1].op = EXPR_VALUE;
408408
stack[sp - 1].value = stack[sp].value;
409+
stack[sp - 1].svalue = stack[sp].svalue;
409410
sp--;
410411
break;
411412
default:

0 commit comments

Comments
 (0)