Linux-Sparse Archive mirror
 help / color / mirror / Atom feed
From: Luc Van Oostenryck <lucvoo@kernel.org>
To: linux-sparse@vger.kernel.org
Cc: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [PATCH 1/2] cast_value: assign the new type
Date: Tue, 31 May 2022 21:32:25 +0200	[thread overview]
Message-ID: <20220531193226.33399-2-lucvoo@kernel.org> (raw)
In-Reply-To: <20220531193226.33399-1-lucvoo@kernel.org>

From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>

The first two arguments of cast_value() are the new expression and the
type wanted for it. This type is then used to calculate the new value.

But the type of the expression must be assigned separately (usually
after the cast because the old and the new expression can refer to
the same object).

To avoid any possible inconsistencies, assign the new type during the
casting itself.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 expand.c     | 4 ++++
 expression.c | 1 -
 parse.c      | 1 -
 3 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/expand.c b/expand.c
index c4f806dee1ba..fb11d77aecbc 100644
--- a/expand.c
+++ b/expand.c
@@ -110,11 +110,13 @@ void cast_value(struct expression *expr, struct symbol *newtype,
 	expr->taint = old->taint;
 	if (old_size == new_size) {
 		expr->value = old->value;
+		expr->ctype = newtype;
 		return;
 	}
 
 	// expand it to the full "long long" value
 	value = get_longlong(old);
+	expr->ctype = newtype;
 
 Int:
 	// _Bool requires a zero test rather than truncation.
@@ -153,6 +155,7 @@ Float:
 		value = (long long)old->fvalue;
 		expr->type = EXPR_VALUE;
 		expr->taint = 0;
+		expr->ctype = newtype;
 		goto Int;
 	}
 
@@ -168,6 +171,7 @@ Float:
 			expr->fvalue = (float)expr->fvalue;
 	}
 	expr->type = EXPR_FVALUE;
+	expr->ctype = newtype;
 }
 
 /* Return true if constant shift size is valid */
diff --git a/expression.c b/expression.c
index efdaa36782c7..bead007f9bda 100644
--- a/expression.c
+++ b/expression.c
@@ -433,7 +433,6 @@ struct token *primary_expression(struct token *token, struct expression **tree)
 		if (token_type(token) < TOKEN_WIDE_CHAR) {
 			expr->ctype = &char_ctype;
 			cast_value(expr, &int_ctype, expr, expr->ctype);
-			expr->ctype = &int_ctype;
 		} else {
 			expr->ctype = wchar_ctype;
 		}
diff --git a/parse.c b/parse.c
index bc1c0602fcb4..0e51b3a33e53 100644
--- a/parse.c
+++ b/parse.c
@@ -904,7 +904,6 @@ static void cast_enum_list(struct symbol_list *list, struct symbol *base_type)
 			continue;
 		}
 		cast_value(expr, base_type, expr, ctype);
-		expr->ctype = base_type;
 	} END_FOR_EACH_PTR(sym);
 }
 
-- 
2.36.1


  reply	other threads:[~2022-05-31 19:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-31 19:32 [PATCH 0/3] improve cast_value() Luc Van Oostenryck
2022-05-31 19:32 ` Luc Van Oostenryck [this message]
2022-05-31 19:32 ` [PATCH 2/2] cast_value: remove error-prone redundant argument Luc Van Oostenryck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220531193226.33399-2-lucvoo@kernel.org \
    --to=lucvoo@kernel.org \
    --cc=linux-sparse@vger.kernel.org \
    --cc=luc.vanoostenryck@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).