From 47dc37f1efa6942366dd61c4acb0c874049dd1e0 Mon Sep 17 00:00:00 2001 From: Zhongxing Xu Date: Thu, 22 Jul 2010 14:01:01 +0000 Subject: [PATCH] This patch adds support for tmpfile in StreamChecker. Patch by Lei Zhang. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@109106 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Checker/StreamChecker.cpp | 32 +++++++++++++++++++++++++------- test/Analysis/stream.c | 6 ++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/Checker/StreamChecker.cpp b/lib/Checker/StreamChecker.cpp index 3945ed095d..cfb94344f6 100644 --- a/lib/Checker/StreamChecker.cpp +++ b/lib/Checker/StreamChecker.cpp @@ -39,7 +39,9 @@ struct StreamState { static StreamState getOpened(const Stmt *s) { return StreamState(Opened, s); } static StreamState getClosed(const Stmt *s) { return StreamState(Closed, s); } - static StreamState getOpenFailed(const Stmt *s) { return StreamState(OpenFailed, s); } + static StreamState getOpenFailed(const Stmt *s) { + return StreamState(OpenFailed, s); + } void Profile(llvm::FoldingSetNodeID &ID) const { ID.AddInteger(K); @@ -48,14 +50,14 @@ struct StreamState { }; class StreamChecker : public CheckerVisitor { - IdentifierInfo *II_fopen, *II_fclose,*II_fread, *II_fwrite, + IdentifierInfo *II_fopen, *II_tmpfile, *II_fclose, *II_fread, *II_fwrite, *II_fseek, *II_ftell, *II_rewind, *II_fgetpos, *II_fsetpos, *II_clearerr, *II_feof, *II_ferror, *II_fileno; BuiltinBug *BT_nullfp, *BT_illegalwhence, *BT_doubleclose; public: StreamChecker() - : II_fopen(0), II_fclose(0), II_fread(0), II_fwrite(0), + : II_fopen(0), II_tmpfile(0) ,II_fclose(0), II_fread(0), II_fwrite(0), II_fseek(0), II_ftell(0), II_rewind(0), II_fgetpos(0), II_fsetpos(0), II_clearerr(0), II_feof(0), II_ferror(0), II_fileno(0), BT_nullfp(0), BT_illegalwhence(0), BT_doubleclose(0) {} @@ -69,6 +71,7 @@ public: private: void Fopen(CheckerContext &C, const CallExpr *CE); + void Tmpfile(CheckerContext &C, const CallExpr *CE); void Fclose(CheckerContext &C, const CallExpr *CE); void Fread(CheckerContext &C, const CallExpr *CE); void Fwrite(CheckerContext &C, const CallExpr *CE); @@ -81,8 +84,9 @@ private: void Feof(CheckerContext &C, const CallExpr *CE); void Ferror(CheckerContext &C, const CallExpr *CE); void Fileno(CheckerContext &C, const CallExpr *CE); + + void OpenFileAux(CheckerContext &C, const CallExpr *CE); - // Return true indicates the stream pointer is NULL. const GRState *CheckNullStream(SVal SV, const GRState *state, CheckerContext &C); const GRState *CheckDoubleClose(const CallExpr *CE, const GRState *state, @@ -114,6 +118,8 @@ bool StreamChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { ASTContext &Ctx = C.getASTContext(); if (!II_fopen) II_fopen = &Ctx.Idents.get("fopen"); + if (!II_tmpfile) + II_tmpfile = &Ctx.Idents.get("tmpfile"); if (!II_fclose) II_fclose = &Ctx.Idents.get("fclose"); if (!II_fread) @@ -143,6 +149,10 @@ bool StreamChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { Fopen(C, CE); return true; } + if (FD->getIdentifier() == II_tmpfile) { + Tmpfile(C, CE); + return true; + } if (FD->getIdentifier() == II_fclose) { Fclose(C, CE); return true; @@ -196,24 +206,32 @@ bool StreamChecker::EvalCallExpr(CheckerContext &C, const CallExpr *CE) { } void StreamChecker::Fopen(CheckerContext &C, const CallExpr *CE) { + OpenFileAux(C, CE); +} + +void StreamChecker::Tmpfile(CheckerContext &C, const CallExpr *CE) { + OpenFileAux(C, CE); +} + +void StreamChecker::OpenFileAux(CheckerContext &C, const CallExpr *CE) { const GRState *state = C.getState(); unsigned Count = C.getNodeBuilder().getCurrentBlockCount(); ValueManager &ValMgr = C.getValueManager(); DefinedSVal RetVal = cast(ValMgr.getConjuredSymbolVal(0, CE, Count)); state = state->BindExpr(CE, RetVal); - + ConstraintManager &CM = C.getConstraintManager(); // Bifurcate the state into two: one with a valid FILE* pointer, the other // with a NULL. const GRState *stateNotNull, *stateNull; llvm::tie(stateNotNull, stateNull) = CM.AssumeDual(state, RetVal); - + SymbolRef Sym = RetVal.getAsSymbol(); assert(Sym); // if RetVal is not NULL, set the symbol's state to Opened. - stateNotNull = stateNotNull->set(Sym, StreamState::getOpened(CE)); + stateNotNull = stateNotNull->set(Sym,StreamState::getOpened(CE)); stateNull = stateNull->set(Sym, StreamState::getOpenFailed(CE)); C.addTransition(stateNotNull); diff --git a/test/Analysis/stream.c b/test/Analysis/stream.c index bc1b58ee04..587795ea94 100644 --- a/test/Analysis/stream.c +++ b/test/Analysis/stream.c @@ -6,6 +6,7 @@ typedef struct _IO_FILE FILE; #define SEEK_CUR 1 /* Seek from current position. */ #define SEEK_END 2 /* Seek from end of file. */ extern FILE *fopen(const char *path, const char *mode); +extern FILE *tmpfile(void); extern int fclose(FILE *fp); extern size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); extern int fseek (FILE *__stream, long int __off, int __whence); @@ -46,3 +47,8 @@ void f6(void) { fclose(p); fclose(p); // expected-warning {{Try to close a file Descriptor already closed. Cause undefined behaviour.}} } + +void f7(void) { + FILE *p = tmpfile(); + ftell(p); // expected-warning {{Stream pointer might be NULL.}} +} -- 2.40.0