Skip to contentSkip to conversation

fix casting constant to _Bool

PATCH New Xi Wang 1 participant 1 message Jun 17, 2012 Sparse checker

lore
Share & tools
lore

Xi Wang

author
#0 lore raw

PATCH v2 1/1 fix casting constant to _Bool

Casting to _Bool requires a zero test rather than truncation. Also add a new cast warning for _Bool since this is not about truncation.

Cc Pekka Enberg <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Dan Carpenter <[email protected]>
---
 expand.c |    8 ++++++++
 1 file changed, 8 insertions(+)
expand.c +8 -0
index 63a9075..fcdefcb 100644
@@ -92,6 +92,14 @@ void cast_value(struct expression *expr, struct symbol *newtype,
 	value = get_longlong(old);
 
 Int:
+	// _Bool requires a zero test rather than truncation.
+	if (is_bool_type(newtype)) {
+		expr->value = value ? 1 : 0;
+		if (!conservative && value != 0 && value != 1)
+			warning(old->pos, "odd constant _Bool cast (%llx becomes 1)", value);
+		return;
+	}
+
 	// Truncate it to the new size
 	signmask = 1ULL << (new_size-1);
 	mask = signmask | (signmask-1);
Signature · 1 line
1.7.9.5