-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
Description of the false positive
Hello!
Recently in systemd (systemd/systemd#22711) we noticed several warnings to pop-up after moving from -std=gnu99
to -std=gnu11
. After closer inspection it looks like the cpp/missing-return
query doesn't properly handle functions explicitly marked as no return using C11 macros (_Noreturn
and noreturn
).
I prepped an example in mrc0mmand/codeql-test#1 but will include the relevant code here as well.
#include <stdio.h>
#include <stdnoreturn.h>
_Noreturn void assert_no_return_c11(void) {
puts("c11");
}
noreturn void assert_no_return_c11_std(void) {
puts("c11 std");
}
__attribute__((__noreturn__)) void assert_no_return_c99(void) {
puts("c99");
}
static int foo_c99(int x) {
if (x == 42)
return 0;
assert_no_return_c99();
}
static int bar_c11(int x) {
if (x == 42)
return 0;
assert_no_return_c11();
}
static int baz_c11_std(int x) {
if (x == 42)
return 0;
assert_no_return_c11_std();
}
int main(void) {
foo_c99(42);
bar_c11(42);
baz_c11_std(42);
return 0;
}
In the code above, the assert_no_return_c99()
function is the only one that's ignored by CodeQL/LGTM, but in practice all three of them should be ignored, as they're explicitly marked as no return functions.
I can reproduce this both in LGTM (see the link below) or manually using the latest CodeQL CLI:
$ codeql database analyze codeql -o results.csv --format=csv -vvv test.qls
$ cat results.csv
"Missing return statement","All functions that are not void should return a value on every exit path.","error","Function baz_c11_std should return a value of type int but does not return a value here","/codeql-test.c","34","5","34","31"
"Missing return statement","All functions that are not void should return a value on every exit path.","error","Function bar_c11 should return a value of type int but does not return a value here","/codeql-test.c","27","5","27","27"
URL to the alert on the project page on LGTM.com
https://lgtm.com/projects/g/mrc0mmand/codeql-test/snapshot/bc159df6762ccc5d0da28cda09dafb6884439eed/files/codeql-test.c?sort=name&dir=ASC&mode=heatmap#xc61c6be402c3286b:1