================
@@ -0,0 +1,115 @@
+// RUN: %clang_analyze_cc1 %s -triple=x86_64-unknown-linux \
+// RUN:   -analyzer-output=text -verify \
+// RUN:   -analyzer-checker=core,security.UnsafeSymlinkTest
+
+struct stat {
+  int st_mode;
+  int st_ino;
+  int st_dev;
+};
+
+typedef int size_t;
+typedef size_t ssize_t;
+int lstat(const char *restrict path, struct stat *restrict buf);
+int open(const char *path, int oflag);
+ssize_t write(int fildes, const void *buf, size_t nbyte);
+int fstat(int fildes, struct stat *buf);
+
+#define S_ISLNK(M) ((M & 2) != 0)
+
+void test_fstat_single(const char *filename, const char *buf, size_t size) {
+  struct stat stat1;
+  int fd;
+
+  if (lstat(filename, &stat1) == -1) // expected-note{{File status is read 
here before opening the file}} \\
+                                     // expected-note{{Assuming the condition 
is false}} \\
+                                     // expected-note{{Taking false branch}}
+    return;
+
+  fd = open(filename, 1);
+  if (fd == -1) // expected-note{{Assuming the condition is false}} \\
+                // expected-note{{Taking false branch}}
+    return;
+
+  struct stat stat2;
+  if (fstat(fd, &stat2) == -1) // expected-note{{File status is read here 
after opening the file}} \\
+                               // expected-note{{Assuming the condition is 
false}} \\
+                               // expected-note{{Taking false branch}}
+    return;
+
+  write(fd, buf, size); // expected-warning{{Possibly missing check for 
external change of file}} \\
+                        // expected-note{{Possibly missing check for external 
change of file}} \\
+                        // expected-note{{File status was obtained before and 
after opening the file which indicates possible intent of a safe check for 
symbolic link}} \\
+                        // expected-note{{For a safe check the fields 
'st_mode', 'st_ino' and 'st_dev' before and after open should be checked for 
equality}}
----------------
balazske wrote:

I like this way better because no ``@-1`` characters are used. This can be only 
a problem for the preprocessor if the line length is too much but it seems to 
work.

https://github.com/llvm/llvm-project/pull/221184
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to