xref: /llvm-project/clang/test/Analysis/cert/env31-c.c (revision b98a594977f25e555822e5ceef457f69c73cce45)
1 // RUN: %clang_analyze_cc1 -analyzer-output=text -Wno-unused %s     \
2 // RUN:   -analyzer-checker=core,security.cert.env.InvalidPtr       \
3 // RUN:   -verify=putenv,common                                     \
4 // RUN:   -DENV_INVALIDATING_CALL="putenv(\"X=Y\")"
5 //
6 // RUN: %clang_analyze_cc1 -analyzer-output=text -Wno-unused %s     \
7 // RUN:   -analyzer-checker=core,security.cert.env.InvalidPtr       \
8 // RUN:   -verify=putenvs,common                                    \
9 // RUN:   -DENV_INVALIDATING_CALL="_putenv_s(\"X\", \"Y\")"
10 //
11 // RUN: %clang_analyze_cc1 -analyzer-output=text -Wno-unused %s     \
12 // RUN:   -analyzer-checker=core,security.cert.env.InvalidPtr       \
13 // RUN:   -verify=wputenvs,common                                   \
14 // RUN:   -DENV_INVALIDATING_CALL="_wputenv_s(\"X\", \"Y\")"
15 //
16 // RUN: %clang_analyze_cc1 -analyzer-output=text -Wno-unused %s     \
17 // RUN:   -analyzer-checker=core,security.cert.env.InvalidPtr       \
18 // RUN:   -verify=setenv,common                                     \
19 // RUN:   -DENV_INVALIDATING_CALL="setenv(\"X\", \"Y\", 0)"
20 //
21 // RUN: %clang_analyze_cc1 -analyzer-output=text -Wno-unused %s     \
22 // RUN:   -analyzer-checker=core,security.cert.env.InvalidPtr       \
23 // RUN:   -verify=unsetenv,common                                   \
24 // RUN:   -DENV_INVALIDATING_CALL="unsetenv(\"X\")"
25 
26 typedef int errno_t;
27 typedef char wchar_t;
28 
29 int putenv(char *string);
30 errno_t _putenv_s(const char *varname, const char *value_string);
31 errno_t _wputenv_s(const wchar_t *varname, const wchar_t *value_string);
32 int setenv(const char *name, const char *value, int overwrite);
33 int unsetenv(const char *name);
34 
35 void fn_without_body(char **e);
fn_with_body(char ** e)36 void fn_with_body(char **e) {}
37 
call_env_invalidating_fn(char ** e)38 void call_env_invalidating_fn(char **e) {
39   ENV_INVALIDATING_CALL;
40   // putenv-note@-1 5 {{'putenv' call may invalidate the environment parameter of 'main'}}
41   // putenvs-note@-2 5 {{'_putenv_s' call may invalidate the environment parameter of 'main'}}
42   // wputenvs-note@-3 5 {{'_wputenv_s' call may invalidate the environment parameter of 'main'}}
43   // setenv-note@-4 5 {{'setenv' call may invalidate the environment parameter of 'main'}}
44   // unsetenv-note@-5 5 {{'unsetenv' call may invalidate the environment parameter of 'main'}}
45 
46   *e;
47   // common-warning@-1 {{dereferencing an invalid pointer}}
48   // common-note@-2 {{dereferencing an invalid pointer}}
49 }
50 
main(int argc,char * argv[],char * envp[])51 int main(int argc, char *argv[], char *envp[]) {
52   char **e = envp;
53   *e;    // no-warning
54   e[0];  // no-warning
55   *envp; // no-warning
56   call_env_invalidating_fn(e);
57   // common-note@-1 5 {{Calling 'call_env_invalidating_fn'}}
58   // common-note@-2 4 {{Returning from 'call_env_invalidating_fn'}}
59 
60   *e;
61   // common-warning@-1 {{dereferencing an invalid pointer}}
62   // common-note@-2 {{dereferencing an invalid pointer}}
63 
64   *envp;
65   // common-warning@-1 2 {{dereferencing an invalid pointer}}
66   // common-note@-2 2 {{dereferencing an invalid pointer}}
67 
68   fn_without_body(e);
69   // common-warning@-1 {{use of invalidated pointer 'e' in a function call}}
70   // common-note@-2 {{use of invalidated pointer 'e' in a function call}}
71 
72   fn_with_body(e); // no-warning
73 }
74