summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2018-12-16 15:36:27 +0000
committermillert <>2018-12-16 15:36:27 +0000
commit57187f2609d0b63f047c7c1d9e7d373da20b68c6 (patch)
tree6782d1998c12642d16887cc486733ac1fb21fbef
parent0599cb3332122792b1eb29f7f3f541c967bea459 (diff)
downloadopenbsd-57187f2609d0b63f047c7c1d9e7d373da20b68c6.tar.gz
openbsd-57187f2609d0b63f047c7c1d9e7d373da20b68c6.tar.bz2
openbsd-57187f2609d0b63f047c7c1d9e7d373da20b68c6.zip
Add regress test for bugs in stdio/fread.c rev 1.13 and 1.17
-rw-r--r--src/regress/lib/libc/Makefile4
-rw-r--r--src/regress/lib/libc/fread/Makefile5
-rw-r--r--src/regress/lib/libc/fread/fread.c153
3 files changed, 160 insertions, 2 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile
index 0530008c49..40288cf5f1 100644
--- a/src/regress/lib/libc/Makefile
+++ b/src/regress/lib/libc/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.51 2017/10/15 12:15:30 visa Exp $ 1# $OpenBSD: Makefile,v 1.52 2018/12/16 15:36:27 millert Exp $
2 2
3SUBDIR+= _setjmp 3SUBDIR+= _setjmp
4SUBDIR+= alloca arc4random-fork atexit 4SUBDIR+= alloca arc4random-fork atexit
@@ -6,7 +6,7 @@ SUBDIR+= basename
6SUBDIR+= cephes cxa-atexit 6SUBDIR+= cephes cxa-atexit
7SUBDIR+= db dirname 7SUBDIR+= db dirname
8SUBDIR+= env explicit_bzero 8SUBDIR+= env explicit_bzero
9SUBDIR+= fmemopen fnmatch fpclassify 9SUBDIR+= fmemopen fnmatch fpclassify fread
10SUBDIR+= getaddrinfo getcap getopt_long glob 10SUBDIR+= getaddrinfo getcap getopt_long glob
11SUBDIR+= hsearch 11SUBDIR+= hsearch
12SUBDIR+= ieeefp ifnameindex 12SUBDIR+= ieeefp ifnameindex
diff --git a/src/regress/lib/libc/fread/Makefile b/src/regress/lib/libc/fread/Makefile
new file mode 100644
index 0000000000..fddc3b853a
--- /dev/null
+++ b/src/regress/lib/libc/fread/Makefile
@@ -0,0 +1,5 @@
1# $OpenBSD: Makefile,v 1.1 2018/12/16 15:36:27 millert Exp $
2
3PROG= fread
4
5.include <bsd.regress.mk>
diff --git a/src/regress/lib/libc/fread/fread.c b/src/regress/lib/libc/fread/fread.c
new file mode 100644
index 0000000000..84e0ba0f65
--- /dev/null
+++ b/src/regress/lib/libc/fread/fread.c
@@ -0,0 +1,153 @@
1/*
2 * Copyright (c) 2018 Todd C. Miller <millert@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <sys/socket.h>
18#include <sys/wait.h>
19
20#include <err.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26/*
27 * Test reading from a socket until EOF with multiple writes on
28 * the other end. The send and receive buffer sizes are reduced
29 * to force multiple read(2) and write(2) calls to happen.
30 *
31 * Tests unbuffered, line buffered and fully-buffers.
32 *
33 * This test catches bugs in stdio/fread.c revs 1.13 and 1.17.
34 */
35
36static char test_string[] =
37 "Now is the time for all good men to come to the aid of the party\n"
38 "The quick brown fox jumps over the lazy dog\n"
39 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
40 "Insert test text here..\n";
41
42static char *
43iomode2str(int iomode)
44{
45 switch (iomode) {
46 case _IOFBF:
47 return "fully buffered";
48 case _IOLBF:
49 return "line buffered";
50 case _IONBF:
51 return "unbuffered";
52 default:
53 return "unknown";
54 }
55}
56
57static void
58dochild(int fd)
59{
60 size_t left;
61 ssize_t nwritten;
62 char *ts = test_string;
63
64 left = strlen(test_string);
65 while (left != 0) {
66 nwritten = write(fd, ts, left);
67 if (nwritten == -1)
68 err(1, "write");
69 left -= nwritten;
70 ts += nwritten;
71 }
72 close(fd);
73 _exit(0);
74}
75
76int
77dotest(int iomode, char *iobuf, size_t iolen)
78{
79 char *ts = test_string;
80 size_t nread, total = 0, off = 0;
81 int sv[2], val;
82 char buf[21];
83 pid_t child;
84 FILE *fp;
85
86 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1)
87 err(1, "socketpair");
88 val = 16;
89 if (setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) == -1)
90 err(1, "setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF)");
91 if (setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
92 err(1, "setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF)");
93 if (setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) == -1)
94 err(1, "setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF)");
95 if (setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
96 err(1, "setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF)");
97
98 if ((fp = fdopen(sv[0], "r")) == NULL)
99 err(1, "fdopen");
100
101 setvbuf(fp, iobuf, iomode, iolen);
102
103 switch ((child = fork())) {
104 case -1:
105 err(1, "fork");
106 case 0:
107 close(sv[0]);
108 dochild(sv[1]);
109 default:
110 close(sv[1]);
111 break;
112 }
113
114 while ((nread = fread(buf, 1, sizeof(buf), fp)) != 0) {
115 if (nread > sizeof(buf)) {
116 warnx("%s: max %zu bytes but got %zu",
117 iomode2str(iomode), sizeof(buf), nread);
118 return 1;
119 }
120 if (strncmp(buf, test_string + off, nread) != 0) {
121 warnx("%s: mismatch: expected %.*s, got %.*s",
122 iomode2str(iomode), (int)nread, test_string + off,
123 (int)nread, buf);
124 return 1;
125 }
126 total += nread;
127 off += nread;
128 }
129 if (!feof(fp)) {
130 if (ferror(fp))
131 warn("%s: read error", iomode2str(iomode));
132 else
133 warnx("%s: missing EOF", iomode2str(iomode));
134 return 1;
135 }
136 fclose(fp);
137 waitpid(child, NULL, 0);
138
139 return 0;
140}
141
142int
143main(int argc, char *argv[])
144{
145 char iobuf[4096];
146 int errors = 0;
147
148 errors += dotest(_IOFBF, iobuf, sizeof(iobuf));
149 errors += dotest(_IOLBF, iobuf, sizeof(iobuf));
150 errors += dotest(_IONBF, NULL, 0);
151
152 return errors;
153}