[compiler-rt] r350225 - [sanitizer_common] Add tests for more stdio.h functions

Michal Gorny via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 2 09:36:37 PST 2019


Author: mgorny
Date: Wed Jan  2 09:36:37 2019
New Revision: 350225

URL: http://llvm.org/viewvc/llvm-project?rev=350225&view=rev
Log:
[sanitizer_common] Add tests for more stdio.h functions

Add two new test cases that test the following stdio.h functions:
- clearerr()
- feof()
- ferror()
- fileno()
- fgetc()
- getc()
- ungetc()

Differential Revision: https://reviews.llvm.org/D56136

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
    compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc?rev=350225&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc Wed Jan  2 09:36:37 2019
@@ -0,0 +1,41 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <assert.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+  FILE *fp = fopen(argv[0], "r");
+  assert(fp);
+
+  // file should be good upon opening
+  assert(!feof(fp) && !ferror(fp));
+
+  // read until EOF
+  char buf[BUFSIZ];
+  while (fread(buf, 1, sizeof buf, fp) != 0) {}
+  assert(feof(fp));
+
+  // clear EOF
+  clearerr(fp);
+  assert(!feof(fp) && !ferror(fp));
+
+  // get file descriptor
+  int fd = fileno(fp);
+  assert(fd != -1);
+
+  // break the file by closing underlying descriptor
+  assert(close(fd) != -1);
+
+  // verify that an error is signalled
+  assert(fread(buf, 1, sizeof buf, fp) == 0);
+  assert(ferror(fp));
+
+  // clear error
+  clearerr(fp);
+  assert(!feof(fp) && !ferror(fp));
+
+  // fclose() will return EBADF because of closed fd
+  assert(fclose(fp) == -1);
+  return 0;
+}

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc?rev=350225&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/Posix/fgetc_ungetc_getc.cc Wed Jan  2 09:36:37 2019
@@ -0,0 +1,19 @@
+// RUN: %clangxx -g %s -o %t && %run %t
+
+#include <assert.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+  FILE *fp = fopen(argv[0], "r");
+  assert(fp);
+
+  // the file should be at least one character long, always
+  assert(fgetc(fp) != EOF);
+  // POSIX guarantees being able to ungetc() at least one character
+  assert(ungetc('X', fp) != EOF);
+  // check whether ungetc() worked
+  assert(getc(fp) == 'X');
+
+  assert(!fclose(fp));
+  return 0;
+}




More information about the llvm-commits mailing list