Skip to content

Commit

Permalink
[analyzer] Fix stdin declaration in C++ tests (#66074)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
steakhal committed Sep 14, 2023
1 parent 0b2778d commit 909c963
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions clang/lib/StaticAnalyzer/Checkers/GenericTaintChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ bool isStdin(SVal Val, const ASTContext &ACtx) {
// variable named stdin with the proper type.
if (const auto *D = dyn_cast_or_null<VarDecl>(DeclReg->getDecl())) {
D = D->getCanonicalDecl();
// FIXME: This should look for an exact match.
if (D->getName().contains("stdin") && D->isExternC()) {
if (D->getName() == "stdin" && D->hasExternalStorage() && D->isExternC()) {
const QualType FILETy = ACtx.getFILEType().getCanonicalType();
const QualType Ty = D->getType().getCanonicalType();

Expand Down
2 changes: 1 addition & 1 deletion clang/test/Analysis/taint-diagnostic-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ size_t strlen( const char* str );
void *malloc(size_t size );
void free( void *ptr );
char *fgets(char *str, int n, FILE *stream);
FILE *stdin;
extern FILE *stdin;

void taintDiagnostic(void)
{
Expand Down
12 changes: 12 additions & 0 deletions clang/test/Analysis/taint-generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ int scanf(const char*, ...);
int mySource1();
int mySource3();

typedef struct _FILE FILE;
extern "C" {
extern FILE *stdin;
}
int fscanf(FILE *stream, const char *format, ...);

bool isOutOfRange2(const int*);

void mySink2(int);
Expand Down Expand Up @@ -124,3 +130,9 @@ void testConfigurationMemberFunc() {
foo.myMemberScanf("%d", &x);
Buffer[x] = 1; // expected-warning {{Out of bound memory access }}
}

void testReadingFromStdin(char **p) {
int n;
fscanf(stdin, "%d", &n);
Buffer[n] = 1; // expected-warning {{Out of bound memory access (index is tainted)}}
}

0 comments on commit 909c963

Please sign in to comment.