linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx@kernel.org>
To: "Daniel Marjamäki" <daniel.marjamaki@gmail.com>
Cc: linux-man <linux-man@vger.kernel.org>
Subject: [cppcheck]: [knownConditionTrueFalse]: false positive
Date: Thu, 31 Aug 2023 01:30:21 +0200	[thread overview]
Message-ID: <e13c74fb-e17a-ba14-f1fc-9c75f05090db@kernel.org> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1396 bytes --]

Hi Daniel,

I found a false positive in cppcheck, regarding knownConditionTrueFalse.
You can see by yourself.

Since I don't know of a mailing list for cppcheck(1), and I found this
in a program in the EXAMPLES of a Linux manual page, I'm CCing the
linux-man@ list.


$ cat neg.c 
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	int     opt;
	size_t  s;

	s = -1;

	while ((opt = getopt(argc, argv, "s:")) != -1) {
		switch (opt) {
		case 's':   s = strtoul(optarg, NULL, 0);break;
		default:    break;
		}
	}
	if (s == -1)
		return 1;
	return 0;
}


$ cc neg.c -Wall -Wextra
neg.c: In function ‘main’:
neg.c:19:15: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
   20 |         if (s == -1)
      |               ^~


$ cppcheck --enable=all --error-exitcode=2 --inconclusive --quiet --suppress=missingIncludeSystem  ./neg.c 
neg.c:20:8: style: Condition 's==-1' is always false [knownConditionTrueFalse]
 if (s == -1)
       ^


$ ./a.out; echo $?
1



While I agree that it's not the best style, that comparison is certainly
possibly true after integer promotions.

Cheers,
Alex


-- 
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

                 reply	other threads:[~2023-08-30 23:30 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=e13c74fb-e17a-ba14-f1fc-9c75f05090db@kernel.org \
    --to=alx@kernel.org \
    --cc=daniel.marjamaki@gmail.com \
    --cc=linux-man@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;
as well as URLs for NNTP newsgroup(s).