Skip to content

Commit 4986b87

Browse files
committed
Emit preprocessor expr error for unary + on string
Previously other unary operators on a string all gave an error, but unary + was handled as a no-op and so didn't.
1 parent 4db4b99 commit 4986b87

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

Examples/test-suite/errors/pp_expressions_bad.i

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,18 @@
5151
/* This didn't fail prior with SWIG < 4.1. Github #1384. */
5252
#if 1 == ("TWO")
5353
#endif
54+
55+
/* These should all give errors. */
56+
#if "1"
57+
#endif
58+
#if -"1"
59+
#endif
60+
#if "1" == -"-1"
61+
#endif
62+
#if "1" == !"-1"
63+
#endif
64+
#if "1" == ~"1"
65+
#endif
66+
/* Unary + was a no-op and so this didn't give an error in SWIG < 4.1.0. */
67+
#if "1" == +"1"
68+
#endif

Examples/test-suite/errors/pp_expressions_bad.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@ pp_expressions_bad.i:48: Warning 202: Could not evaluate expression '"TWO" == 1'
2121
pp_expressions_bad.i:48: Warning 202: Can't mix strings and integers in expression
2222
pp_expressions_bad.i:52: Warning 202: Could not evaluate expression '1 == ("TWO")'
2323
pp_expressions_bad.i:52: Warning 202: Can't mix strings and integers in expression
24+
pp_expressions_bad.i:58: Warning 202: Could not evaluate expression '-"1"'
25+
pp_expressions_bad.i:58: Warning 202: Syntax error: attempt to apply unary operator to string
26+
pp_expressions_bad.i:60: Warning 202: Could not evaluate expression '"1" == -"-1"'
27+
pp_expressions_bad.i:60: Warning 202: Syntax error: attempt to apply unary operator to string
28+
pp_expressions_bad.i:62: Warning 202: Could not evaluate expression '"1" == !"-1"'
29+
pp_expressions_bad.i:62: Warning 202: Syntax error: attempt to apply unary operator to string
30+
pp_expressions_bad.i:64: Warning 202: Could not evaluate expression '"1" == ~"1"'
31+
pp_expressions_bad.i:64: Warning 202: Syntax error: attempt to apply unary operator to string
32+
pp_expressions_bad.i:67: Warning 202: Could not evaluate expression '"1" == +"1"'
33+
pp_expressions_bad.i:67: Warning 202: Syntax error: attempt to apply unary operator to string

Source/Preprocessor/expr.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ typedef struct {
3939
#define EXPR_OP 3
4040
#define EXPR_GROUP 4
4141

42-
/* Special token value used here to distinguish from SWIG_TOKEN_MINUS
43-
* (which we use here for a two argument minus).
42+
/* Special token values used here to distinguish from SWIG_TOKEN_MINUS
43+
* and SWIG_TOKEN_PLUS (which we use here for a two argument versions).
4444
*/
4545
#define OP_UMINUS 100
46+
#define OP_UPLUS 101
4647

4748
static exprval stack[256]; /* Parsing stack */
4849
static int sp = 0; /* Stack pointer */
@@ -54,6 +55,7 @@ static const char *errmsg = 0; /* Parsing error */
5455
static void init_precedence() {
5556
prec[SWIG_TOKEN_NOT] = 10;
5657
prec[OP_UMINUS] = 10;
58+
prec[OP_UPLUS] = 10;
5759
prec[SWIG_TOKEN_STAR] = 20;
5860
prec[SWIG_TOKEN_SLASH] = 20;
5961
prec[SWIG_TOKEN_PERCENT] = 20;
@@ -78,7 +80,8 @@ static void init_precedence() {
7880

7981
#define UNARY_OP(token) (((token) == SWIG_TOKEN_NOT) || \
8082
((token) == SWIG_TOKEN_LNOT) || \
81-
((token) == OP_UMINUS))
83+
((token) == OP_UMINUS) || \
84+
((token) == OP_UPLUS))
8285

8386
/* Reduce a single operator on the stack */
8487
/* return 0 on failure, 1 on success */
@@ -202,6 +205,10 @@ static int reduce_op() {
202205
stack[sp - 1].value = -stack[sp].value;
203206
sp--;
204207
break;
208+
case OP_UPLUS:
209+
stack[sp - 1].value = stack[sp].value;
210+
sp--;
211+
break;
205212
case SWIG_TOKEN_SLASH:
206213
if (stack[sp].value != 0) {
207214
stack[sp - 2].value = stack[sp - 2].value / stack[sp].value;
@@ -314,10 +321,11 @@ int Preprocessor_expr(DOH *s, int *error) {
314321
stack[sp].value = (long) strtol(c, 0, 0);
315322
stack[sp].svalue = 0;
316323
stack[sp].op = EXPR_VALUE;
317-
} else if (token == SWIG_TOKEN_PLUS) {
318-
} else if ((token == SWIG_TOKEN_MINUS) || (token == SWIG_TOKEN_LNOT) || (token == SWIG_TOKEN_NOT)) {
324+
} else if ((token == SWIG_TOKEN_MINUS) || (token == SWIG_TOKEN_PLUS) || (token == SWIG_TOKEN_LNOT) || (token == SWIG_TOKEN_NOT)) {
319325
if (token == SWIG_TOKEN_MINUS)
320326
token = OP_UMINUS;
327+
else if (token == SWIG_TOKEN_PLUS)
328+
token = OP_UPLUS;
321329
stack[sp].value = token;
322330
stack[sp].op = EXPR_OP;
323331
sp++;

0 commit comments

Comments
 (0)