1 // RUN: %clang_analyze_cc1 \
2 // RUN: -analyzer-checker=alpha.security.cert.pos.34c\
5 // Examples from the CERT rule's page.
6 // https://wiki.sei.cmu.edu/confluence/x/6NYxBQ
8 #include "../Inputs/system-header-simulator.h"
9 void free(void *memblock
);
10 void *malloc(size_t size
);
12 int snprintf(char *str
, size_t size
, const char *format
, ...);
14 namespace test_auto_var_used_bad
{
16 int volatile_memory1(const char *var
) {
18 int retval
= snprintf(env
, sizeof(env
), "TEST=%s", var
);
19 if (retval
< 0 || (size_t)retval
>= sizeof(env
)) {
24 // expected-warning@-1 {{The 'putenv' function should not be called with arguments that have automatic storage}}
27 } // namespace test_auto_var_used_bad
29 namespace test_auto_var_used_good
{
31 int test_static(const char *var
) {
32 static char env
[1024];
34 int retval
= snprintf(env
, sizeof(env
), "TEST=%s", var
);
35 if (retval
< 0 || (size_t)retval
>= sizeof(env
)) {
42 int test_heap_memory(const char *var
) {
44 const char *env_format
= "TEST=%s";
45 const size_t len
= strlen(var
) + strlen(env_format
);
46 char *env
= (char *)malloc(len
);
50 if (putenv(env
) != 0) { // no-warning: env was dynamically allocated.
55 free(oldenv
); /* avoid memory leak */
61 } // namespace test_auto_var_used_good