Skip to content

Commit 5c3cfde

Browse files
Reorganize the tests
1 parent 6b55204 commit 5c3cfde

File tree

2 files changed

+122
-108
lines changed

2 files changed

+122
-108
lines changed

clang/test/Analysis/getline-unixapi.c

Lines changed: 2 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,alpha.unix.Stream,debug.ExprInspection -verify %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix,debug.ExprInspection -verify %s
22

33
#include "Inputs/system-header-simulator.h"
44
#include "Inputs/system-header-simulator-for-malloc.h"
@@ -9,12 +9,6 @@ void clang_analyzer_dump_int(int);
99
void clang_analyzer_dump_ptr(void*);
1010
void clang_analyzer_warnIfReached();
1111

12-
void test_getline_null_file() {
13-
char *buffer = NULL;
14-
size_t n = 0;
15-
getline(&buffer, &n, NULL); // expected-warning {{Stream pointer might be NULL}}
16-
}
17-
1812
void test_getline_null_lineptr() {
1913
FILE *F1 = tmpfile();
2014
if (!F1)
@@ -154,12 +148,6 @@ void test_getline_null_buffer() {
154148
fclose(F1);
155149
}
156150

157-
void test_getdelim_null_file() {
158-
char *buffer = NULL;
159-
size_t n = 0;
160-
getdelim(&buffer, &n, '\n', NULL); // expected-warning {{Stream pointer might be NULL}}
161-
}
162-
163151
void test_getdelim_null_size() {
164152
FILE *F1 = tmpfile();
165153
if (!F1)
@@ -240,22 +228,6 @@ void test_getline_while() {
240228
fclose(file);
241229
}
242230

243-
void test_getline_no_return_check() {
244-
FILE *file = fopen("file.txt", "r");
245-
if (file == NULL) {
246-
return;
247-
}
248-
249-
char *line = NULL;
250-
size_t len = 0;
251-
getline(&line, &len, file);
252-
253-
if (line[0] == '\0') {} // expected-warning {{The left operand of '==' is a garbage value}}
254-
255-
free(line);
256-
fclose(file);
257-
}
258-
259231
void test_getline_return_check() {
260232
FILE *file = fopen("file.txt", "r");
261233
if (file == NULL) {
@@ -273,61 +245,6 @@ void test_getline_return_check() {
273245
fclose(file);
274246
}
275247

276-
void test_getline_feof_check() {
277-
FILE *file = fopen("file.txt", "r");
278-
if (file == NULL) {
279-
return;
280-
}
281-
282-
char *line = NULL;
283-
size_t len = 0;
284-
ssize_t r = getline(&line, &len, file);
285-
286-
if (r != -1) {
287-
// success, end-of-file is not possible
288-
int f = feof(file);
289-
clang_analyzer_eval(f == 0); // expected-warning {{TRUE}}
290-
} else {
291-
// failure, end-of-file is possible, but not the only reason to fail
292-
int f = feof(file);
293-
clang_analyzer_eval(f == 0); // expected-warning {{TRUE}} \\
294-
expected-warning {{FALSE}}
295-
}
296-
free(line);
297-
fclose(file);
298-
}
299-
300-
void test_getline_after_eof() {
301-
FILE *file = fopen("file.txt", "r");
302-
if (file == NULL) {
303-
return;
304-
}
305-
306-
size_t n = 10;
307-
char *buffer = malloc(n);
308-
ssize_t read = fread(buffer, n, 1, file);
309-
if (!feof(file)) {
310-
getline(&buffer, &n, file); // expected-warning {{File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior}}
311-
}
312-
fclose(file);
313-
free(buffer);
314-
}
315-
316-
void test_getline_feof() {
317-
FILE *file = fopen("file.txt", "r");
318-
if (file == NULL) {
319-
return;
320-
}
321-
322-
size_t n = 10;
323-
char *buffer = malloc(n);
324-
ssize_t read = fread(buffer, n, 1, file);
325-
getline(&buffer, &n, file); // expected-warning {{File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior}} \\
326-
expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
327-
fclose(file);
328-
free(buffer);
329-
}
330-
331248
void test_getline_clear_eof() {
332249
FILE *file = fopen("file.txt", "r");
333250
if (file == NULL) {
@@ -379,26 +296,6 @@ void test_getline_size_constraint(size_t size) {
379296
free(buffer);
380297
}
381298

382-
void test_getline_ret_value() {
383-
FILE *file = fopen("file.txt", "r");
384-
if (file == NULL) {
385-
return;
386-
}
387-
388-
size_t n = 0;
389-
char *buffer = NULL;
390-
ssize_t r = getline(&buffer, &n, file);
391-
392-
if (r > -1) {
393-
// The return value does *not* include the terminating null byte.
394-
// The buffer must be large enough to include it.
395-
clang_analyzer_eval(n > r); // expected-warning{{TRUE}}
396-
}
397-
398-
fclose(file);
399-
free(buffer);
400-
}
401-
402299
void test_getline_negative_buffer() {
403300
FILE *file = fopen("file.txt", "r");
404301
if (file == NULL) {
@@ -419,10 +316,7 @@ void test_getline_negative_buffer_2(char *buffer) {
419316
}
420317

421318
size_t n = -1;
422-
ssize_t ret = getline(&buffer, &n, file);
423-
if (ret > 0) {
424-
clang_analyzer_eval(ret < n); // expected-warning {{TRUE}}
425-
}
319+
(void)getline(&buffer, &n, file); // ok
426320
free(buffer);
427321
fclose(file);
428322
}

clang/test/Analysis/stream.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// RUN: %clang_analyze_cc1 -triple=hexagon -analyzer-checker=core,alpha.unix.Stream,debug.ExprInspection -verify %s
55

66
#include "Inputs/system-header-simulator.h"
7+
#include "Inputs/system-header-simulator-for-malloc.h"
78
#include "Inputs/system-header-simulator-for-valist.h"
89

910
void clang_analyzer_eval(int);
@@ -376,3 +377,122 @@ void fflush_on_open_failed_stream(void) {
376377
}
377378
fclose(F);
378379
}
380+
381+
void getline_null_file() {
382+
char *buffer = NULL;
383+
size_t n = 0;
384+
getline(&buffer, &n, NULL); // expected-warning {{Stream pointer might be NULL}}
385+
}
386+
387+
void getdelim_null_file() {
388+
char *buffer = NULL;
389+
size_t n = 0;
390+
getdelim(&buffer, &n, '\n', NULL); // expected-warning {{Stream pointer might be NULL}}
391+
}
392+
393+
void getline_no_return_check() {
394+
FILE *file = fopen("file.txt", "r");
395+
if (file == NULL) {
396+
return;
397+
}
398+
399+
char *line = NULL;
400+
size_t len = 0;
401+
getline(&line, &len, file);
402+
403+
if (line[0] == '\0') {} // expected-warning {{The left operand of '==' is a garbage value}}
404+
405+
free(line);
406+
fclose(file);
407+
}
408+
409+
void getline_after_eof() {
410+
FILE *file = fopen("file.txt", "r");
411+
if (file == NULL) {
412+
return;
413+
}
414+
415+
size_t n = 10;
416+
char *buffer = malloc(n);
417+
ssize_t read = fread(buffer, n, 1, file);
418+
if (!feof(file)) {
419+
getline(&buffer, &n, file); // expected-warning {{File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior}}
420+
}
421+
fclose(file);
422+
free(buffer);
423+
}
424+
425+
void getline_feof() {
426+
FILE *file = fopen("file.txt", "r");
427+
if (file == NULL) {
428+
return;
429+
}
430+
431+
size_t n = 10;
432+
char *buffer = malloc(n);
433+
ssize_t read = fread(buffer, n, 1, file);
434+
getline(&buffer, &n, file); // expected-warning {{File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior}} \\
435+
expected-warning {{Read function called when stream is in EOF state. Function has no effect}}
436+
fclose(file);
437+
free(buffer);
438+
}
439+
440+
void getline_feof_check() {
441+
FILE *file = fopen("file.txt", "r");
442+
if (file == NULL) {
443+
return;
444+
}
445+
446+
char *line = NULL;
447+
size_t len = 0;
448+
ssize_t r = getline(&line, &len, file);
449+
450+
if (r != -1) {
451+
// success, end-of-file is not possible
452+
int f = feof(file);
453+
clang_analyzer_eval(f == 0); // expected-warning {{TRUE}}
454+
} else {
455+
// failure, end-of-file is possible, but not the only reason to fail
456+
int f = feof(file);
457+
clang_analyzer_eval(f == 0); // expected-warning {{TRUE}} \\
458+
expected-warning {{FALSE}}
459+
}
460+
free(line);
461+
fclose(file);
462+
}
463+
464+
void getline_ret_value() {
465+
FILE *file = fopen("file.txt", "r");
466+
if (file == NULL) {
467+
return;
468+
}
469+
470+
size_t n = 0;
471+
char *buffer = NULL;
472+
ssize_t r = getline(&buffer, &n, file);
473+
474+
if (r > -1) {
475+
// The return value does *not* include the terminating null byte.
476+
// The buffer must be large enough to include it.
477+
clang_analyzer_eval(n > r); // expected-warning{{TRUE}}
478+
}
479+
480+
fclose(file);
481+
free(buffer);
482+
}
483+
484+
485+
void getline_buffer_size_invariant(char *buffer) {
486+
FILE *file = fopen("file.txt", "r");
487+
if (file == NULL) {
488+
return;
489+
}
490+
491+
size_t n = -1;
492+
ssize_t ret = getline(&buffer, &n, file);
493+
if (ret > 0) {
494+
clang_analyzer_eval(ret < n); // expected-warning {{TRUE}}
495+
}
496+
free(buffer);
497+
fclose(file);
498+
}

0 commit comments

Comments
 (0)