xref: /llvm-project/clang/test/Analysis/errno-stdlibraryfunctions-notes.c (revision 72d3bf2b87ff7fab1a189d76f516bc03eac3271d)
1 // RUN: %clang_analyze_cc1 -verify -analyzer-output text %s \
2 // RUN:   -analyzer-checker=core \
3 // RUN:   -analyzer-checker=debug.ExprInspection \
4 // RUN:   -analyzer-checker=unix.StdCLibraryFunctions \
5 // RUN:   -analyzer-checker=apiModeling.Errno \
6 // RUN:   -analyzer-checker=unix.Errno \
7 // RUN:   -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true
8 
9 #include "Inputs/errno_var.h"
10 
11 int access(const char *path, int amode);
12 
13 void clang_analyzer_warnIfReached();
14 
test1()15 void test1() {
16   access("path", 0);
17   access("path", 0);
18   // expected-note@-1{{Assuming that 'access' is successful; 'errno' becomes undefined after the call}}
19   if (errno != 0) {
20     // expected-warning@-1{{An undefined value may be read from 'errno'}}
21     // expected-note@-2{{An undefined value may be read from 'errno'}}
22   }
23 }
24 
test2()25 void test2() {
26   if (access("path", 0) == -1) {
27     // expected-note@-1{{Taking true branch}}
28     // Failure path.
29     if (errno != 0) {
30       // expected-note@-1{{'errno' is not equal to 0}}
31       // expected-note@-2{{Taking true branch}}
32       clang_analyzer_warnIfReached(); // expected-note {{REACHABLE}} expected-warning {{REACHABLE}}
33     } else {
34       clang_analyzer_warnIfReached(); // no-warning: We are on the failure path.
35     }
36   }
37 }
38 
test3()39 void test3() {
40   if (access("path", 0) != -1) {
41     // Success path.
42     // expected-note@-2{{Assuming that 'access' is successful; 'errno' becomes undefined after the call}}
43     // expected-note@-3{{Taking true branch}}
44     if (errno != 0) {
45       // expected-warning@-1{{An undefined value may be read from 'errno'}}
46       // expected-note@-2{{An undefined value may be read from 'errno'}}
47     }
48   }
49 }
50