File: test-ferror.c

package info (click to toggle)
libfiu 1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: ansic: 2,633; python: 973; makefile: 599; sh: 309
file content (69 lines) | stat: -rw-r--r-- 1,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* Test the handling of FILE * errors. */

#include <errno.h>
#include <fiu-control.h>
#include <fiu.h>
#include <stdio.h>
#include <string.h>

int test(const char *prefix)
{
	FILE *fp = fopen("/dev/zero", "r");

	unsigned char buf[1024];
	ssize_t r;

	fiu_enable("posix/stdio/rw/fread", 1, (void *)EIO, 0);

	r = fread(buf, 1, 1024, fp);

	fiu_disable("posix/stdio/rw/fread");

	if (r != 0) {
		printf("%s: fread() succeeded, should have failed\n", prefix);
		return -1;
	}

	if (errno != EIO) {
		printf("%s: errno not set appropriately: ", prefix);
		printf("errno = %d / %s, expected EIO\n", errno,
		       strerror(errno));
		return -1;
	}

	if (ferror(fp) == 0) {
		printf(
		    "%s: ferror() said there was no failure, but there was\n",
		    prefix);
		return -1;
	}

	clearerr(fp);

	if (ferror(fp) != 0) {
		printf("%s: clearerr(), ferror() said there were failures\n",
		       prefix);
		return -1;
	}

	fclose(fp);
	// Unfortunately we can't easily test after fclose() has been called,
	// because it's impossible to distinguish between a libfiu failure
	// from a libc error (which would appear due to an operation on a
	// closed file). To make things worse, some versions of glibc make
	// ferror() return an error on closed files, but others work.

	return 0;
}

int main(void)
{
	// Run the test many times, to stress structure reuse a bit. This is
	// not as thorough but does exercise some bugs we've had, such as
	// forgetting to decrement the recursion counter.
	char prefix[8];
	for (int i = 0; i < 200; i++) {
		snprintf(prefix, 8, "%2d", i);
		test(prefix);
	}
}