This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] non-obvious analyzer warning: Use of zero-allocated memory
ClosedPublic

Authored by chrisdangelo on Oct 12 2021, 9:16 AM.

Details

Summary

This change clarifies the message provided when the analyzer catches the use of memory that is allocated with size zero.

Diff Detail

Event Timeline

chrisdangelo created this revision.Oct 12 2021, 9:16 AM
chrisdangelo requested review of this revision.Oct 12 2021, 9:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 12 2021, 9:16 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
NoQ accepted this revision.Oct 12 2021, 10:08 AM

LGTM! Can confirm, people were confused.

I'll commit.

This revision is now accepted and ready to land.Oct 12 2021, 10:08 AM
Quuxplusone added inline comments.
clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
2474–2475

Peanut gallery says:
(1) Might want to fix the typo in "Zerro" at the same time, or in a followup commit.
(2) I would naively have expected all the test cases below to give something simple like "Read/write beyond end of allocated space," because they allocate N bytes and then try to read/write into byte number N. This is never allowed in C or C++, regardless of the value of N; there's nothing special about N=0. So I don't see why it needs any special diagnostic (confusing or otherwise).

NoQ added a comment.Oct 12 2021, 11:29 AM

Yuck I forgot to forge commit author. Sorry!!

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
2474–2475

The only thing special about N=0 is that the static analyzer is currently better at catching it. It's much easier to catch correctly and avoid false positives when *any* use is disallowed than when some uses are allowed but some aren't.

Speaking of typos, it probably also makes sense to fix the bug type message ("Use of zero allocated") to include a subject.