1 // RUN: %clang_analyze_cc1 \
2 // RUN: -analyzer-checker=security.PutenvStackArray \
5 #include "Inputs/system-header-simulator.h"
9 int snprintf(char *, size_t, const char *, ...);
11 int test_auto_var(const char *var
) {
13 (void)snprintf(env
, sizeof(env
), "TEST=%s", var
);
14 return putenv(env
); // expected-warning{{The 'putenv' function should not be called with arrays that have automatic storage}}
17 int test_static_var(const char *var
) {
18 static char env
[1024];
19 (void)snprintf(env
, sizeof(env
), "TEST=%s", var
);
20 return putenv(env
); // no-warning: static array is used
23 void test_heap_memory(const char *var
) {
24 const char *env_format
= "TEST=%s";
25 const size_t len
= strlen(var
) + strlen(env_format
);
26 char *env
= (char *)malloc(len
);
29 if (putenv(env
) != 0) // no-warning: env was dynamically allocated.
38 int test_auto_var_struct() {
40 return putenv(mem
.Env
); // expected-warning{{The 'putenv' function should not be called with}}
43 int test_auto_var_subarray() {
45 return putenv(env
+ 100); // expected-warning{{The 'putenv' function should not be called with}}
48 int f_test_auto_var_call(char *env
) {
49 return putenv(env
); // expected-warning{{The 'putenv' function should not be called with}}
52 int test_auto_var_call() {
54 return f_test_auto_var_call(env
);
59 return putenv(env
); // no-warning: data is not on the stack
64 return putenv(ext_env
); // no-warning: extern storage class.
67 void test_auto_var_reset() {
68 char env
[] = "NAME=value";
69 putenv(env
); // expected-warning{{The 'putenv' function should not be called with}}
71 // Even cases like this are likely a bug:
72 // It looks like that if one string was passed to putenv,
73 // it should not be deallocated at all, because when reading the
74 // environment variable a pointer into this string is returned.
75 // In this case, if another (or the same) thread reads variable "NAME"
76 // at this point and does not copy the returned string, the data may
78 putenv((char *)"NAME=anothervalue");
81 void f_main(char *env
) {
82 putenv(env
); // no warning: string allocated in stack of 'main'
85 int main(int argc
, char **argv
) {
86 char env
[] = "NAME=value";
87 putenv(env
); // no warning: string allocated in stack of 'main'