aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBalázs Kéri <balazs.keri@ericsson.com>2024-03-28 18:04:35 +0100
committerGitHub <noreply@github.com>2024-03-28 18:04:35 +0100
commit8dcff10e9b7405138af768ec89d0718e048ca340 (patch)
treee020bdc382ffcfd7fc952eeb13e4ed03dc29bd3a
parent23ca8e654df4a416132ac1207dd3ba8a14b82774 (diff)
downloadllvm-8dcff10e9b7405138af768ec89d0718e048ca340.zip
llvm-8dcff10e9b7405138af768ec89d0718e048ca340.tar.gz
llvm-8dcff10e9b7405138af768ec89d0718e048ca340.tar.bz2
[clang][analyzer] Improve documentation of StreamChecker (NFC). (#83858)
-rw-r--r--clang/docs/analyzer/checkers.rst66
1 files changed, 52 insertions, 14 deletions
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 8af99a0..f188f18 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -3120,44 +3120,82 @@ Check for misuses of stream APIs. Check for misuses of stream APIs: ``fopen, fcl
alpha.unix.Stream (C)
"""""""""""""""""""""
-Check stream handling functions: ``fopen, tmpfile, fclose, fread, fwrite, fseek, ftell, rewind, fgetpos,``
-``fsetpos, clearerr, feof, ferror, fileno``.
+Check C stream handling functions:
+``fopen, fdopen, freopen, tmpfile, fclose, fread, fwrite, fgetc, fgets, fputc, fputs, fprintf, fscanf, ungetc, getdelim, getline, fseek, fseeko, ftell, ftello, fflush, rewind, fgetpos, fsetpos, clearerr, feof, ferror, fileno``.
+
+The checker maintains information about the C stream objects (``FILE *``) and
+can detect error conditions related to use of streams. The following conditions
+are detected:
+
+* The ``FILE *`` pointer passed to the function is NULL (the single exception is
+ ``fflush`` where NULL is allowed).
+* Use of stream after close.
+* Opened stream is not closed.
+* Read from a stream after end-of-file. (This is not a fatal error but reported
+ by the checker. Stream remains in EOF state and the read operation fails.)
+* Use of stream when the file position is indeterminate after a previous failed
+ operation. Some functions (like ``ferror``, ``clearerr``, ``fseek``) are
+ allowed in this state.
+* Invalid 3rd ("``whence``") argument to ``fseek``.
+
+The checker does not track the correspondence between integer file descriptors
+and ``FILE *`` pointers. Operations on standard streams like ``stdin`` are not
+treated specially and are therefore often not recognized (because these streams
+are usually not opened explicitly by the program, and are global variables).
.. code-block:: c
- void test() {
+ void test1() {
FILE *p = fopen("foo", "r");
} // warn: opened file is never closed
- void test() {
+ void test2() {
FILE *p = fopen("foo", "r");
fseek(p, 1, SEEK_SET); // warn: stream pointer might be NULL
fclose(p);
}
- void test() {
+ void test3() {
FILE *p = fopen("foo", "r");
+ if (p) {
+ fseek(p, 1, 3); // warn: third arg should be SEEK_SET, SEEK_END, or SEEK_CUR
+ fclose(p);
+ }
+ }
- if (p)
- fseek(p, 1, 3);
- // warn: third arg should be SEEK_SET, SEEK_END, or SEEK_CUR
+ void test4() {
+ FILE *p = fopen("foo", "r");
+ if (!p)
+ return;
fclose(p);
+ fclose(p); // warn: stream already closed
}
- void test() {
+ void test5() {
FILE *p = fopen("foo", "r");
+ if (!p)
+ return;
+
+ fgetc(p);
+ if (!ferror(p))
+ fgetc(p); // warn: possible read after end-of-file
+
fclose(p);
- fclose(p); // warn: already closed
}
- void test() {
- FILE *p = tmpfile();
- ftell(p); // warn: stream pointer might be NULL
+ void test6() {
+ FILE *p = fopen("foo", "r");
+ if (!p)
+ return;
+
+ fgetc(p);
+ if (!feof(p))
+ fgetc(p); // warn: file position may be indeterminate after I/O error
+
fclose(p);
}
-
.. _alpha-unix-cstring-BufferOverlap:
alpha.unix.cstring.BufferOverlap (C)