FreeBSD Bugzilla – Attachment 231246 Details for
Bug 76398
[libc] stdio can lose data in the presence of signals
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Corrected Yar' test
pr76398.c (text/plain), 3.16 KB, created by
Konstantin Belousov
on 2022-01-23 07:04:23 UTC
(
hide
)
Description:
Corrected Yar' test
Filename:
MIME Type:
Creator:
Konstantin Belousov
Created:
2022-01-23 07:04:23 UTC
Size:
3.16 KB
patch
obsolete
>/* > * Initially written by Yar Tikhiy <yar@freebsd.org> in PR 76398. > * Bug fixes and instrumentation by kib@freebsd.org. > */ > >#include <sys/types.h> >#include <sys/socket.h> >#include <sys/stat.h> >#include <sys/wait.h> >#include <err.h> >#include <errno.h> >#include <fcntl.h> >#include <md5.h> >#include <signal.h> >#include <stdio.h> >#include <stdlib.h> >#include <string.h> >#include <unistd.h> > >/* #define CHILD_READER */ >/* #define USE_PIPE */ > >#define NDATA 1000 >#define DELAY 2 > >static void hup(int); >static void setdata(int); >static char *getdata(void); > >int >main(void) >{ > char c, digest0[33], digest[33], *p; > FILE *fp; > int i, s[2], total; > MD5_CTX md5; > pid_t child; > struct sigaction sa; > > MD5Init(&md5); > setdata(NDATA); > for (total = 0; (p = getdata()) != NULL; total += strlen(p)) > MD5Update(&md5, p, strlen(p)); > p = MD5End(&md5, digest0); > printf("True digest is %s, total %d\n", digest0, total); > > sa.sa_handler = hup; > sigemptyset(&sa.sa_mask); > sa.sa_flags = 0; > if (sigaction(SIGHUP, &sa, NULL) == -1) > err(2, "sigaction"); > >#if defined(USE_FILE) > s[0] = open("/tmp/1a.txt", O_RDWR | O_CREAT | O_TRUNC, 0666); > s[1] = open("/tmp/1a.txt", O_RDONLY); >#elif defined(USE_PIPE) > if (pipe(s) < 0) > err(2, "pipe"); >#else > if (socketpair(PF_UNIX, SOCK_STREAM, 0, s) < 0) > err(2, "socketpair"); >#endif > > switch (child = fork()) { > case -1: > err(2, "fork"); > >#ifdef CHILD_READER > default: >#else > case 0: >#endif > if ((fp = fdopen(s[0], "w")) == NULL) > err(2, "fdopen in writer"); > close(s[1]); > setdata(NDATA); > while ((p = getdata())) { > for (; *p;) { >#ifdef STDIO_WRITER > if (fputc(*p, fp) == EOF) { > if (errno == EINTR) > clearerr(fp); > else > err(2, "fputc"); > } else { > p++; > } >#else > unsigned char x; > int r; > > x = *p; > r = write(fileno(fp), &x, 1); > if (r == 0) > errx(2, "write EOF"); > if (r < 0) { > if (errno == EINTR) > continue; > err(2, "write"); > } > p++; >#endif > } > } > fclose(fp); >#ifdef CHILD_READER > waitpid(child, &i, 0); >#endif > break; > >#ifdef CHILD_READER > case 0: >#else > default: >#endif > close(s[0]); > if ((fp = fdopen(s[1], "r")) == NULL) > err(2, "fdopen in reader"); > sleep(DELAY); >#ifdef CHILD_READER > if (kill(getppid(), SIGHUP) == -1) >#else > if (kill(child, SIGHUP) == -1) >#endif > err(2, "kill"); > sleep(DELAY); > MD5Init(&md5); > for (total = 0;;) { >#if defined(STDIO_READER) > i = fgetc(fp); > if (i == EOF) { > if (errno == EINTR) > clearerr(fp); > else > err(2, "fgetc"); > continue; > } >#else > int r; > unsigned char x; > r = read(fileno(fp), &x, 1); > if (r <= 0) > err(2, "read"); > i = x; >#endif > total++; > c = i; > MD5Update(&md5, &c, 1); > if (i == '#') > break; > } > MD5End(&md5, digest); > printf(" Got digest of %s, total %d\n", digest, total); > fclose(fp); > break; > } > return (0); >} > >static void >hup(int signo __unused) >{ >} > >static int ndata, seq; > >static void >setdata(int n) >{ > > ndata = n; > seq = 0; >} > >static char * >getdata(void) >{ > static char databuf[256]; > static char xeof[] = "#"; > > if (seq > ndata) > return (NULL); > if (seq == ndata) { > seq++; > return (xeof); > } > sprintf(databuf, "%08d xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", seq++); > return (databuf); >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 76398
: 231246
Working