llvm / llvm/llvm-project

Clang 22 Static Analyzer misreports case of undefined errno in branch conditions

Open
#204,307 6 comments 0 reactions 0 assignees View on GitHub
clang:static analyzer false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Version: clang version 22.1.6 with GNU libstdc++ 15.3.0 / openSUSE Tumbleweed x86_64 20260615

Input:

```c++
#include
#include
#include
#include

struct del {
void operator()(DIR *d) const { closedir(d); }
};
struct D {
std::string foo;
std::unique_ptr bar;
};

void test()
{
D d;
d.foo = "foo";
errno = 0;
d.bar.reset(opendir("."));
if (d.bar != nullptr)
return;
if (errno != ENOENT)
;
}
```

Observed output:

```
>clang++ 1.cpp -Wall --analyze -c
clang++: warning: argument unused during compilation: '-c' [-Wunused-command-line-argument]
warning: Path diagnostic report is not generated. Current output format does not support diagnostics that cross file boundaries. Refer to --analyzer-output for valid output formats
1.cpp:22:6: warning: An undefined value may be read from 'errno' [unix.Errno]
22 | if (errno != ENOENT)
| ^~~~~
/usr/include/errno.h:38:16: note: expanded from macro 'errno'
38 | # define errno (*__errno_location ())
| ^~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
```

Expected output:

Stay silent. I do not see how errno could be undefined at that point.

Additional information:

Removing the line `d.foo = "foo";` line also makes clang go silent despite being unrelated to the opendir call.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.