Skip to contentSkip to conversation

fix infinite loop when expanding __builtin_object_size() with self-init vars

PATCH New Luc Van Oostenryck 1 participant 1 message May 21, 2022 Sparse checker

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix infinite loop when expanding __builtin_object_size() with self-init vars

From: Luc Van Oostenryck <[email protected]>

expand_object_size(), used to expand __builtin_object_size(), recursively try to get the parent initializer. This fails miserably by looping endlessly when the object is a self-initialized variable.

For the moment, fix this in the most obvious way: stop the recursion and do not expand such variables.

Note: I wouldn't be surprised if these self-initialized variables create
other problems elsewhere. Maybe we should remove their initializer and somehow mark them as "do not warn about -Wuninitialized" (well, there is no such warnings *yet*).

---
 builtin.c                              |  8 ++++++++
 validation/builtin-objsize-self-init.c | 11 +++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 validation/builtin-objsize-self-init.c
builtin.c +8 -0
index 8e1d2d7e9386..3a29c3aec8a6 100644
@@ -546,11 +546,19 @@ static int expand_object_size(struct expression *expr, int cost)
 			// a deref is just intermediate variable
 			// and so the offset needs to be zeroed.
 			if (arg->op == '*') {
+				struct expression *parent = arg;
 				arg = arg->unop;
 				off = 0;
 				switch (arg->type) {
 				case EXPR_SYMBOL:
 					arg = arg->symbol->initializer;
+					if (arg == parent) {
+						// stop at self-initialized vars
+						// and do not expand them.
+						arg = NULL;
+						val = -1;
+						break;
+					}
 					continue;
 				default:
 					break;
validation/builtin-objsize-self-init.c new file +11 -0
new file mode 100644
index 000000000000..77e3da43e6d2
@@ -0,0 +1,11 @@
+static void f(void)
+{
+	void *param = param;
+	__builtin_object_size(param, 0);
+}
+
+/*
+ * check-name: builtin-objsize-self-init
+ * check-timeout:
+ * check-error-end
+ */
Signature · 1 line
2.36.1