Hash :
fd33cbeb
Author :
Date :
2011-06-07T15:41:20
test-perror: relax test to ignore cygwin bug glibc was not the only platform where fprintf(fopen(,"r")) fails to detect errors; cygwin 1.7.9 is another culprit (although it will be fixed for 1.7.10), and I suspect that several other platforms were failing perror2 for the same reason. At this point, there are so many functions affected, and the way to avoid the bug is easy enough (don't pass bogus streams to output-producing functions), that I'm not worried about fixing things other than to document them. * tests/test-perror2.c (main): Relax test on requiring detection of stream errors, and use unbuffered stream. * doc/posix-functions/dprintf.texi (dprintf): Document bug. * doc/posix-functions/fprintf.texi (fprintf): Likewise. * doc/posix-functions/fputc.texi (fputc): Likewise. * doc/posix-functions/fputs.texi (fputs): Likewise. * doc/posix-functions/fputws.texi (fputws): Likewise. * doc/posix-functions/fwprintf.texi (fwprintf): Likewise. * doc/posix-functions/fwrite.texi (fwrite): Likewise. * doc/posix-functions/getopt.texi (getopt): Likewise. * doc/posix-functions/perror.texi (perror): Likewise. * doc/posix-functions/printf.texi (printf): Likewise. * doc/posix-functions/psiginfo.texi (psiginfo): Likewise. * doc/posix-functions/psignal.texi (psignal): Likewise. * doc/posix-functions/putc.texi (putc): Likewise. * doc/posix-functions/putc_unlocked.texi (putc_unlocked): Likewise. * doc/posix-functions/putchar.texi (putchar): Likewise. * doc/posix-functions/putchar_unlocked.texi (putchar_unlocked): Likewise. * doc/posix-functions/puts.texi (puts): Likewise. * doc/posix-functions/putwc.texi (putwc): Likewise. * doc/posix-functions/putwchar.texi (putwchar): Likewise. * doc/posix-functions/vdprintf.texi (vdprintf): Likewise. * doc/posix-functions/vfprintf.texi (vfprintf): Likewise. * doc/posix-functions/vfwprintf.texi (vfwprintf): Likewise. * doc/posix-functions/vprintf.texi (vprintf): Likewise. * doc/posix-functions/vwprintf.texi (vwprintf): Likewise. * doc/posix-functions/wordexp.texi (wordexp): Likewise. * doc/posix-functions/wprintf.texi (wprintf): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com>
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
/* Test of perror() function.
Copyright (C) 2011 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <config.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
/* This test intentionally parses stderr. So, we arrange to have fd 10
(outside the range of interesting fd's during the test) set up to
duplicate the original stderr. */
#define BACKUP_STDERR_FILENO 10
#define ASSERT_STREAM myerr
#include "macros.h"
static FILE *myerr;
#define BASE "test-perror2"
int
main (void)
{
/* We change fd 2 later, so save it in fd 10. */
if (dup2 (STDERR_FILENO, BACKUP_STDERR_FILENO) != BACKUP_STDERR_FILENO
|| (myerr = fdopen (BACKUP_STDERR_FILENO, "w")) == NULL)
return 2;
ASSERT (freopen (BASE ".tmp", "w+", stderr) == stderr);
/* Test that perror does not clobber strerror buffer. */
{
const char *msg1;
const char *msg2;
const char *msg3;
const char *msg4;
char *str1;
char *str2;
char *str3;
char *str4;
msg1 = strerror (ENOENT);
ASSERT (msg1);
str1 = strdup (msg1);
ASSERT (str1);
msg2 = strerror (ERANGE);
ASSERT (msg2);
str2 = strdup (msg2);
ASSERT (str2);
msg3 = strerror (-4);
ASSERT (msg3);
str3 = strdup (msg3);
ASSERT (str3);
msg4 = strerror (1729576);
ASSERT (msg4);
str4 = strdup (msg4);
ASSERT (str4);
errno = EACCES;
perror ("");
errno = -5;
perror ("");
ASSERT (!ferror (stderr));
ASSERT (msg1 == msg2 || msg1 == msg4 || STREQ (msg1, str1));
ASSERT (msg2 == msg4 || STREQ (msg2, str2));
ASSERT (msg3 == msg4 || STREQ (msg3, str3));
ASSERT (STREQ (msg4, str4));
free (str1);
free (str2);
free (str3);
free (str4);
}
/* Test that perror uses the same message as strerror. */
{
int errs[] = { EACCES, 0, -3, };
int i;
for (i = 0; i < SIZEOF (errs); i++)
{
char buf[256];
char *err = strerror (errs[i]);
ASSERT (err);
ASSERT (strlen (err) < sizeof buf);
rewind (stderr);
ASSERT (ftruncate (fileno (stderr), 0) == 0);
errno = errs[i];
perror (NULL);
ASSERT (!ferror (stderr));
rewind (stderr);
ASSERT (fgets (buf, sizeof buf, stderr) == buf);
ASSERT (strstr (buf, err));
}
}
/* Test that perror reports write failure. */
{
ASSERT (freopen (BASE ".tmp", "r", stderr) == stderr);
ASSERT (setvbuf (stderr, NULL, _IONBF, BUFSIZ) == 0);
errno = -1;
ASSERT (!ferror (stderr));
perror (NULL);
#if 0
/* Commented out until cygwin behaves:
http://sourceware.org/ml/newlib/2011/msg00228.html */
ASSERT (errno > 0);
/* Commented out until glibc behaves:
http://sourceware.org/bugzilla/show_bug.cgi?id=12792 */
ASSERT (ferror (stderr));
#endif
}
ASSERT (fclose (stderr) == 0);
ASSERT (remove (BASE ".tmp") == 0);
return 0;
}