1 // RUN: %clang_analyze_cc1 -analyzer-checker=core \
2 // RUN: -analyzer-checker=unix.Stream \
3 // RUN: -analyzer-config unix.Stream:Pedantic=true \
4 // RUN: -analyzer-checker=unix.Errno \
5 // RUN: -analyzer-checker=unix.StdCLibraryFunctions \
6 // RUN: -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true \
7 // RUN: -analyzer-output text -verify %s
9 #include "Inputs/system-header-simulator.h"
10 #include "Inputs/errno_func.h"
12 void check_fopen(void) {
13 FILE *F
= fopen("xxx", "r");
14 // expected-note@-1{{Assuming that 'fopen' is successful; 'errno' becomes undefined after the call}}
15 // expected-note@+2{{'F' is non-null}}
16 // expected-note@+1{{Taking false branch}}
19 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}
20 // expected-note@-1{{An undefined value may be read from 'errno'}}
24 void check_tmpfile(void) {
26 // expected-note@-1{{Assuming that 'tmpfile' is successful; 'errno' becomes undefined after the call}}
27 // expected-note@+2{{'F' is non-null}}
28 // expected-note@+1{{Taking false branch}}
31 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno' [unix.Errno]}}
32 // expected-note@-1{{An undefined value may be read from 'errno'}}
36 void check_freopen(void) {
38 // expected-note@+2{{'F' is non-null}}
39 // expected-note@+1{{Taking false branch}}
42 F
= freopen("xxx", "w", F
);
43 // expected-note@-1{{Assuming that 'freopen' is successful; 'errno' becomes undefined after the call}}
44 // expected-note@+2{{'F' is non-null}}
45 // expected-note@+1{{Taking false branch}}
48 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
49 // expected-note@-1{{An undefined value may be read from 'errno'}}
53 void check_fclose(void) {
55 // expected-note@+2{{'F' is non-null}}
56 // expected-note@+1{{Taking false branch}}
60 // expected-note@-1{{Assuming that 'fclose' is successful; 'errno' becomes undefined after the call}}
61 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
62 // expected-note@-1{{An undefined value may be read from 'errno'}}
65 void check_fread(void) {
68 // expected-note@+2{{'F' is non-null}}
69 // expected-note@+1{{Taking false branch}}
72 (void)fread(Buf
, 1, 10, F
);
73 // expected-note@-1{{Assuming that 'fread' is successful; 'errno' becomes undefined after the call}}
74 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
75 // expected-note@-1{{An undefined value may be read from 'errno'}}
79 void check_fread_size0(void) {
82 // expected-note@+2{{'F' is non-null}}
83 // expected-note@+1{{Taking false branch}}
87 // expected-note@-1{{Assuming that argument 'size' to 'fread' is 0; 'errno' becomes undefined after the call}}
88 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
89 // expected-note@-1{{An undefined value may be read from 'errno'}}
92 void check_fread_nmemb0(void) {
95 // expected-note@+2{{'F' is non-null}}
96 // expected-note@+1{{Taking false branch}}
100 // expected-note@-1{{Assuming that 'fread' is successful; 'errno' becomes undefined after the call}}
101 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
102 // expected-note@-1{{An undefined value may be read from 'errno'}}
105 void check_fwrite(void) {
106 char Buf
[] = "0123456789";
108 // expected-note@+2{{'F' is non-null}}
109 // expected-note@+1{{Taking false branch}}
112 int R
= fwrite(Buf
, 1, 10, F
);
113 // expected-note@-1{{Assuming that 'fwrite' is successful; 'errno' becomes undefined after the call}}
114 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
115 // expected-note@-1{{An undefined value may be read from 'errno'}}
119 void check_fseek(void) {
121 // expected-note@+2{{'F' is non-null}}
122 // expected-note@+1{{Taking false branch}}
125 (void)fseek(F
, 11, SEEK_SET
);
126 // expected-note@-1{{Assuming that 'fseek' is successful; 'errno' becomes undefined after the call}}
127 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
128 // expected-note@-1{{An undefined value may be read from 'errno'}}
132 void check_rewind_errnocheck(void) {
134 // expected-note@+2{{'F' is non-null}}
135 // expected-note@+1{{Taking false branch}}
139 rewind(F
); // expected-note{{After calling 'rewind' reading 'errno' is required to find out if the call has failed}}
140 fclose(F
); // expected-warning{{Value of 'errno' was not checked and may be overwritten by function 'fclose' [unix.Errno]}}
141 // expected-note@-1{{Value of 'errno' was not checked and may be overwritten by function 'fclose'}}
144 void check_fileno(void) {
145 // nothing to check: checker assumes that 'fileno' is always successful
146 // (and does not change 'errno')
149 void check_fwrite_zeroarg(size_t Siz
) {
150 char Buf
[] = "0123456789";
152 // expected-note@+2{{'F' is non-null}}
153 // expected-note@+1{{Taking false branch}}
157 int R
= fwrite(Buf
, Siz
, 1, F
);
158 // expected-note@-1{{Assuming that argument 'size' to 'fwrite' is 0; 'errno' becomes undefined after the call}}
159 // expected-note@+2{{'R' is <= 0}}
160 // expected-note@+1{{Taking true branch}}
162 if (errno
) {} // expected-warning{{An undefined value may be read from 'errno'}}
163 // expected-note@-1{{An undefined value may be read from 'errno'}}