Skip to content

Commit

Permalink
[clang][analyzer] Restrict 'fopen' & 'tmpfile' modeling to POSIX vers…
Browse files Browse the repository at this point in the history
…ions in StreamChecker (#70540)

'tmpfile' has only one form that it has no argument.
  • Loading branch information
benshi001 committed Nov 2, 2023
1 parent e2564b2 commit 32521bb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,10 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,

private:
CallDescriptionMap<FnDescription> FnDescriptions = {
{{{"fopen"}}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
{{{"fopen"}, 2}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
{{{"freopen"}, 3},
{&StreamChecker::preFreopen, &StreamChecker::evalFreopen, 2}},
{{{"tmpfile"}}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
{{{"tmpfile"}, 0}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
{{{"fclose"}, 1},
{&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
{{{"fread"}, 4},
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Analysis/stream-non-posix-function.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_analyze_cc1 -fno-builtin -analyzer-checker=core,alpha.unix.Stream -verify %s
// expected-no-diagnostics

typedef struct _FILE FILE;

// These functions are not standard C library functions.
FILE *tmpfile(const char *restrict path); // Real 'tmpfile' should have exactly 0 formal parameters.
FILE *fopen(const char *restrict path); // Real 'fopen' should have exactly 2 formal parameters.

void test_fopen_non_posix(void) {
FILE *fp = fopen("file"); // no-leak: This isn't the standard POSIX `fopen`, we don't know the semantics of this call.
}

void test_tmpfile_non_posix(void) {
FILE *fp = tmpfile("file"); // // no-leak: This isn't the standard POSIX `tmpfile`, we don't know the semantics of this call.
}

0 comments on commit 32521bb

Please sign in to comment.