brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.2 KiB · ca82844 Raw
135 lines · c
1// Default options.2// RUN: %clang_analyze_cc1                                                      \3// RUN:  -analyzer-checker=core,security.cert.env.InvalidPtr                    \4// RUN:  -verify -Wno-unused %s5//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 %s11//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 %s17 18#include "../Inputs/system-header-simulator.h"19char *getenv(const char *name);20int setenv(const char *name, const char *value, int overwrite);21int strcmp(const char*, const char*);22char *strdup(const char*);23void free(void *memblock);24void *malloc(size_t size);25 26void incorrect_usage_setenv_getenv_invalidation(void) {27  char *tmpvar;28  char *tempvar;29 30  tmpvar = getenv("TMP");31 32  if (!tmpvar)33    return;34 35  setenv("TEMP", "", 1); //setenv can invalidate env36 37  if (!tmpvar)38    return;39 40  if (strcmp(tmpvar, "") == 0) { // body of strcmp is unknown41    // expected-warning@-1{{use of invalidated pointer 'tmpvar' in a function call}}42  }43}44 45void incorrect_usage_double_getenv_invalidation(void) {46  char *tmpvar;47  char *tempvar;48 49  tmpvar = getenv("TMP");50 51  if (!tmpvar)52    return;53 54  tempvar = getenv("TEMP"); //getenv should not invalidate env in non-pedantic mode55 56  if (!tempvar)57    return;58 59  if (strcmp(tmpvar, tempvar) == 0) { // body of strcmp is unknown60    // pedantic-warning@-1{{use of invalidated pointer 'tmpvar' in a function call}}61  }62}63 64void correct_usage_1(void) {65  char *tmpvar;66  char *tempvar;67 68  const char *temp = getenv("TMP");69  if (temp != NULL) {70    tmpvar = (char *)malloc(strlen(temp)+1);71    if (tmpvar != NULL) {72      strcpy(tmpvar, temp);73    } else {74      return;75    }76  } else {77    return;78  }79 80  temp = getenv("TEMP");81  if (temp != NULL) {82    tempvar = (char *)malloc(strlen(temp)+1);83    if (tempvar != NULL) {84      strcpy(tempvar, temp);85    } else {86      return;87    }88  } else {89    return;90  }91 92  if (strcmp(tmpvar, tempvar) == 0) {93    printf("TMP and TEMP are the same.\n");94  } else {95    printf("TMP and TEMP are NOT the same.\n");96  }97  free(tmpvar);98  free(tempvar);99}100 101void correct_usage_2(void) {102  char *tmpvar;103  char *tempvar;104 105  const char *temp = getenv("TMP");106  if (temp != NULL) {107    tmpvar = strdup(temp);108    if (tmpvar == NULL) {109      return;110    }111  } else {112    return;113  }114 115  temp = getenv("TEMP");116  if (temp != NULL) {117    tempvar = strdup(temp);118    if (tempvar == NULL) {119      return;120    }121  } else {122    return;123  }124 125  if (strcmp(tmpvar, tempvar) == 0) {126    printf("TMP and TEMP are the same.\n");127  } else {128    printf("TMP and TEMP are NOT the same.\n");129  }130  free(tmpvar);131  tmpvar = NULL;132  free(tempvar);133  tempvar = NULL;134}135