1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,debug.ExprInspection -verify %s
3 // RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,alpha.unix,debug.ExprInspection -verify %s
5 #include "Inputs/system-header-simulator.h"
6 #include "Inputs/system-header-simulator-for-malloc.h"
8 void test_getline_null_buffer() {
14 if (getline(&buffer
, &n
, F1
) > 0) {
15 char c
= buffer
[0]; // ok
21 void test_getline_malloc_buffer() {
27 char *buffer
= malloc(n
);
30 ssize_t r
= getdelim(&buffer
, &n
, '\r', F1
);
31 // ptr may be dangling
32 free(ptr
); // expected-warning {{Attempt to free released memory}}
37 void test_getline_alloca() {
42 char *buffer
= alloca(n
);
43 getline(&buffer
, &n
, F1
); // expected-warning {{Memory allocated by 'alloca()' should not be deallocated}}
47 void test_getline_invalid_ptr() {
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()'}}
57 void test_getline_leak() {
66 while ((read
= getline(&buffer
, &n
, F1
)) != -1) {
67 printf("%s\n", buffer
);
70 fclose(F1
); // expected-warning {{Potential memory leak}}
73 void test_getline_stack() {
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()'}}
85 void test_getline_static() {
87 static char buffer
[10];
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()'}}