<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/88356>88356</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
False Positive - null dereference in clang-tidy
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mushenoy
</td>
</tr>
</table>
<pre>
A False positive in clang-tidy for below sample program.
**sample.c**
```
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
double (*cosine)(double) = NULL;
extern void test (void);
int
main(int argc, char **argv)
{
void *handle;
handle = dlopen("libtest.so", RTLD_LAZY);
if (!handle) {
exit(EXIT_FAILURE);
}
cosine = dlsym(handle, "cos");
if (cosine) {
test();
printf("%f\n", cosine (2.0));
}
dlclose(handle);
exit(EXIT_SUCCESS);
}
```
**Warnings**:
```
clang-tidy --checks="clang-analyzer-*" /tmp/sample.c
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "/tmp/sample.c"
No compilation database found in /tmp or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 warning generated.
/tmp/sample.c:21:23: warning: Called function pointer is null (null dereference) [clang-analyzer-core.CallAndMessage]
21 | printf("%f\n", cosine (2.0));
| ^~~~~~
/tmp/sample.c:14:9: note: Assuming 'handle' is non-null
14 | if (!handle) {
| ^~~~~~~
/tmp/sample.c:14:5: note: Taking false branch
14 | if (!handle) {
| ^
/tmp/sample.c:19:8: note: Assuming 'cosine' is non-null
19 | if (cosine) {
| ^~~~~~
/tmp/sample.c:19:4: note: Taking true branch
19 | if (cosine) {
| ^
/tmp/sample.c:20:8: note: Null pointer value stored to 'cosine'
20 | test();
| ^~~~~~
/tmp/sample.c:21:23: note: Called function pointer is null (null dereference)
21 | printf("%f\n", cosine (2.0));
| ^~~~~~
```
**Version**
clang-tidy --version
LLVM (http://llvm.org/):
LLVM version 19.0.0
**Observations:**
- Warnings are not reported if the cosine function ptr is made as a local variable
- Warnings are not reported if the test function definition is provided
- Warnings are not reported if the test() function call is removed
- Warnings are not reported if the “= NULL” is removed in line 5.
- Warnings are still reported even if the function ptr is made as static variable
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEV02T2roS_TVi02XKyHiABQvCR1VekcmrTJL3sUnJUhsrERIlyUy4i_vbb0m2wcMMSeZmcV2UbCP16dNH3ZLMnJM7jTgn-RuSrwas9pWx833tKtTmNCiMOM0XsGHKIRyMk14eEaQGrpjeJV6KE5TGQoHKPIJj-4NCOFizs2w_JOmKpIu2peHXDBjy5u1J_13a_trhmdRc1QKBZEvnhTTDimTrG71KFje7hSq57vfGVpi6UAiETgldcOOkRkJnhE6bDkJnQLIV3H_abkn2prHB7x6thqORAjw6H6zDSzDsxjSt1L552DOpCZ1K7YHZHSd0CbxiFhoBmN0dg3FjOWkhAKDxQeiiYloovEIPA5qOyFEoc0AdI6FKFoHZ0BlCafD24eN29WW7-P__-iQDgCyb4EetixBwn0G48Lv0hE7X_3378ctm8Xb76cP6GoZMVlfUGjFbZu60J3TauVgCoZQbF7k9wWnYnOfhGZUQVKT71DsAHKzUvmyiJzQvSb7UbewdEzqlwzTO7g3u4UUorozDHtung_taPHxaLtcPD0_m_azDVSL3sv8_zGqpd67N_uzl1O9VVpLwCvk3R7JVEC52MM3U6Q-0SUShQOjG7w-Ebs61FVHW1hoLj5VUCN6epN6BN6AME8CAm_1BKual0SCYZwVzeKazNLUSoI0HVnuTCPTI_YsmsfJLGcuIPudBaQN4b25Z11qEtaQxBWOB6RMcmEXtQUiL3Bt7akBK-R1F0sNJesShH20ohhButOiHB_cGXM2rhrKx1y6-OqNf5eFfD-_vX-PgQ62j3aP0lak9lIrtXLtKjuCxyQ7YoUbLPIpu_XwmbLago9BkwWdrFh6XTCkUUNaaR6UPRmqPFqQDXSsVCiHeBVos0aLmTa3lb65SixuLw4C20OIdOs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