Skip to contentSkip to conversation

: [knownConditionTrueFalse]: false positive

cppcheck Alejandro Colomar 1 participant 1 message Aug 30, 2023 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore

Alejandro Colomar

author maintainer
#0 lore raw

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

Signature · 2 lines
<http://www.alejandro-colomar.es/>
GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5