This is an archive of the discontinued LLVM Phabricator instance.

[analyzer][StdLibraryFunctionsChecker] Make close and mmap to accept -1 as fd
ClosedPublic

Authored by martong on Dec 7 2020, 7:17 AM.

Details

Summary

close:
It is quite often that users chose to call close even if the fd is
negative. Theoretically, it would be nicer to close only valid fds, but
in practice the implementations of close just returns with EBADF in case
of a non-valid fd param. So, we can eliminate many false positives if we
let close to take -1 as an fd. Other negative values are very unlikely,
because open and other fd factories return with -1 in case of failure.

mmap:
In the case of MAP_ANONYMOUS flag (which is supported e.g. in Linux) the
mapping is not backed by any file; its contents are initialized to zero.
The fd argument is ignored; however, some implementations require fd to
be -1 if MAP_ANONYMOUS (or MAP_ANON) is specified, and portable
applications should ensure this.
Consequently, we must allow -1 as the 4th arg.

Diff Detail

Event Timeline

martong created this revision.Dec 7 2020, 7:17 AM
martong requested review of this revision.Dec 7 2020, 7:17 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 7 2020, 7:17 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

LGTM besides my inline comment.

clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
1737

mmap should have the same conditions, am I right?

martong updated this revision to Diff 309931.Dec 7 2020, 8:41 AM
martong marked an inline comment as done.
  • Use -1 for mmap too
clang/lib/StaticAnalyzer/Checkers/StdLibraryFunctionsChecker.cpp
1737

Yep, thanks!

steakhal accepted this revision.Dec 8 2020, 3:27 AM

Thank you.

This revision is now accepted and ready to land.Dec 8 2020, 3:27 AM
This revision was landed with ongoing or failed builds.Dec 8 2020, 8:01 AM
This revision was automatically updated to reflect the committed changes.