[clang] [clang][analyzer] fix false positive of BlockInCriticalSectionChecker (PR #126752)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 12 04:28:40 PST 2025
================
@@ -339,6 +425,36 @@ void BlockInCriticalSectionChecker::reportBlockInCritSection(
os.str(), ErrNode);
R->addRange(Call.getSourceRange());
R->markInteresting(Call.getReturnValue());
+ // for 'read' call, check whether it's file descriptor(first argument) is
+ // created by 'open' API with O_NONBLOCK flag and don't report for this
+ // situation.
+ if (Call.getCalleeIdentifier()->getName() == "read") {
+ do {
+ const auto *arg = Call.getArgExpr(0);
+ if (!arg)
+ break;
+
+ const auto *DRE = dyn_cast<DeclRefExpr>(arg->IgnoreImpCasts());
----------------
flovent wrote:
thank you for your detailed explanation, I did overlook some complex scenarios.
And I agree with your point, Checking for open's return value before calling `read` is very necessary in a real project, this is a modified testcase with checking for file descirptor,in that case we can ignore concrete integers situation.
```
void foo()
{
std::lock_guard<std::mutex> lock(mtx);
const char *filename = "example.txt";
int fd = open(filename, O_RDONLY | O_NONBLOCK);
char buffer[200] = {};
if (fd == -1)
return;
read(fd, buffer, 199); // no-warning: fd is a non-block file descriptor
close(fd);
}
```
https://github.com/llvm/llvm-project/pull/126752
More information about the cfe-commits
mailing list