]> granicus.if.org Git - clang/commitdiff
[analyzer] UnixAPI: Check that the third argument to open(2) (if present) is an integer.
authorJordan Rose <jordan_rose@apple.com>
Wed, 20 Aug 2014 16:58:09 +0000 (16:58 +0000)
committerJordan Rose <jordan_rose@apple.com>
Wed, 20 Aug 2014 16:58:09 +0000 (16:58 +0000)
Patch by Daniel Fahlgren.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@216079 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
test/Analysis/unix-api.c

index e7626ca24b6a65b68732541b004a2304c3ca18ad..4bfed8504f985bd538090a0ed92d3aae23938dd6 100644 (file)
@@ -95,6 +95,15 @@ void UnixAPIChecker::CheckOpen(CheckerContext &C, const CallExpr *CE) const {
     // The frontend should issue a warning for this case, so this is a sanity
     // check.
     return;
+  } else if (CE->getNumArgs() == 3) {
+    const Expr *Arg = CE->getArg(2);
+    QualType QT = Arg->getType();
+    if (!QT->isIntegerType()) {
+      ReportOpenBug(C, state,
+                    "Third argument to 'open' is not an integer",
+                    Arg->getSourceRange());
+      return;
+    }
   } else if (CE->getNumArgs() > 3) {
     ReportOpenBug(C, state,
                   "Call to 'open' with more than three arguments",
index 4146822e21bc5c6bd84763da392a0c0316acf6c1..86c702d725942d30fd991aecfeb140c994b3697b 100644 (file)
@@ -25,3 +25,51 @@ void open_2(const char *path) {
   if (fd > -1)
     close(fd);
 }
+
+void open_3(const char *path) {
+  int fd;
+  fd = open(path, O_RDONLY, NULL); // expected-warning{{Third argument to 'open' is not an integer}}
+  if (fd > -1)
+    close(fd);
+}
+
+void open_4(const char *path) {
+  int fd;
+  fd = open(path, O_RDONLY, ""); // expected-warning{{Third argument to 'open' is not an integer}}
+  if (fd > -1)
+    close(fd);
+}
+
+void open_5(const char *path) {
+  int fd;
+  struct {
+    int val;
+  } st = {0};
+  fd = open(path, O_RDONLY, st); // expected-warning{{Third argument to 'open' is not an integer}}
+  if (fd > -1)
+    close(fd);
+}
+
+void open_6(const char *path) {
+  int fd;
+  struct {
+    int val;
+  } st = {0};
+  fd = open(path, O_RDONLY, st.val); // no-warning
+  if (fd > -1)
+    close(fd);
+}
+
+void open_7(const char *path) {
+  int fd;
+  fd = open(path, O_RDONLY, &open); // expected-warning{{Third argument to 'open' is not an integer}}
+  if (fd > -1)
+    close(fd);
+}
+
+void open_8(const char *path) {
+  int fd;
+  fd = open(path, O_RDONLY, 0.0f); // expected-warning{{Third argument to 'open' is not an integer}}
+  if (fd > -1)
+    close(fd);
+}