xref: /llvm-project/clang/test/Analysis/std-c-library-functions-path-notes.c (revision 1246b64faa5eea1553c1c1aad425c31b701fa6ea)
1 // RUN: %clang_analyze_cc1 -verify %s \
2 // RUN:     -analyzer-checker=core,unix.StdCLibraryFunctions \
3 // RUN:     -analyzer-config unix.StdCLibraryFunctions:ModelPOSIX=true \
4 // RUN:     -analyzer-output=text
5 
6 #include "Inputs/std-c-library-functions-POSIX.h"
7 #define NULL ((void *)0)
8 
9 char *getenv(const char *);
10 int isalpha(int);
11 int isdigit(int);
12 int islower(int);
13 
test_getenv()14 char test_getenv() {
15   char *env = getenv("VAR"); // \
16   // expected-note{{Assuming the environment variable does not exist}} \
17   // expected-note{{'env' initialized here}}
18 
19   return env[0]; // \
20   // expected-warning{{Array access (from variable 'env') results in a null pointer dereference}} \
21   // expected-note   {{Array access (from variable 'env') results in a null pointer dereference}}
22 }
23 
test_isalpha(int * x,char c,char d)24 int test_isalpha(int *x, char c, char d) {
25   int iad = isalpha(d);
26   if (isalpha(c)) {// \
27     // expected-note{{Taking true branch}}
28     x = NULL; // \
29     // expected-note{{Null pointer value stored to 'x'}}
30   }
31 
32   return *x; // \
33   // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} \
34   // expected-note   {{Dereference of null pointer (loaded from variable 'x')}}
35 }
36 
test_isdigit(int * x,char c)37 int test_isdigit(int *x, char c) {
38   if (!isdigit(c)) {// \
39   // expected-note{{Taking true branch}}
40     x = NULL; // \
41     // expected-note{{Null pointer value stored to 'x'}}
42   }
43 
44   return *x; // \
45   // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} \
46   // expected-note   {{Dereference of null pointer (loaded from variable 'x')}}
47 }
48 
test_islower(int * x)49 int test_islower(int *x) {
50   char c = 'c';
51   // No "Assuming..." note. We aren't assuming anything. We *know*.
52   if (islower(c)) { // \
53     // expected-note{{Taking true branch}}
54     x = NULL; // \
55     // expected-note{{Null pointer value stored to 'x'}}
56   }
57 
58   return *x; // \
59   // expected-warning{{Dereference of null pointer (loaded from variable 'x')}} \
60   // expected-note   {{Dereference of null pointer (loaded from variable 'x')}}
61 }
62 
test_bugpath_notes(FILE * f1,char c,FILE * f2)63 int test_bugpath_notes(FILE *f1, char c, FILE *f2) {
64   // This test has the purpose of checking that notes appear at correct place.
65   long a = ftell(f2); // no note
66   if (a == -1) // \
67     // expected-note{{Taking false branch}}
68     return -1;
69   int l = islower(c); // no note
70   a = ftell(f1); // \
71   // expected-note{{Value assigned to 'a'}} \
72   // expected-note{{Assuming that 'ftell' fails}}
73   return dup(a); // \
74   // expected-warning{{The 1st argument to 'dup' is -1 but should be >= 0}} \
75   // expected-note{{The 1st argument to 'dup' is -1 but should be >= 0}}
76 }
77 
test_fileno_arg_note(FILE * f1)78 int test_fileno_arg_note(FILE *f1) {
79   return dup(fileno(f1)); // no warning
80 }
81 
test_readlink_bufsize_zero(char * Buf,size_t Bufsize)82 int test_readlink_bufsize_zero(char *Buf, size_t Bufsize) {
83   ssize_t Ret = readlink("path", Buf, Bufsize); // \
84   // expected-note{{Assuming that argument 'bufsize' is 0}} \
85   // expected-note{{'Ret' initialized here}}
86   return 1 / Ret; // \
87   // expected-warning{{Division by zero}} \
88   // expected-note{{Division by zero}}
89 }
90