strerror_r: fix OpenBSD behavior on out-of-range
[gnulib.git] / lib / strerror_r.c
1 /* strerror_r.c --- POSIX compatible system error routine
2
3    Copyright (C) 2010-2011 Free Software Foundation, Inc.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by Bruno Haible <bruno@clisp.org>, 2010.  */
19
20 #include <config.h>
21
22 /* Enable declaration of sys_nerr and sys_errlist in <errno.h> on NetBSD.  */
23 #define _NETBSD_SOURCE 1
24
25 /* Specification.  */
26 #include <string.h>
27
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "strerror-override.h"
33
34 #if (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4, cygwin >= 1.7.9 */
35
36 # define USE_XPG_STRERROR_R 1
37
38 #elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
39
40 /* The system's strerror_r function is OK, except that its third argument
41    is 'int', not 'size_t', or its return type is wrong.  */
42
43 # include <limits.h>
44
45 # define USE_SYSTEM_STRERROR_R 1
46
47 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
48
49 /* Use the system's strerror().  Exclude glibc and cygwin because the
50    system strerror_r has the wrong return type, and cygwin 1.7.9
51    strerror_r clobbers strerror.  */
52 # undef strerror
53
54 # define USE_SYSTEM_STRERROR 1
55
56 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
57
58 /* No locking needed.  */
59
60 /* Get catgets internationalization functions.  */
61 #  if HAVE_CATGETS
62 #   include <nl_types.h>
63 #  endif
64
65 /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
66    Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI).  */
67 #  if defined __hpux || defined __sgi
68 extern int sys_nerr;
69 extern char *sys_errlist[];
70 #  endif
71
72 /* Get sys_nerr on Solaris.  */
73 #  if defined __sun && !defined _LP64
74 extern int sys_nerr;
75 #  endif
76
77 # else
78
79 #  include "glthread/lock.h"
80
81 /* This lock protects the buffer returned by strerror().  We assume that
82    no other uses of strerror() exist in the program.  */
83 gl_lock_define_initialized(static, strerror_lock)
84
85 # endif
86
87 #endif
88
89 /* Copy as much of MSG into BUF as possible, without corrupting errno.
90    Return 0 if MSG fit in BUFLEN, otherwise return ERANGE.  */
91 static int
92 safe_copy (char *buf, size_t buflen, const char *msg)
93 {
94   size_t len = strlen (msg);
95   int ret;
96
97   if (len < buflen)
98     {
99       /* Although POSIX allows memcpy() to corrupt errno, we don't
100          know of any implementation where this is a real problem.  */
101       memcpy (buf, msg, len + 1);
102       ret = 0;
103     }
104   else
105     {
106       memcpy (buf, msg, buflen - 1);
107       buf[buflen - 1] = '\0';
108       ret = ERANGE;
109     }
110   return ret;
111 }
112
113
114 int
115 strerror_r (int errnum, char *buf, size_t buflen)
116 #undef strerror_r
117 {
118   /* Filter this out now, so that rest of this replacement knows that
119      there is room for a non-empty message and trailing NUL.  */
120   if (buflen <= 1)
121     {
122       if (buflen)
123         *buf = '\0';
124       return ERANGE;
125     }
126   *buf = '\0';
127
128   /* Check for gnulib overrides.  */
129   {
130     char const *msg = strerror_override (errnum);
131
132     if (msg)
133       return safe_copy (buf, buflen, msg);
134   }
135
136   {
137     int ret;
138     int saved_errno = errno;
139
140 #if USE_XPG_STRERROR_R
141
142     {
143       extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
144
145       ret = __xpg_strerror_r (errnum, buf, buflen);
146       if (ret < 0)
147         ret = errno;
148       if (!*buf)
149         {
150           /* glibc 2.13 would not touch buf on err, so we have to fall
151              back to GNU strerror_r which always returns a thread-safe
152              untruncated string to (partially) copy into our buf.  */
153           safe_copy (buf, buflen, strerror_r (errnum, buf, buflen));
154         }
155     }
156
157 #elif USE_SYSTEM_STRERROR_R
158
159     if (buflen > INT_MAX)
160       buflen = INT_MAX;
161
162 # ifdef __hpux
163     /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it
164        also fails to change buf on EINVAL.  */
165     {
166       char stackbuf[80];
167
168       if (buflen < sizeof stackbuf)
169         {
170           ret = strerror_r (errnum, stackbuf, sizeof stackbuf);
171           if (ret == 0)
172             ret = safe_copy (buf, buflen, stackbuf);
173         }
174       else
175         ret = strerror_r (errnum, buf, buflen);
176     }
177 # else
178     ret = strerror_r (errnum, buf, buflen);
179
180     /* Some old implementations may return (-1, EINVAL) instead of EINVAL.  */
181     if (ret < 0)
182       ret = errno;
183 # endif
184
185 # ifdef _AIX
186     /* AIX returns 0 rather than ERANGE when truncating strings; try
187        again until we are sure we got the entire string.  */
188     if (!ret && strlen (buf) == buflen - 1)
189       {
190         char stackbuf[STACKBUF_LEN];
191         size_t len;
192         strerror_r (errnum, stackbuf, sizeof stackbuf);
193         len = strlen (stackbuf);
194         /* STACKBUF_LEN should have been large enough.  */
195         if (len + 1 == sizeof stackbuf)
196           abort ();
197         if (buflen <= len)
198           ret = ERANGE;
199       }
200 # else
201     /* Solaris 10 does not populate buf on ERANGE.  OpenBSD 4.7
202        truncates early on ERANGE rather than return a partial integer.
203        We prefer the maximal string.  We set buf[0] earlier, and we
204        know of no implementation that modifies buf to be an
205        unterminated string, so this strlen should be portable in
206        practice (rather than pulling in a safer strnlen).  */
207     if (ret == ERANGE && strlen (buf) < buflen - 1)
208       {
209         char stackbuf[STACKBUF_LEN];
210
211         /* STACKBUF_LEN should have been large enough.  */
212         if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
213           abort ();
214         safe_copy (buf, buflen, stackbuf);
215       }
216 # endif
217
218 #else /* USE_SYSTEM_STRERROR */
219
220     /* Try to do what strerror (errnum) does, but without clobbering the
221        buffer used by strerror().  */
222
223 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
224
225     /* NetBSD:        sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
226                       and <errno.h> above.
227        HP-UX:         sys_nerr, sys_errlist are declared explicitly above.
228        native Win32:  sys_nerr, sys_errlist are declared in <stdlib.h>.
229        Cygwin:        sys_nerr, sys_errlist are declared in <errno.h>.  */
230     if (errnum >= 0 && errnum < sys_nerr)
231       {
232 #  if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
233 #   if defined __NetBSD__
234         nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
235         const char *errmsg =
236           (catd != (nl_catd)-1
237            ? catgets (catd, 1, errnum, sys_errlist[errnum])
238            : sys_errlist[errnum]);
239 #   endif
240 #   if defined __hpux
241         nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
242         const char *errmsg =
243           (catd != (nl_catd)-1
244            ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
245            : sys_errlist[errnum]);
246 #   endif
247 #  else
248         const char *errmsg = sys_errlist[errnum];
249 #  endif
250         if (errmsg == NULL || *errmsg == '\0')
251           ret = EINVAL;
252         else
253           ret = safe_copy (buf, buflen, errmsg);
254 #  if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
255         if (catd != (nl_catd)-1)
256           catclose (catd);
257 #  endif
258       }
259     else
260       ret = EINVAL;
261
262 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
263
264     /* For a valid error number, the system's strerror() function returns
265        a pointer to a not copied string, not to a buffer.  */
266     if (errnum >= 0 && errnum < sys_nerr)
267       {
268         char *errmsg = strerror (errnum);
269
270         if (errmsg == NULL || *errmsg == '\0')
271           ret = EINVAL;
272         else
273           ret = safe_copy (buf, buflen, errmsg);
274       }
275     else
276       ret = EINVAL;
277
278 # else
279
280     gl_lock_lock (strerror_lock);
281
282     {
283       char *errmsg = strerror (errnum);
284
285       /* For invalid error numbers, strerror() on
286            - IRIX 6.5 returns NULL,
287            - HP-UX 11 returns an empty string.  */
288       if (errmsg == NULL || *errmsg == '\0')
289         ret = EINVAL;
290       else
291         ret = safe_copy (buf, buflen, errmsg);
292     }
293
294     gl_lock_unlock (strerror_lock);
295
296 # endif
297
298 #endif
299
300     if (ret == EINVAL && !*buf)
301       snprintf (buf, buflen, "Unknown error %d", errnum);
302
303     errno = saved_errno;
304     return ret;
305   }
306 }