Skip to content

Commit 5b9bcca

Browse files
Move invalidation tests to stream-invalidate.c
1 parent c3425b9 commit 5b9bcca

File tree

2 files changed

+51
-85
lines changed

2 files changed

+51
-85
lines changed

clang/test/Analysis/stream-invalidate.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// RUN: -analyzer-checker=debug.ExprInspection
55

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

89
void clang_analyzer_eval(int);
910
void clang_analyzer_dump(int);
@@ -145,3 +146,53 @@ void test_fgetpos() {
145146

146147
fclose(F);
147148
}
149+
150+
void test_fprintf() {
151+
FILE *F1 = tmpfile();
152+
if (!F1)
153+
return;
154+
155+
unsigned a = 42;
156+
char *output = "HELLO";
157+
int r = fprintf(F1, "%s\t%u\n", output, a);
158+
// fprintf does not invalidate any of its input
159+
// 69 is ascii for 'E'
160+
clang_analyzer_dump(a); // expected-warning {{42 S32b}}
161+
clang_analyzer_dump(output[1]); // expected-warning {{69 S32b}}
162+
if (r < 0) {
163+
// Failure
164+
fprintf(F1, "%s\t%u\n", output, a); // expected-warning {{File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior}}
165+
} else {
166+
char buffer[10];
167+
fscanf(F1, "%s", buffer);
168+
if (fseek(F1, 0, SEEK_SET) == 0) {
169+
fprintf(F1, "%s\t%u\n", buffer, a); // ok
170+
}
171+
}
172+
173+
fclose(F1);
174+
}
175+
176+
int test_vfscanf_inner(const char *fmt, ...) {
177+
FILE *F1 = tmpfile();
178+
if (!F1)
179+
return EOF;
180+
181+
va_list ap;
182+
va_start(ap, fmt);
183+
184+
int r = vfscanf(F1, fmt, ap);
185+
186+
fclose(F1);
187+
va_end(ap);
188+
return r;
189+
}
190+
191+
void test_vfscanf() {
192+
int i = 42;
193+
int r = test_vfscanf_inner("%d", &i);
194+
if (r != EOF) {
195+
clang_analyzer_dump(i); // expected-warning {{conj_$}}
196+
// FIXME va_list "hides" the pointer to i
197+
}
198+
}

clang/test/Analysis/stream.c

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

33
#include "Inputs/system-header-simulator.h"
4-
#include "Inputs/system-header-simulator-for-valist.h"
54

65
void clang_analyzer_eval(int);
7-
void clang_analyzer_dump_char(char);
8-
void clang_analyzer_dump_int(int);
96

107
void check_fread(void) {
118
FILE *fp = tmpfile();
@@ -363,85 +360,3 @@ void fflush_on_open_failed_stream(void) {
363360
}
364361
fclose(F);
365362
}
366-
367-
void test_fscanf_escape() {
368-
FILE *F1 = tmpfile();
369-
if (!F1)
370-
return;
371-
372-
int a = 48;
373-
unsigned b = 127;
374-
char buffer[] = "FSCANF"; // 70 83 67 65 78 70
375-
376-
clang_analyzer_dump_int(a); // expected-warning {{48 S32b}}
377-
clang_analyzer_dump_int(b); // expected-warning {{127 S32b}}
378-
clang_analyzer_dump_char(buffer[2]); // expected-warning {{67 S8b}}
379-
380-
int ret = fscanf(F1, "%d %u %s", &a, &b, buffer);
381-
if (ret != EOF) {
382-
clang_analyzer_dump_int(a); // expected-warning {{conj_$}}
383-
clang_analyzer_dump_int(b); // expected-warning {{conj_$}}
384-
clang_analyzer_dump_char(buffer[2]); // expected-warning {{derived_$}}
385-
} else {
386-
clang_analyzer_dump_int(a); // expected-warning {{48 S32b}}
387-
clang_analyzer_dump_int(b); // expected-warning {{127 S32b}}
388-
clang_analyzer_dump_char(buffer[2]); // expected-warning {{67 S8b}}
389-
}
390-
391-
if (ret != EOF) {
392-
char c = fgetc(F1); // ok
393-
}
394-
395-
fclose(F1);
396-
}
397-
398-
void test_fprintf() {
399-
FILE *F1 = tmpfile();
400-
if (!F1)
401-
return;
402-
403-
unsigned a = 42;
404-
char *output = "HELLO";
405-
int r = fprintf(F1, "%s\t%u\n", output, a);
406-
// fprintf does not invalidate any of its input
407-
// 69 is ascii for 'E'
408-
clang_analyzer_dump_int(a); // expected-warning {{42 S32b}}
409-
clang_analyzer_dump_char(output[1]); // expected-warning {{69 S8b}}
410-
if (r < 0) {
411-
// Failure
412-
fprintf(F1, "%s\t%u\n", output, a); // expected-warning {{File position of the stream might be 'indeterminate' after a failed operation. Can cause undefined behavior}}
413-
} else {
414-
char buffer[10];
415-
fscanf(F1, "%s", buffer);
416-
if (fseek(F1, 0, SEEK_SET) == 0) {
417-
fprintf(F1, "%s\t%u\n", buffer, a); // ok
418-
}
419-
}
420-
421-
fclose(F1);
422-
}
423-
424-
425-
int test_vfscanf_inner(const char *fmt, ...) {
426-
FILE *F1 = tmpfile();
427-
if (!F1)
428-
return EOF;
429-
430-
va_list ap;
431-
va_start(ap, fmt);
432-
433-
int r = vfscanf(F1, fmt, ap);
434-
435-
fclose(F1);
436-
va_end(ap);
437-
return r;
438-
}
439-
440-
void test_vfscanf() {
441-
int i = 42;
442-
int r = test_vfscanf_inner("%d", &i);
443-
if (r != EOF) {
444-
clang_analyzer_dump_int(i); // expected-warning {{conj_$}}
445-
// FIXME va_list "hides" the pointer to i
446-
}
447-
}

0 commit comments

Comments
 (0)