2 // RUN: %clang_analyze_cc1 \
3 // RUN: -analyzer-checker=core,security.cert.env.InvalidPtr \
4 // RUN: -verify -Wno-unused %s
6 // Test the laxer handling of getenv function (this is the default).
7 // RUN: %clang_analyze_cc1 \
8 // RUN: -analyzer-checker=core,security.cert.env.InvalidPtr \
9 // RUN: -analyzer-config security.cert.env.InvalidPtr:InvalidatingGetEnv=false \
10 // RUN: -verify -Wno-unused %s
12 // Test the stricter handling of getenv function.
13 // RUN: %clang_analyze_cc1 \
14 // RUN: -analyzer-checker=core,security.cert.env.InvalidPtr \
15 // RUN: -analyzer-config security.cert.env.InvalidPtr:InvalidatingGetEnv=true \
16 // RUN: -verify=expected,pedantic -Wno-unused %s
18 #include "../Inputs/system-header-simulator.h"
19 char *getenv(const char *name
);
20 int setenv(const char *name
, const char *value
, int overwrite
);
21 int strcmp(const char*, const char*);
22 char *strdup(const char*);
23 void free(void *memblock
);
24 void *malloc(size_t size
);
26 void incorrect_usage_setenv_getenv_invalidation(void) {
30 tmpvar
= getenv("TMP");
35 setenv("TEMP", "", 1); //setenv can invalidate env
40 if (strcmp(tmpvar
, "") == 0) { // body of strcmp is unknown
41 // expected-warning@-1{{use of invalidated pointer 'tmpvar' in a function call}}
45 void incorrect_usage_double_getenv_invalidation(void) {
49 tmpvar
= getenv("TMP");
54 tempvar
= getenv("TEMP"); //getenv should not invalidate env in non-pedantic mode
59 if (strcmp(tmpvar
, tempvar
) == 0) { // body of strcmp is unknown
60 // pedantic-warning@-1{{use of invalidated pointer 'tmpvar' in a function call}}
64 void correct_usage_1(void) {
68 const char *temp
= getenv("TMP");
70 tmpvar
= (char *)malloc(strlen(temp
)+1);
80 temp
= getenv("TEMP");
82 tempvar
= (char *)malloc(strlen(temp
)+1);
83 if (tempvar
!= NULL
) {
84 strcpy(tempvar
, temp
);
92 if (strcmp(tmpvar
, tempvar
) == 0) {
93 printf("TMP and TEMP are the same.\n");
95 printf("TMP and TEMP are NOT the same.\n");
101 void correct_usage_2(void) {
105 const char *temp
= getenv("TMP");
107 tmpvar
= strdup(temp
);
108 if (tmpvar
== NULL
) {
115 temp
= getenv("TEMP");
117 tempvar
= strdup(temp
);
118 if (tempvar
== NULL
) {
125 if (strcmp(tmpvar
, tempvar
) == 0) {
126 printf("TMP and TEMP are the same.\n");
128 printf("TMP and TEMP are NOT the same.\n");