Skip to contentSkip to conversation

fix crash when inlining casts of erroneous expressions

PATCH New Luc Van Oostenryck 1 participant 1 message Jun 7, 2022 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix crash when inlining casts of erroneous expressions

From: Luc Van Oostenryck <[email protected]>

Sparse do inlining very early, during expansion, just after (type) evaluation and before IR linearization, and is done even if some errors have been found. This means that the inlining must be robust against erroneous code.

However, during inlining, a cast expression is always dereferenced and a crash will occur if not valid (in which case it should be null).

Fix this by checking for null cast expressions and directly returning NULL, like done for the inlining of the other invalid expressions.

---
 inline.c                                |  2 ++
 validation/inline-early/bug-bad-token.c | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 validation/inline-early/bug-bad-token.c
inline.c +2 -0
index eceef8ba0443..0097e4bf620a 100644
@@ -155,6 +155,8 @@ static struct expression * copy_expression(struct expression *expr)
 
 	/* Cast/sizeof/__alignof__ */
 	case EXPR_CAST:
+		if (!expr->cast_expression)
+			return NULL;
 		if (expr->cast_expression->type == EXPR_INITIALIZER) {
 			struct expression *cast = expr->cast_expression;
 			struct symbol *sym = expr->cast_type;
validation/inline-early/bug-bad-token.c new file +15 -0
new file mode 100644
index 000000000000..9049bdb48ab0
@@ -0,0 +1,15 @@
+inline void fun(int x)
+{
+	(typeof(@)) x;
+}
+
+void foo(void)
+{
+	fun;
+}
+
+/*
+ * check-name: bug-bad-token
+ * check-exit-value: 0
+ * check-error-ignore
+ */
Signature · 1 line
2.36.1