Skip to content

Commit 32521bb

Browse files
authored
[clang][analyzer] Restrict 'fopen' & 'tmpfile' modeling to POSIX versions in StreamChecker (#70540)
'tmpfile' has only one form that it has no argument.
1 parent e2564b2 commit 32521bb

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

clang/lib/StaticAnalyzer/Checkers/StreamChecker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ class StreamChecker : public Checker<check::PreCall, eval::Call,
238238

239239
private:
240240
CallDescriptionMap<FnDescription> FnDescriptions = {
241-
{{{"fopen"}}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
241+
{{{"fopen"}, 2}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
242242
{{{"freopen"}, 3},
243243
{&StreamChecker::preFreopen, &StreamChecker::evalFreopen, 2}},
244-
{{{"tmpfile"}}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
244+
{{{"tmpfile"}, 0}, {nullptr, &StreamChecker::evalFopen, ArgNone}},
245245
{{{"fclose"}, 1},
246246
{&StreamChecker::preDefault, &StreamChecker::evalFclose, 0}},
247247
{{{"fread"}, 4},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_analyze_cc1 -fno-builtin -analyzer-checker=core,alpha.unix.Stream -verify %s
2+
// expected-no-diagnostics
3+
4+
typedef struct _FILE FILE;
5+
6+
// These functions are not standard C library functions.
7+
FILE *tmpfile(const char *restrict path); // Real 'tmpfile' should have exactly 0 formal parameters.
8+
FILE *fopen(const char *restrict path); // Real 'fopen' should have exactly 2 formal parameters.
9+
10+
void test_fopen_non_posix(void) {
11+
FILE *fp = fopen("file"); // no-leak: This isn't the standard POSIX `fopen`, we don't know the semantics of this call.
12+
}
13+
14+
void test_tmpfile_non_posix(void) {
15+
FILE *fp = tmpfile("file"); // // no-leak: This isn't the standard POSIX `tmpfile`, we don't know the semantics of this call.
16+
}

0 commit comments

Comments
 (0)