| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,debug.ExprInspection -verify %s |
| 2 |
|
| 3 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,alpha.unix,debug.ExprInspection -verify %s |
| 4 |
|
| 5 |
#include "Inputs/system-header-simulator.h" |
| 6 |
#include "Inputs/system-header-simulator-for-malloc.h" |
| 7 |
|
| 8 |
void test_getline_null_buffer() { |
| 9 |
FILE *F1 = tmpfile(); |
| 10 |
if (!F1) |
| 11 |
return; |
| 12 |
char *buffer = NULL; |
| 13 |
size_t n = 0; |
| 14 |
if (getline(&buffer, &n, F1) > 0) { |
| 15 |
char c = buffer[0]; // ok |
| 16 |
} |
| 17 |
free(buffer); |
| 18 |
fclose(F1); |
| 19 |
} |
| 20 |
|
| 21 |
void test_getline_malloc_buffer() { |
| 22 |
FILE *F1 = tmpfile(); |
| 23 |
if (!F1) |
| 24 |
return; |
| 25 |
|
| 26 |
size_t n = 10; |
| 27 |
char *buffer = malloc(n); |
| 28 |
char *ptr = buffer; |
| 29 |
|
| 30 |
ssize_t r = getdelim(&buffer, &n, '\r', F1); |
| 31 |
// ptr may be dangling |
| 32 |
free(ptr); // expected-warning {{Attempt to release already released memory}} |
| 33 |
free(buffer); // ok |
| 34 |
fclose(F1); |
| 35 |
} |
| 36 |
|
| 37 |
void test_getline_alloca() { |
| 38 |
FILE *F1 = tmpfile(); |
| 39 |
if (!F1) |
| 40 |
return; |
| 41 |
size_t n = 10; |
| 42 |
char *buffer = alloca(n); |
| 43 |
getline(&buffer, &n, F1); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}} |
| 44 |
fclose(F1); |
| 45 |
} |
| 46 |
|
| 47 |
void test_getline_invalid_ptr() { |
| 48 |
FILE *F1 = tmpfile(); |
| 49 |
if (!F1) |
| 50 |
return; |
| 51 |
size_t n = 10; |
| 52 |
char *buffer = (char*)test_getline_invalid_ptr; |
| 53 |
getline(&buffer, &n, F1); // expected-warning {{Argument to 'getline()' is the address of the function 'test_getline_invalid_ptr', which is not memory allocated by 'malloc()'}} |
| 54 |
fclose(F1); |
| 55 |
} |
| 56 |
|
| 57 |
void test_getline_leak() { |
| 58 |
FILE *F1 = tmpfile(); |
| 59 |
if (!F1) |
| 60 |
return; |
| 61 |
|
| 62 |
char *buffer = NULL; |
| 63 |
size_t n = 0; |
| 64 |
ssize_t read; |
| 65 |
|
| 66 |
while ((read = getline(&buffer, &n, F1)) != -1) { |
| 67 |
printf("%s\n", buffer); |
| 68 |
} |
| 69 |
|
| 70 |
fclose(F1); // expected-warning {{Potential memory leak}} |
| 71 |
} |
| 72 |
|
| 73 |
void test_getline_stack() { |
| 74 |
size_t n = 10; |
| 75 |
char buffer[10]; |
| 76 |
char *ptr = buffer; |
| 77 |
|
| 78 |
FILE *F1 = tmpfile(); |
| 79 |
if (!F1) |
| 80 |
return; |
| 81 |
|
| 82 |
getline(&ptr, &n, F1); // expected-warning {{Argument to 'getline()' is the address of the local variable 'buffer', which is not memory allocated by 'malloc()'}} |
| 83 |
} |
| 84 |
|
| 85 |
void test_getline_static() { |
| 86 |
static size_t n = 10; |
| 87 |
static char buffer[10]; |
| 88 |
char *ptr = buffer; |
| 89 |
|
| 90 |
FILE *F1 = tmpfile(); |
| 91 |
if (!F1) |
| 92 |
return; |
| 93 |
|
| 94 |
getline(&ptr, &n, F1); // expected-warning {{Argument to 'getline()' is the address of the static variable 'buffer', which is not memory allocated by 'malloc()'}} |
| 95 |
} |