[PATCH] D56136: [compiler-rt] [sanitizer_common] Add tests for more stdio.h functions

Michał Górny via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 28 12:22:11 PST 2018


mgorny created this revision.
mgorny added reviewers: krytarowski, vitalybuka.
Herald added subscribers: Sanitizers, llvm-commits, dberris, kubamracek.

Add two new test cases that test the following stdio.h functions:

- clearerr()
- feof()
- ferror()
- fileno()
- fgetc()
- getc()
- ungetc()


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D56136

Files:
  test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
  test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc


Index: test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc
@@ -0,0 +1,23 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  FILE *fp;
+
+  fp = fopen(argv[0], "r");
+  if (!fp)
+    return 1;
+
+  if (fgetc(fp) == EOF)
+    return 2;
+
+  if (ungetc('X', fp) == EOF)
+    return 3;
+
+  if (getc(fp) != 'X')
+    return 4;
+
+  fclose(fp);
+  return 0;
+}
Index: test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
===================================================================
--- /dev/null
+++ test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
@@ -0,0 +1,51 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+  FILE *fp;
+  int fd;
+  char buf[BUFSIZ];
+
+  fp = fopen(argv[0], "r");
+  if (!fp)
+    return 1;
+
+  // file should be good upon opening
+  if (feof(fp) || ferror(fp))
+    return 2;
+
+  // read until EOF
+  while (fread(buf, 1, sizeof buf, fp) != 0) {}
+  if (!feof(fp))
+    return 3;
+
+  // clear EOF
+  clearerr(fp);
+  if (feof(fp) || ferror(fp))
+    return 4;
+
+  // get file descriptor
+  fd = fileno(fp);
+  if (fd == -1)
+    return 5;
+
+  // break the file by closing underlying descriptor
+  if (close(fd) == -1)
+    return 6;
+
+  // verify that an error is signalled
+  if (fread(buf, 1, sizeof buf, fp) != 0)
+    return 7;
+  if (!ferror(fp))
+    return 8;
+
+  // clear error
+  clearerr(fp);
+  if (feof(fp) || ferror(fp))
+    return 9;
+
+  fclose(fp);
+  return 0;
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D56136.179655.patch
Type: text/x-patch
Size: 1716 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20181228/364fc23b/attachment.bin>


More information about the cfe-commits mailing list