public inbox for smatch@vger.kernel.org
 help / color / mirror / Atom feed
From: John Levon <levon@movementarian.org>
To: smatch@vger.kernel.org
Cc: rm@fingolfin.org
Subject: sval_type_max() sadness
Date: Wed, 11 Dec 2019 14:24:30 +0000	[thread overview]
Message-ID: <20191211142430.GA17277@movementarian.org> (raw)


Robert (Cc:ed) found a false positive from smatch, which boils down to:

```
#include <limits.h>

int
main(int argc, char *argv[])
{
	long double sec = 0;
	// also __int128 !

        if (sec > (float)INT_MAX) {
		0;
	}
}
```

$ ./src/smatch/smatch -m32 test.smatch.c 
test.smatch.c:9 main() warn: impossible condition '(sec > 2147483647) =>
(s32min-s32max > s32max)'


A cheesy fix here is:

$ git diff
diff --git a/smatch_type.c b/smatch_type.c
index 305a0b5c..1e289363 100644
--- a/smatch_type.c
+++ b/smatch_type.c
@@ -411,7 +411,11 @@ sval_t sval_type_max(struct symbol *base_type)
                base_type = &llong_ctype;
        ret.type = base_type;
 
-       ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
+       int pos_bits = type_positive_bits(base_type);
+       if (pos_bits > 63)
+               pos_bits = 63;
+
+       ret.value = (~0ULL) >> (64 - pos_bits);
        return ret;
 }
 

The issue being that for these types, type_positive_bits() is 64 or
more. If only there were a tool to warn about negative shift operands :)

But type_positive_bits() is used all over, so I have no idea if there's
a lot of other problems.

Or, if this patch is OK on its own, I can add a couple of tests and send
a proper patch over.

Thoughts?

thanks
john

             reply	other threads:[~2019-12-11 14:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-11 14:24 John Levon [this message]
2019-12-12 16:55 ` sval_type_max() sadness Dan Carpenter

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=20191211142430.GA17277@movementarian.org \
    --to=levon@movementarian.org \
    --cc=rm@fingolfin.org \
    --cc=smatch@vger.kernel.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