linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Xi Wang <xi.wang@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Xi Wang <xi.wang@gmail.com>, Pekka Enberg <penberg@kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Dan Carpenter <dan.carpenter@oracle.com>
Subject: [PATCH v2] fix casting constant to _Bool
Date: Sun, 17 Jun 2012 14:45:05 -0400	[thread overview]
Message-ID: <1339958705-9881-1-git-send-email-xi.wang@gmail.com> (raw)

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 <penberg@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
 expand.c |    8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/expand.c b/expand.c
index 63a9075..fcdefcb 100644
--- a/expand.c
+++ b/expand.c
@@ -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);
-- 
1.7.9.5


                 reply	other threads:[~2012-06-17 18:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1339958705-9881-1-git-send-email-xi.wang@gmail.com \
    --to=xi.wang@gmail.com \
    --cc=dan.carpenter@oracle.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=penberg@kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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 NNTP newsgroup(s).