brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.4 KiB · 81bee58 Raw
64 lines · c
1// RUN: %clang_analyze_cc1                                                      \2// RUN:  -analyzer-checker=security.cert.env.InvalidPtr                         \3// RUN:  -analyzer-config security.cert.env.InvalidPtr:InvalidatingGetEnv=false \4// RUN:  -analyzer-output=text -verify -Wno-unused %s5//6// RUN: %clang_analyze_cc1                                                      \7// RUN:  -analyzer-checker=security.cert.env.InvalidPtr                         \8// RUN:  -analyzer-config                                                       \9// RUN: security.cert.env.InvalidPtr:InvalidatingGetEnv=true                    \10// RUN: -analyzer-output=text -verify=expected,pedantic -Wno-unused %s11 12#include "Inputs/system-header-simulator.h"13 14char *getenv(const char *name);15int setenv(const char *name, const char *value, int overwrite);16int strcmp(const char *, const char *);17 18int custom_env_handler(const char **envp);19 20void getenv_after_getenv(void) {21  char *v1 = getenv("V1");22  // pedantic-note@-1{{previous function call was here}}23 24  char *v2 = getenv("V2");25  // pedantic-note@-1{{'getenv' call may invalidate the result of the previous 'getenv'}}26 27  strcmp(v1, v2);28  // pedantic-warning@-1{{use of invalidated pointer 'v1' in a function call}}29  // pedantic-note@-2{{use of invalidated pointer 'v1' in a function call}}30}31 32void setenv_after_getenv(void) {33  char *v1 = getenv("VAR1");34 35  setenv("VAR2", "...", 1);36  // expected-note@-1{{'setenv' call may invalidate the environment returned by 'getenv'}}37 38  strcmp(v1, "");39  // expected-warning@-1{{use of invalidated pointer 'v1' in a function call}}40  // expected-note@-2{{use of invalidated pointer 'v1' in a function call}}41}42 43int main(int argc, const char *argv[], const char *envp[]) {44  setenv("VAR", "...", 0);45  // expected-note@-1 2 {{'setenv' call may invalidate the environment parameter of 'main'}}46 47  *envp;48  // expected-warning@-1 2 {{dereferencing an invalid pointer}}49  // expected-note@-2 2 {{dereferencing an invalid pointer}}50}51 52void multiple_invalidation_no_duplicate_notes(void) {53  char *v1 = getenv("VAR1");54 55  setenv("VAR2", "...", 1); // no note here56 57  setenv("VAR3", "...", 1);58  // expected-note@-1{{'setenv' call may invalidate the environment returned by 'getenv'}}59 60  strcmp(v1, "");61  // expected-warning@-1{{use of invalidated pointer 'v1' in a function call}}62  // expected-note@-2{{use of invalidated pointer 'v1' in a function call}}63}64