summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubomir Rintel <lkundrak@v3.sk>2022-11-02 20:29:53 +0100
committerEmmanuele Bassi <ebassi@gmail.com>2023-01-08 02:24:39 +0000
commitb37f24b7e27a77c398f41cc331608aff806f0d42 (patch)
tree0c47a5f9e1ce348fd2817ad2d391dc88b32c7001
parent41feafa85d73b4a9dd8eac9dbe1231130bf2c7ca (diff)
downloadgobject-introspection-b37f24b7e27a77c398f41cc331608aff806f0d42.tar.gz
scanner: don't accept invalid symbols in binary expressions
The rules for binary expressions were entirely oblivious to the type of the operand symbols and assumed they're integer constants. This is very unfortunate, since it caused all sort of nonsense to end up getting accepted. One such example is the following define from NetworkManager's libnm: #define NM_SETTING_PARAM_SECRET (1 << (2 + G_PARAM_USER_SHIFT)) As G_PARAM_USER_SHIFT is unknown, it was parsed as an invalid symbol. The addition didn't care, treated it as: #define NM_SETTING_PARAM_SECRET (1 << (2 + 0)) Let's just ensure we get CSYMBOL_TYPE_CONST only when both operands actually have const_int_set. Otherwise just create CSYMBOL_TYPE_INVALID. That will cause the symbol to be dropped on the floor eventually, but that's probably much better than a having an invalid value.
-rw-r--r--giscanner/scannerparser.y58
-rw-r--r--giscanner/sourcescanner.c15
-rw-r--r--giscanner/sourcescanner.h4
-rw-r--r--tests/scanner/Regress-1.0-C-expected/Regress.BAD_EXPR_CONSTANT.page14
-rw-r--r--tests/scanner/Regress-1.0-Gjs-expected/Regress.BAD_EXPR_CONSTANT.page14
-rw-r--r--tests/scanner/Regress-1.0-Python-expected/Regress.BAD_EXPR_CONSTANT.page14
-rw-r--r--tests/scanner/Regress-1.0-expected.gir6
7 files changed, 41 insertions, 84 deletions
diff --git a/giscanner/scannerparser.y b/giscanner/scannerparser.y
index 52fc1996..26c9eba7 100644
--- a/giscanner/scannerparser.y
+++ b/giscanner/scannerparser.y
@@ -582,24 +582,25 @@ multiplicative_expression
: cast_expression
| multiplicative_expression '*' cast_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int * $3->const_int;
}
| multiplicative_expression '/' cast_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
if ($3->const_int != 0) {
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int / $3->const_int;
+ } else {
+ $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_file, lineno);
}
}
| multiplicative_expression '%' cast_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
if ($3->const_int != 0) {
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int % $3->const_int;
+ } else {
+ $$ = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, scanner->current_file, lineno);
}
}
;
@@ -608,14 +609,12 @@ additive_expression
: multiplicative_expression
| additive_expression '+' multiplicative_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int + $3->const_int;
}
| additive_expression '-' multiplicative_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int - $3->const_int;
}
;
@@ -624,8 +623,7 @@ shift_expression
: additive_expression
| shift_expression SL additive_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int << $3->const_int;
/* assume this is a bitfield/flags declaration
@@ -636,8 +634,7 @@ shift_expression
}
| shift_expression SR additive_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int >> $3->const_int;
}
;
@@ -646,26 +643,22 @@ relational_expression
: shift_expression
| relational_expression '<' shift_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int < $3->const_int;
}
| relational_expression '>' shift_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int > $3->const_int;
}
| relational_expression LTEQ shift_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int <= $3->const_int;
}
| relational_expression GTEQ shift_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int >= $3->const_int;
}
;
@@ -674,14 +667,12 @@ equality_expression
: relational_expression
| equality_expression EQ relational_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int == $3->const_int;
}
| equality_expression NOTEQ relational_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int != $3->const_int;
}
;
@@ -690,8 +681,7 @@ and_expression
: equality_expression
| and_expression '&' equality_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int & $3->const_int;
}
;
@@ -700,8 +690,7 @@ exclusive_or_expression
: and_expression
| exclusive_or_expression '^' and_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int ^ $3->const_int;
}
;
@@ -710,8 +699,7 @@ inclusive_or_expression
: exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int = $1->const_int | $3->const_int;
}
;
@@ -720,8 +708,7 @@ logical_and_expression
: inclusive_or_expression
| logical_and_expression ANDAND inclusive_or_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int =
gi_source_symbol_get_const_boolean ($1) &&
gi_source_symbol_get_const_boolean ($3);
@@ -732,8 +719,7 @@ logical_or_expression
: logical_and_expression
| logical_or_expression OROR logical_and_expression
{
- $$ = gi_source_symbol_new (CSYMBOL_TYPE_CONST, scanner->current_file, lineno);
- $$->const_int_set = TRUE;
+ $$ = gi_source_symbol_const_binary ($1, $3, scanner->current_file, lineno);
$$->const_int =
gi_source_symbol_get_const_boolean ($1) ||
gi_source_symbol_get_const_boolean ($3);
diff --git a/giscanner/sourcescanner.c b/giscanner/sourcescanner.c
index 27f82f77..80c61a27 100644
--- a/giscanner/sourcescanner.c
+++ b/giscanner/sourcescanner.c
@@ -74,6 +74,21 @@ gi_source_symbol_copy (GISourceSymbol * symbol)
}
GISourceSymbol *
+gi_source_symbol_const_binary (GISourceSymbol * s1, GISourceSymbol * s2, GFile *file, int line)
+{
+ GISourceSymbol *new_symbol;
+
+ if (s1->const_int_set && s2->const_int_set) {
+ new_symbol = gi_source_symbol_new (CSYMBOL_TYPE_CONST, file, line);
+ new_symbol->const_int_set = TRUE;
+ } else {
+ new_symbol = gi_source_symbol_new (CSYMBOL_TYPE_INVALID, file, line);
+ }
+
+ return new_symbol;
+}
+
+GISourceSymbol *
gi_source_symbol_ref (GISourceSymbol * symbol)
{
symbol->ref_count++;
diff --git a/giscanner/sourcescanner.h b/giscanner/sourcescanner.h
index 40c5fc96..eb2d312f 100644
--- a/giscanner/sourcescanner.h
+++ b/giscanner/sourcescanner.h
@@ -172,6 +172,10 @@ gboolean gi_source_symbol_get_const_boolean (GISourceSymbol *symb
GISourceSymbol * gi_source_symbol_ref (GISourceSymbol *symbol);
void gi_source_symbol_unref (GISourceSymbol *symbol);
GISourceSymbol * gi_source_symbol_copy (GISourceSymbol *symbol);
+GISourceSymbol * gi_source_symbol_const_binary (GISourceSymbol *s1,
+ GISourceSymbol *s2,
+ GFile *file,
+ int line);
/* Private */
void gi_source_scanner_add_symbol (GISourceScanner *scanner,
diff --git a/tests/scanner/Regress-1.0-C-expected/Regress.BAD_EXPR_CONSTANT.page b/tests/scanner/Regress-1.0-C-expected/Regress.BAD_EXPR_CONSTANT.page
deleted file mode 100644
index bc8190a8..00000000
--- a/tests/scanner/Regress-1.0-C-expected/Regress.BAD_EXPR_CONSTANT.page
+++ /dev/null
@@ -1,14 +0,0 @@
-<?xml version="1.0"?>
-<page id="Regress.BAD_EXPR_CONSTANT"
- type="topic"
- style="default"
- xmlns="http://projectmallard.org/1.0/"
- xmlns:api="http://projectmallard.org/experimental/api/"
- xmlns:ui="http://projectmallard.org/1.0/ui/">
- <info>
- <link xref="index" group="default" type="guide"/>
- </info>
- <title>Regress.BAD_EXPR_CONSTANT</title>
-
-
-</page>
diff --git a/tests/scanner/Regress-1.0-Gjs-expected/Regress.BAD_EXPR_CONSTANT.page b/tests/scanner/Regress-1.0-Gjs-expected/Regress.BAD_EXPR_CONSTANT.page
deleted file mode 100644
index bc8190a8..00000000
--- a/tests/scanner/Regress-1.0-Gjs-expected/Regress.BAD_EXPR_CONSTANT.page
+++ /dev/null
@@ -1,14 +0,0 @@
-<?xml version="1.0"?>
-<page id="Regress.BAD_EXPR_CONSTANT"
- type="topic"
- style="default"
- xmlns="http://projectmallard.org/1.0/"
- xmlns:api="http://projectmallard.org/experimental/api/"
- xmlns:ui="http://projectmallard.org/1.0/ui/">
- <info>
- <link xref="index" group="default" type="guide"/>
- </info>
- <title>Regress.BAD_EXPR_CONSTANT</title>
-
-
-</page>
diff --git a/tests/scanner/Regress-1.0-Python-expected/Regress.BAD_EXPR_CONSTANT.page b/tests/scanner/Regress-1.0-Python-expected/Regress.BAD_EXPR_CONSTANT.page
deleted file mode 100644
index bc8190a8..00000000
--- a/tests/scanner/Regress-1.0-Python-expected/Regress.BAD_EXPR_CONSTANT.page
+++ /dev/null
@@ -1,14 +0,0 @@
-<?xml version="1.0"?>
-<page id="Regress.BAD_EXPR_CONSTANT"
- type="topic"
- style="default"
- xmlns="http://projectmallard.org/1.0/"
- xmlns:api="http://projectmallard.org/experimental/api/"
- xmlns:ui="http://projectmallard.org/1.0/ui/">
- <info>
- <link xref="index" group="default" type="guide"/>
- </info>
- <title>Regress.BAD_EXPR_CONSTANT</title>
-
-
-</page>
diff --git a/tests/scanner/Regress-1.0-expected.gir b/tests/scanner/Regress-1.0-expected.gir
index b986c72f..079405d2 100644
--- a/tests/scanner/Regress-1.0-expected.gir
+++ b/tests/scanner/Regress-1.0-expected.gir
@@ -1248,12 +1248,6 @@ it says it's pointer but it's actually a string.</doc>
</field>
</union>
</record>
- <constant name="BAD_EXPR_CONSTANT"
- value="1"
- c:type="REGRESS_BAD_EXPR_CONSTANT">
- <source-position filename="regress.h" line="528"/>
- <type name="gint" c:type="gint"/>
- </constant>
<constant name="BOOL_CONSTANT" value="true" c:type="REGRESS_BOOL_CONSTANT">
<source-position filename="regress.h" line="524"/>
<type name="gboolean" c:type="gboolean"/>