Skip to content

Commit 909c963

Browse files
committed
[analyzer] Fix stdin declaration in C++ tests (#66074)
The `stdin` declaration should be within `extern "C" {...}`, in C++ mode. In addition, it should be also marked `extern` in both C and C++ modes. I tightened the check to ensure we only accept `stdin` if both of these match. However, from the Juliet test suite's perspective, this commit should not matter. #66074
1 parent 0b2778d commit 909c963

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ bool isStdin(SVal Val, const ASTContext &ACtx) {
104104
// variable named stdin with the proper type.
105105
if (const auto *D = dyn_cast_or_null<VarDecl>(DeclReg->getDecl())) {
106106
D = D->getCanonicalDecl();
107-
// FIXME: This should look for an exact match.
108-
if (D->getName().contains("stdin") && D->isExternC()) {
107+
if (D->getName() == "stdin" && D->hasExternalStorage() && D->isExternC()) {
109108
const QualType FILETy = ACtx.getFILEType().getCanonicalType();
110109
const QualType Ty = D->getType().getCanonicalType();
111110

clang/test/Analysis/taint-diagnostic-visitor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ size_t strlen( const char* str );
1313
void *malloc(size_t size );
1414
void free( void *ptr );
1515
char *fgets(char *str, int n, FILE *stream);
16-
FILE *stdin;
16+
extern FILE *stdin;
1717

1818
void taintDiagnostic(void)
1919
{

clang/test/Analysis/taint-generic.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ int scanf(const char*, ...);
77
int mySource1();
88
int mySource3();
99

10+
typedef struct _FILE FILE;
11+
extern "C" {
12+
extern FILE *stdin;
13+
}
14+
int fscanf(FILE *stream, const char *format, ...);
15+
1016
bool isOutOfRange2(const int*);
1117

1218
void mySink2(int);
@@ -124,3 +130,9 @@ void testConfigurationMemberFunc() {
124130
foo.myMemberScanf("%d", &x);
125131
Buffer[x] = 1; // expected-warning {{Out of bound memory access }}
126132
}
133+
134+
void testReadingFromStdin(char **p) {
135+
int n;
136+
fscanf(stdin, "%d", &n);
137+
Buffer[n] = 1; // expected-warning {{Out of bound memory access (index is tainted)}}
138+
}

0 commit comments

Comments
 (0)