A disagreement on one file#
Two path-sensitive analyzers can look at the same file and reach opposite answers without either one being broken. They choose different entry points, apply different suppression heuristics and ship different checkers. Before deciding which result to believe, find out which of those differences produced the split.
This constructed file maps a status code to a label and measures it:
#include <stdio.h>
#include <string.h>
const char *label(int code)
{
const char *s = NULL;
if (code == 1)
s = "one";
else if (code == 2)
s = "two";
return s;
}
size_t label_len(int code)
{
return strlen(label(code)); // line 16
}
int main(void)
{
printf("%zu\n", label_len(1) + label_len(2));
return 0;
}
GCC 13.3.0 (gcc -fanalyzer -fdiagnostics-plain-output -c label.c) reports a null argument to strlen, starting its path at label_len:
label.c: In function 'label_len':
label.c:16:10: warning: use of NULL where non-null expected [CWE-476] [-Wanalyzer-null-argument]
label.c:14:8: note: (1) entry to 'label_len'
label.c:16:10: note: (2) calling 'label' from 'label_len'
label.c:4:13: note: (3) entry to 'label'
label.c:6:15: note: (4) 's' is NULL
label.c:7:6: note: (5) following 'false' branch (when 'code != 1')...
label.c:9:11: note: (6) ...to here
label.c:9:11: note: (7) following 'false' branch (when 'code != 2')...
label.c:11:10: note: (8) ...to here
label.c:11:10: note: (9) 's' is NULL
label.c:16:10: note: (10) returning to 'label_len' from 'label'
label.c:16:10: note: (11) argument 1 ('label(code)') NULL where non-null expected
...
Clang 18.1.3 (clang --analyze label.c) prints nothing and exits 0.
Read each tool's model#
The first difference is where each tool starts. Clang's -analyzer-display-progress shows that it ran path-sensitive analysis on main alone:
ANALYZE (Syntax): label.c label : 0.1 ms
ANALYZE (Syntax): label.c label_len : 0.0 ms
ANALYZE (Syntax): label.c main : 0.0 ms
ANALYZE (Path, Inline_Regular): label.c main : 1.3 ms
That is the documented order. Clang's AnalysisConsumer.cpp at llvmorg-18.1.3 walks the call graph "in topological order, so that we analyze parents before the children. Skip the functions inlined into the previously processed functions." label_len was inlined into main with the arguments 1 and 2, so Clang only ever saw codes that have labels, and it never analyzed label_len with an arbitrary code.
GCC starts from every function. In engine.cc for 13.3.0, the analyzer loops over each function with a body and asks toplevel_function_p, whose comment reads "Return true if FUN should be traversed directly, rather than only as called via other functions." It returns false only for names with an __analyzer_ prefix. So GCC explored label_len with an unknown code, which is how its path reaches code != 1 and code != 2.
The second difference is a Clang heuristic. Run on a copy of the file without main, so that label_len becomes Clang's entry point, Clang still reports nothing. Its AnalyzerOptions.def explains why: suppress-null-return-paths, on by default, hides reports whose paths go "through null returns", described as "a heuristic for avoiding bug reports with paths that go through inlined functions that are more defensive than their callers." Turning it off brings back the same path GCC found (source excerpts under each note omitted):
$ clang --analyze -Xclang -analyzer-config -Xclang suppress-null-return-paths=false \
-Xclang -analyzer-output=text label_lib.c
label_lib.c:16:10: warning: Null pointer passed to 1st parameter expecting 'nonnull' [core.NonNullParamChecker]
label_lib.c:16:17: note: Calling 'label'
label_lib.c:6:3: note: 's' initialized to a null pointer value
label_lib.c:7:7: note: Assuming 'code' is not equal to 1
label_lib.c:7:3: note: Taking false branch
label_lib.c:9:12: note: Assuming 'code' is not equal to 2
label_lib.c:9:8: note: Taking false branch
label_lib.c:11:3: note: Returning null pointer (loaded from 's')
label_lib.c:16:17: note: Returning from 'label'
label_lib.c:16:10: note: Null pointer passed to 1st parameter expecting 'nonnull'
Both tools now describe one path, and both treat strlen(NULL) as a bug. What remains open is whether any caller passes a code other than 1 or 2.
Settle it with the callers and a runtime check#
label_len has external linkage. nm label.o lists it as T label_len, a global symbol, so code in other files can call it with any integer. A short test in a separate file does exactly that:
size_t label_len(int code);
int main(void) { printf("label_len(3) = %zu\n", label_len(3)); return 0; }
Built with -fsanitize=address against label.c (its main renamed), it stops in strlen:
==...==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x... bp 0x... sp 0x... T0)
==...==The signal is caused by a READ memory access.
==...==Hint: address points to the zero page.
#0 0x... in __strlen_evex ../sysdeps/x86_64/multiarch/strlen-evex-base.S:81
#1 0x... in strlen ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:389
#2 0x... in label_len label.c:16
GCC is right for this file. Clang's silence reflected the one caller it saw and a heuristic that assumes label is more defensive than its callers. Fix label to return a placeholder or make label_len handle NULL.
When the quiet tool's model is the true one#
The disconfirmation is the same file with both helpers declared static. GCC gives the identical warning, again starting at entry to 'label_len', and Clang is again silent. Now nm lists t label and t label_len, local symbols that no other file can call. The only callers in the program are the two in main, with codes 1 and 2, and the built program prints 6 and exits 0 under AddressSanitizer.
In this version Clang's model, which follows the real callers, matches the program, and GCC's model, which explores the function with any argument, describes a call that cannot happen. Dismissing the GCC finding is defensible, with the reason "static function; every caller passes 1 or 2." Because the bug returns the moment someone adds a caller or removes static, a one-line fix in label is often cheaper than the dismissal.
Questions that explain most splits#
- Entry points: did one tool analyze the function only through its callers? Clang's
-analyzer-display-progressand GCC's path start (entry to ...) answer this. - Heuristics: is the quiet tool suppressing a category of path on purpose? Check its documented options before concluding it proved anything.
- Checkers: does the quiet tool have a checker for this class at all? Clang's checker list states that its alpha checkers "are under development and are switched off by default", and GCC's invoke.texi lists the warnings
-fanalyzerenables. Silence from a tool with no checker for the bug class is not a disagreement. - Feasibility: does the reporting tool's path take two branches that cannot both hold? Read the branch notes against the code.
Decide from the model that matches the callers#
| What the tools say | Evidence to collect | Decision |
|---|---|---|
| One reports, the other is silent, and the silent one analyzed only from callers | Linkage (nm), every caller's arguments, and a test that calls the function with the reported values | Fix if any caller, current or external, can pass them |
Same, but the function is static and every caller passes safe values | The list of callers and a clean sanitizer run of the program | Dismiss with the caller list as the reason, or make the cheap fix |
| Silent tool has a suppression heuristic for this path shape | The same run with the heuristic disabled | Treat the silence as a suppressed report |
| Silent tool has no checker for the bug class | The tool's checker list | Ignore the silence; judge the report on its own path |
| Both report the same bug | One runtime reproduction | Fix it |
For reading the reporting tool's path in SARIF form, see the guide to SARIF result fields. For warnings that appear only under some optimization levels, see the guide to optimization-dependent compiler warnings.