diff mbox series

fix zero/sign extension of integer character constants

Message ID 20220522190858.42163-1-lucvoo@kernel.org (mailing list archive)
State Mainlined, archived
Headers show
Series fix zero/sign extension of integer character constants | expand

Commit Message

Luc Van Oostenryck May 22, 2022, 7:08 p.m. UTC
From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>

An integer character constant has type 'int' but, subtly enough,
its value is the one of a 'char' converted to an 'int'.

So, do this conversion.
Also set the type of wide character constants from 'long' to 'wchar_t'.

Link: https://lore.kernel.org/r/20210927130253.GH2083@kadam
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Reported-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expression.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/expression.c b/expression.c
index 221d7780a76e..e3b58cb5b653 100644
--- a/expression.c
+++ b/expression.c
@@ -427,8 +427,15 @@  struct token *primary_expression(struct token *token, struct expression **tree)
 	case TOKEN_CHAR ... TOKEN_WIDE_CHAR_EMBEDDED_3:
 		expr = alloc_expression(token->pos, EXPR_VALUE);
 		expr->flags = CEF_SET_CHAR;
-		expr->ctype = token_type(token) < TOKEN_WIDE_CHAR ? &int_ctype : &long_ctype;
 		get_char_constant(token, &expr->value);
+
+		// TODO: handle 'u8', 'u' & 'U' prefixes.
+		if (token_type(token) < TOKEN_WIDE_CHAR) {
+			expr->ctype = &int_ctype;
+			expr->value = extend_value(expr->value, &char_ctype);
+		} else {
+			expr->ctype = wchar_ctype;
+		}
 		token = token->next;
 		break;