[RISCV] Simplify usage of SplatPat_simm5_plus1. NFC (#125340)
[llvm-project.git] / clang / test / Analysis / getline-alloc.c
blob74a40a11b978285fc434f247b1477d31cc50aa31
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() {
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
17 free(buffer);
18 fclose(F1);
21 void test_getline_malloc_buffer() {
22 FILE *F1 = tmpfile();
23 if (!F1)
24 return;
26 size_t n = 10;
27 char *buffer = malloc(n);
28 char *ptr = buffer;
30 ssize_t r = getdelim(&buffer, &n, '\r', F1);
31 // ptr may be dangling
32 free(ptr); // expected-warning {{Attempt to free released memory}}
33 free(buffer); // ok
34 fclose(F1);
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);
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);
57 void test_getline_leak() {
58 FILE *F1 = tmpfile();
59 if (!F1)
60 return;
62 char *buffer = NULL;
63 size_t n = 0;
64 ssize_t read;
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() {
74 size_t n = 10;
75 char buffer[10];
76 char *ptr = buffer;
78 FILE *F1 = tmpfile();
79 if (!F1)
80 return;
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() {
86 static size_t n = 10;
87 static char buffer[10];
88 char *ptr = buffer;
90 FILE *F1 = tmpfile();
91 if (!F1)
92 return;
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()'}}