You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang][analyzer] Model allocation behavior or getdelim/geline (#83138)
`getdelim` and `getline` may free, allocate, or re-allocate the input
buffer, ensuring its size is enough to hold the incoming line, the
delimiter, and the null terminator.
`*lineptr` must be a valid argument to `free`, which means it can be
either
1. `NULL`, in which case these functions perform an allocation
equivalent to a call to `malloc` even on failure.
2. A pointer returned by the `malloc` family of functions. Other
pointers are UB (`alloca`, a pointer to a static, to a stack variable, etc.)
free(ptr); // expected-warning {{Attempt to free released memory}}
33
+
free(buffer); // ok
34
+
fclose(F1);
35
+
}
36
+
37
+
voidtest_getline_alloca() {
38
+
FILE*F1=tmpfile();
39
+
if (!F1)
40
+
return;
41
+
size_tn=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
+
voidtest_getline_invalid_ptr() {
48
+
FILE*F1=tmpfile();
49
+
if (!F1)
50
+
return;
51
+
size_tn=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()}}
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
+
voidtest_getline_static() {
86
+
staticsize_tn=10;
87
+
staticcharbuffer[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()}}
0 commit comments