linux-man.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [cppcheck]: [knownConditionTrueFalse]: false positive
@ 2023-08-30 23:30 Alejandro Colomar
  0 siblings, 0 replies; only message in thread
From: Alejandro Colomar @ 2023-08-30 23:30 UTC (permalink / raw)
  To: Daniel Marjamäki; +Cc: linux-man


[-- 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 --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-08-30 23:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-30 23:30 [cppcheck]: [knownConditionTrueFalse]: false positive Alejandro Colomar

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).