annotate lib/stdio-write.c @ 425:67815606c5d9 3.0-beta2

Prevent NULL deref in lw_expr_destroy
author lost@l-w.ca
date Sun, 19 Sep 2010 10:27:47 -0600
parents 3c2e1f24cc7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
421
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1 /* POSIX compatible FILE stream write function.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2 Copyright (C) 2008-2010 Free Software Foundation, Inc.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2008.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
8 (at your option) any later version.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
9
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
13 GNU General Public License for more details.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
14
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
17
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
18 #include <config.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
19
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
20 /* Specification. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
21 #include <stdio.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
22
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
23 /* Replace these functions only if module 'sigpipe' is requested. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
24 #if GNULIB_SIGPIPE
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
25
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
26 /* On native Windows platforms, SIGPIPE does not exist. When write() is
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
27 called on a pipe with no readers, WriteFile() fails with error
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
28 GetLastError() = ERROR_NO_DATA, and write() in consequence fails with
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
29 error EINVAL. This write() function is at the basis of the function
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
30 which flushes the buffer of a FILE stream. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
31
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
32 # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
33
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
34 # include <errno.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
35 # include <signal.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
36 # include <io.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
37
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
38 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
39 # include <windows.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
40
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
41 # define CALL_WITH_SIGPIPE_EMULATION(RETTYPE, EXPRESSION, FAILED) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
42 if (ferror (stream)) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
43 return (EXPRESSION); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
44 else \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
45 { \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
46 RETTYPE ret; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
47 SetLastError (0); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
48 ret = (EXPRESSION); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
49 if (FAILED && GetLastError () == ERROR_NO_DATA && ferror (stream)) \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
50 { \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
51 int fd = fileno (stream); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
52 if (fd >= 0 \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
53 && GetFileType ((HANDLE) _get_osfhandle (fd)) == FILE_TYPE_PIPE)\
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
54 { \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
55 /* Try to raise signal SIGPIPE. */ \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
56 raise (SIGPIPE); \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
57 /* If it is currently blocked or ignored, change errno from \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
58 EINVAL to EPIPE. */ \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
59 errno = EPIPE; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
60 } \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
61 } \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
62 return ret; \
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
63 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
64
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
65 # if !REPLACE_PRINTF_POSIX /* avoid collision with printf.c */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
66 # if !DEPENDS_ON_LIBINTL /* avoid collision with intl/printf.c */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
67 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
68 printf (const char *format, ...)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
69 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
70 int retval;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
71 va_list args;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
72
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
73 va_start (args, format);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
74 retval = vfprintf (stdout, format, args);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
75 va_end (args);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
76
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
77 return retval;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
78 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
79 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
80 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
81
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
82 # if !REPLACE_FPRINTF_POSIX /* avoid collision with fprintf.c */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
83 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
84 fprintf (FILE *stream, const char *format, ...)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
85 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
86 int retval;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
87 va_list args;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
88
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
89 va_start (args, format);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
90 retval = vfprintf (stream, format, args);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
91 va_end (args);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
92
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
93 return retval;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
94 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
95 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
96
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
97 # if !REPLACE_VPRINTF_POSIX /* avoid collision with vprintf.c */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
98 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
99 vprintf (const char *format, va_list args)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
100 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
101 return vfprintf (stdout, format, args);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
102 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
103 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
104
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
105 # if !REPLACE_VFPRINTF_POSIX /* avoid collision with vfprintf.c */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
106 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
107 vfprintf (FILE *stream, const char *format, va_list args)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
108 #undef vfprintf
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
109 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
110 CALL_WITH_SIGPIPE_EMULATION (int, vfprintf (stream, format, args), ret == EOF)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
111 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
112 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
113
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
114 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
115 putchar (int c)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
116 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
117 return fputc (c, stdout);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
118 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
119
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
120 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
121 fputc (int c, FILE *stream)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
122 #undef fputc
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
123 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
124 CALL_WITH_SIGPIPE_EMULATION (int, fputc (c, stream), ret == EOF)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
125 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
126
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
127 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
128 fputs (const char *string, FILE *stream)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
129 #undef fputs
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
130 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
131 CALL_WITH_SIGPIPE_EMULATION (int, fputs (string, stream), ret == EOF)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
132 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
133
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
134 int
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
135 puts (const char *string)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
136 #undef puts
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
137 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
138 FILE *stream = stdout;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
139 CALL_WITH_SIGPIPE_EMULATION (int, puts (string), ret == EOF)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
140 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
141
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
142 size_t
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
143 fwrite (const void *ptr, size_t s, size_t n, FILE *stream)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
144 #undef fwrite
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
145 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
146 CALL_WITH_SIGPIPE_EMULATION (size_t, fwrite (ptr, s, n, stream), ret < n)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
147 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
148
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
149 # endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
150 #endif