[cfe-commits] r106742 - in /cfe/trunk: lib/Checker/StreamChecker.cpp test/Analysis/stream.c

Zhongxing Xu xuzhongxing at gmail.com
Thu Jun 24 06:36:41 PDT 2010


Author: zhongxingxu
Date: Thu Jun 24 08:36:41 2010
New Revision: 106742

URL: http://llvm.org/viewvc/llvm-project?rev=106742&view=rev
Log:
Add check for illegal whence argument of fseek.

Modified:
    cfe/trunk/lib/Checker/StreamChecker.cpp
    cfe/trunk/test/Analysis/stream.c

Modified: cfe/trunk/lib/Checker/StreamChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/StreamChecker.cpp?rev=106742&r1=106741&r2=106742&view=diff
==============================================================================
--- cfe/trunk/lib/Checker/StreamChecker.cpp (original)
+++ cfe/trunk/lib/Checker/StreamChecker.cpp Thu Jun 24 08:36:41 2010
@@ -27,14 +27,14 @@
   IdentifierInfo *II_fopen, *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;
+  BuiltinBug *BT_nullfp, *BT_illegalwhence;
 
 public:
   StreamChecker() 
     : II_fopen(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_nullfp(0), BT_illegalwhence(0) {}
 
   static void *getTag() {
     static int x;
@@ -186,8 +186,33 @@
 
 void StreamChecker::Fseek(CheckerContext &C, const CallExpr *CE) {
   const GRState *state = C.getState();
-  if (!CheckNullStream(state->getSVal(CE->getArg(0)), state, C))
+  if (!(state = CheckNullStream(state->getSVal(CE->getArg(0)), state, C)))
+    return;
+  // Check the legality of the 'whence' argument of 'fseek'.
+  SVal Whence = state->getSVal(CE->getArg(2));
+  bool WhenceIsLegal = true;
+  const nonloc::ConcreteInt *CI = dyn_cast<nonloc::ConcreteInt>(&Whence);
+  if (!CI)
+    WhenceIsLegal = false;
+
+  int64_t x = CI->getValue().getSExtValue();
+  if (!(x == 0 || x == 1 || x == 2))
+    WhenceIsLegal = false;
+
+  if (!WhenceIsLegal) {
+    if (ExplodedNode *N = C.GenerateSink(state)) {
+      if (!BT_illegalwhence)
+        BT_illegalwhence = new BuiltinBug("Illegal whence argument",
+                                     "The whence argument to fseek() should be "
+                                          "SEEK_SET, SEEK_END, or SEEK_CUR.");
+      BugReport *R = new BugReport(*BT_illegalwhence, 
+                                   BT_illegalwhence->getDescription(), N);
+      C.EmitReport(R);
+    }
     return;
+  }
+
+  C.addTransition(state);
 }
 
 void StreamChecker::Ftell(CheckerContext &C, const CallExpr *CE) {

Modified: cfe/trunk/test/Analysis/stream.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/stream.c?rev=106742&r1=106741&r2=106742&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/stream.c (original)
+++ cfe/trunk/test/Analysis/stream.c Thu Jun 24 08:36:41 2010
@@ -32,3 +32,10 @@
   rewind(p); // expected-warning {{Stream pointer might be NULL.}}
 }
 
+void f5(void) {
+  FILE *p = fopen("foo", "r");
+  if (!p)
+    return;
+  fseek(p, 1, SEEK_SET); // no-warning
+  fseek(p, 1, 3); // expected-warning {{The whence argument to fseek() should be SEEK_SET, SEEK_END, or SEEK_CUR.}}
+}





More information about the cfe-commits mailing list