/src/gettext-0.26/gettext-tools/libgettextpo/strerror_r.c
Line | Count | Source |
1 | | /* strerror_r.c --- POSIX compatible system error routine |
2 | | |
3 | | Copyright (C) 2010-2025 Free Software Foundation, Inc. |
4 | | |
5 | | This file is free software: you can redistribute it and/or modify |
6 | | it under the terms of the GNU Lesser General Public License as |
7 | | published by the Free Software Foundation; either version 2.1 of the |
8 | | License, or (at your option) any later version. |
9 | | |
10 | | This file 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 Lesser General Public License for more details. |
14 | | |
15 | | You should have received a copy of the GNU Lesser General Public License |
16 | | along with this program. If not, see <https://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 | | #if !HAVE_SNPRINTF |
32 | | # include <stdarg.h> |
33 | | #endif |
34 | | |
35 | | #include "strerror-override.h" |
36 | | |
37 | | #if STRERROR_R_CHAR_P && !defined _AIX |
38 | | |
39 | | # if HAVE___XPG_STRERROR_R |
40 | | _GL_EXTERN_C int __xpg_strerror_r (int errnum, char *buf, size_t buflen); |
41 | | # endif |
42 | | |
43 | | #elif HAVE_DECL_STRERROR_R |
44 | | |
45 | | /* The system's strerror_r function's API is OK, except that its third argument |
46 | | is 'int', not 'size_t', or its return type is wrong. */ |
47 | | |
48 | | # include <limits.h> |
49 | | |
50 | | #else |
51 | | |
52 | | /* Use the system's strerror(). Exclude glibc and cygwin because the |
53 | | system strerror_r has the wrong return type, and cygwin 1.7.9 |
54 | | strerror_r clobbers strerror. */ |
55 | | # undef strerror |
56 | | |
57 | | # if defined __NetBSD__ || defined __hpux || (defined _WIN32 && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__ |
58 | | |
59 | | /* No locking needed. */ |
60 | | |
61 | | /* Get catgets internationalization functions. */ |
62 | | # if HAVE_CATGETS |
63 | | # include <nl_types.h> |
64 | | # endif |
65 | | |
66 | | #ifdef __cplusplus |
67 | | extern "C" { |
68 | | #endif |
69 | | |
70 | | /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode). |
71 | | Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI). */ |
72 | | # if defined __hpux || defined __sgi |
73 | | extern int sys_nerr; |
74 | | extern char *sys_errlist[]; |
75 | | # endif |
76 | | |
77 | | /* Get sys_nerr on Solaris. */ |
78 | | # if defined __sun && !defined _LP64 |
79 | | extern int sys_nerr; |
80 | | # endif |
81 | | |
82 | | #ifdef __cplusplus |
83 | | } |
84 | | #endif |
85 | | |
86 | | # else |
87 | | |
88 | | # include "glthread/lock.h" |
89 | | |
90 | | /* This lock protects the buffer returned by strerror(). We assume that |
91 | | no other uses of strerror() exist in the program. */ |
92 | | gl_lock_define_initialized(static, strerror_lock) |
93 | | |
94 | | # endif |
95 | | |
96 | | #endif |
97 | | |
98 | | /* On MSVC, there is no snprintf() function, just a _snprintf(). |
99 | | It is of lower quality, but sufficient for the simple use here. |
100 | | We only have to make sure to NUL terminate the result (_snprintf |
101 | | does not NUL terminate, like strncpy). */ |
102 | | #if !HAVE_SNPRINTF |
103 | | static int |
104 | | local_snprintf (char *buf, size_t buflen, const char *format, ...) |
105 | | { |
106 | | va_list args; |
107 | | int result; |
108 | | |
109 | | va_start (args, format); |
110 | | result = _vsnprintf (buf, buflen, format, args); |
111 | | va_end (args); |
112 | | if (buflen > 0 && (result < 0 || result >= buflen)) |
113 | | buf[buflen - 1] = '\0'; |
114 | | return result; |
115 | | } |
116 | | # undef snprintf |
117 | | # define snprintf local_snprintf |
118 | | #endif |
119 | | |
120 | | /* Copy as much of MSG into BUF as possible, without corrupting errno. |
121 | | Return 0 if MSG fit in BUFLEN, otherwise return ERANGE. */ |
122 | | static int |
123 | | safe_copy (char *buf, size_t buflen, const char *msg) |
124 | 0 | { |
125 | 0 | size_t len = strlen (msg); |
126 | 0 | size_t moved = len < buflen ? len : buflen - 1; |
127 | | |
128 | | /* Although POSIX lets memmove corrupt errno, we don't |
129 | | know of any implementation where this is a real problem. */ |
130 | 0 | memmove (buf, msg, moved); |
131 | 0 | buf[moved] = '\0'; |
132 | 0 | return len < buflen ? 0 : ERANGE; |
133 | 0 | } |
134 | | |
135 | | |
136 | | int |
137 | | strerror_r (int errnum, char *buf, size_t buflen) |
138 | | #undef strerror_r |
139 | 0 | { |
140 | | /* Filter this out now, so that rest of this replacement knows that |
141 | | there is room for a non-empty message and trailing NUL. */ |
142 | 0 | if (buflen <= 1) |
143 | 0 | { |
144 | 0 | if (buflen) |
145 | 0 | *buf = '\0'; |
146 | 0 | return ERANGE; |
147 | 0 | } |
148 | 0 | *buf = '\0'; |
149 | | |
150 | | /* Check for gnulib overrides. */ |
151 | 0 | { |
152 | 0 | char const *msg = strerror_override (errnum); |
153 | |
|
154 | 0 | if (msg) |
155 | 0 | return safe_copy (buf, buflen, msg); |
156 | 0 | } |
157 | | |
158 | 0 | { |
159 | 0 | int ret; |
160 | 0 | int saved_errno = errno; |
161 | | |
162 | | /* Due to the '#undef strerror_r' above, on AIX, we're always using |
163 | | the POSIX-compatible strerror_r function, regardless whether |
164 | | _LINUX_SOURCE_COMPAT is defined or not. */ |
165 | 0 | #if STRERROR_R_CHAR_P && !defined _AIX |
166 | |
|
167 | 0 | { |
168 | 0 | ret = 0; |
169 | |
|
170 | 0 | # if HAVE___XPG_STRERROR_R |
171 | 0 | ret = __xpg_strerror_r (errnum, buf, buflen); |
172 | | /* ret is 0 upon success, or EINVAL or ERANGE upon failure. */ |
173 | 0 | # endif |
174 | |
|
175 | 0 | if (!*buf) |
176 | 0 | { |
177 | | /* glibc 2.13 ... 2.34 (at least) don't touch buf upon failure. |
178 | | Therefore we have to fall back to strerror_r which, for valid |
179 | | errnum, returns a thread-safe untruncated string. For invalid |
180 | | errnum, though, it returns a truncated string, which does not |
181 | | allow us to determine whether to return ERANGE or 0. Thus we |
182 | | need to pass a sufficiently large buffer. */ |
183 | 0 | char stackbuf[80]; |
184 | 0 | char *errstring = strerror_r (errnum, stackbuf, sizeof stackbuf); |
185 | 0 | ret = errstring ? safe_copy (buf, buflen, errstring) : errno; |
186 | 0 | } |
187 | 0 | } |
188 | |
|
189 | | #elif HAVE_DECL_STRERROR_R |
190 | | |
191 | | if (buflen > INT_MAX) |
192 | | buflen = INT_MAX; |
193 | | |
194 | | # ifdef __hpux |
195 | | /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it |
196 | | also fails to change buf on EINVAL. */ |
197 | | { |
198 | | char stackbuf[80]; |
199 | | |
200 | | if (buflen < sizeof stackbuf) |
201 | | { |
202 | | ret = strerror_r (errnum, stackbuf, sizeof stackbuf); |
203 | | if (ret == 0) |
204 | | ret = safe_copy (buf, buflen, stackbuf); |
205 | | } |
206 | | else |
207 | | ret = strerror_r (errnum, buf, buflen); |
208 | | } |
209 | | # else |
210 | | ret = strerror_r (errnum, buf, buflen); |
211 | | |
212 | | /* Some old implementations may return (-1, EINVAL) instead of EINVAL. |
213 | | But on Haiku, valid error numbers are negative. */ |
214 | | # if !defined __HAIKU__ |
215 | | if (ret < 0) |
216 | | ret = errno; |
217 | | # endif |
218 | | # endif |
219 | | |
220 | | # if defined _AIX || defined __HAIKU__ |
221 | | /* AIX and Haiku return 0 rather than ERANGE when truncating strings; try |
222 | | again until we are sure we got the entire string. */ |
223 | | if (!ret && strlen (buf) == buflen - 1) |
224 | | { |
225 | | char stackbuf[STACKBUF_LEN]; |
226 | | size_t len; |
227 | | strerror_r (errnum, stackbuf, sizeof stackbuf); |
228 | | len = strlen (stackbuf); |
229 | | /* STACKBUF_LEN should have been large enough. */ |
230 | | if (len + 1 == sizeof stackbuf) |
231 | | abort (); |
232 | | if (buflen <= len) |
233 | | ret = ERANGE; |
234 | | } |
235 | | # else |
236 | | /* Solaris 10 does not populate buf on ERANGE. OpenBSD 4.7 |
237 | | truncates early on ERANGE rather than return a partial integer. |
238 | | We prefer the maximal string. We set buf[0] earlier, and we |
239 | | know of no implementation that modifies buf to be an |
240 | | unterminated string, so this strlen should be portable in |
241 | | practice (rather than pulling in a safer strnlen). */ |
242 | | if (ret == ERANGE && strlen (buf) < buflen - 1) |
243 | | { |
244 | | char stackbuf[STACKBUF_LEN]; |
245 | | |
246 | | /* STACKBUF_LEN should have been large enough. */ |
247 | | if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE) |
248 | | abort (); |
249 | | safe_copy (buf, buflen, stackbuf); |
250 | | } |
251 | | # endif |
252 | | |
253 | | #else /* strerror_r is not declared. */ |
254 | | |
255 | | /* Try to do what strerror (errnum) does, but without clobbering the |
256 | | buffer used by strerror(). */ |
257 | | |
258 | | # if defined __NetBSD__ || defined __hpux || (defined _WIN32 && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Windows, Cygwin */ |
259 | | |
260 | | /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE |
261 | | and <errno.h> above. |
262 | | HP-UX: sys_nerr, sys_errlist are declared explicitly above. |
263 | | native Windows: sys_nerr, sys_errlist are declared in <stdlib.h>. |
264 | | Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */ |
265 | | if (errnum >= 0 && errnum < sys_nerr) |
266 | | { |
267 | | # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux) |
268 | | # if defined __NetBSD__ |
269 | | nl_catd catd = catopen ("libc", NL_CAT_LOCALE); |
270 | | const char *errmsg = |
271 | | (catd != (nl_catd)-1 |
272 | | ? catgets (catd, 1, errnum, sys_errlist[errnum]) |
273 | | : sys_errlist[errnum]); |
274 | | # endif |
275 | | # if defined __hpux |
276 | | nl_catd catd = catopen ("perror", NL_CAT_LOCALE); |
277 | | const char *errmsg = |
278 | | (catd != (nl_catd)-1 |
279 | | ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum]) |
280 | | : sys_errlist[errnum]); |
281 | | # endif |
282 | | # else |
283 | | const char *errmsg = sys_errlist[errnum]; |
284 | | # endif |
285 | | if (errmsg == NULL || *errmsg == '\0') |
286 | | ret = EINVAL; |
287 | | else |
288 | | ret = safe_copy (buf, buflen, errmsg); |
289 | | # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux) |
290 | | if (catd != (nl_catd)-1) |
291 | | catclose (catd); |
292 | | # endif |
293 | | } |
294 | | else |
295 | | ret = EINVAL; |
296 | | |
297 | | # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */ |
298 | | |
299 | | /* For a valid error number, the system's strerror() function returns |
300 | | a pointer to a not copied string, not to a buffer. */ |
301 | | if (errnum >= 0 && errnum < sys_nerr) |
302 | | { |
303 | | char *errmsg = strerror (errnum); |
304 | | |
305 | | if (errmsg == NULL || *errmsg == '\0') |
306 | | ret = EINVAL; |
307 | | else |
308 | | ret = safe_copy (buf, buflen, errmsg); |
309 | | } |
310 | | else |
311 | | ret = EINVAL; |
312 | | |
313 | | # else |
314 | | |
315 | | gl_lock_lock (strerror_lock); |
316 | | |
317 | | { |
318 | | char *errmsg = strerror (errnum); |
319 | | |
320 | | /* For invalid error numbers, strerror() on |
321 | | - IRIX 6.5 returns NULL, |
322 | | - HP-UX 11 returns an empty string. */ |
323 | | if (errmsg == NULL || *errmsg == '\0') |
324 | | ret = EINVAL; |
325 | | else |
326 | | ret = safe_copy (buf, buflen, errmsg); |
327 | | } |
328 | | |
329 | | gl_lock_unlock (strerror_lock); |
330 | | |
331 | | # endif |
332 | | |
333 | | #endif |
334 | |
|
335 | | #if defined _WIN32 && !defined __CYGWIN__ |
336 | | /* MSVC 14 defines names for many error codes in the range 100..140, |
337 | | but _sys_errlist contains strings only for the error codes |
338 | | < _sys_nerr = 43. */ |
339 | | if (ret == EINVAL) |
340 | | { |
341 | | const char *errmsg; |
342 | | |
343 | | switch (errnum) |
344 | | { |
345 | | case 100 /* EADDRINUSE */: |
346 | | errmsg = "Address already in use"; |
347 | | break; |
348 | | case 101 /* EADDRNOTAVAIL */: |
349 | | errmsg = "Cannot assign requested address"; |
350 | | break; |
351 | | case 102 /* EAFNOSUPPORT */: |
352 | | errmsg = "Address family not supported by protocol"; |
353 | | break; |
354 | | case 103 /* EALREADY */: |
355 | | errmsg = "Operation already in progress"; |
356 | | break; |
357 | | case 105 /* ECANCELED */: |
358 | | errmsg = "Operation canceled"; |
359 | | break; |
360 | | case 106 /* ECONNABORTED */: |
361 | | errmsg = "Software caused connection abort"; |
362 | | break; |
363 | | case 107 /* ECONNREFUSED */: |
364 | | errmsg = "Connection refused"; |
365 | | break; |
366 | | case 108 /* ECONNRESET */: |
367 | | errmsg = "Connection reset by peer"; |
368 | | break; |
369 | | case 109 /* EDESTADDRREQ */: |
370 | | errmsg = "Destination address required"; |
371 | | break; |
372 | | case 110 /* EHOSTUNREACH */: |
373 | | errmsg = "No route to host"; |
374 | | break; |
375 | | case 112 /* EINPROGRESS */: |
376 | | errmsg = "Operation now in progress"; |
377 | | break; |
378 | | case 113 /* EISCONN */: |
379 | | errmsg = "Transport endpoint is already connected"; |
380 | | break; |
381 | | case 114 /* ELOOP */: |
382 | | errmsg = "Too many levels of symbolic links"; |
383 | | break; |
384 | | case 115 /* EMSGSIZE */: |
385 | | errmsg = "Message too long"; |
386 | | break; |
387 | | case 116 /* ENETDOWN */: |
388 | | errmsg = "Network is down"; |
389 | | break; |
390 | | case 117 /* ENETRESET */: |
391 | | errmsg = "Network dropped connection on reset"; |
392 | | break; |
393 | | case 118 /* ENETUNREACH */: |
394 | | errmsg = "Network is unreachable"; |
395 | | break; |
396 | | case 119 /* ENOBUFS */: |
397 | | errmsg = "No buffer space available"; |
398 | | break; |
399 | | case 123 /* ENOPROTOOPT */: |
400 | | errmsg = "Protocol not available"; |
401 | | break; |
402 | | case 126 /* ENOTCONN */: |
403 | | errmsg = "Transport endpoint is not connected"; |
404 | | break; |
405 | | case 128 /* ENOTSOCK */: |
406 | | errmsg = "Socket operation on non-socket"; |
407 | | break; |
408 | | case 129 /* ENOTSUP */: |
409 | | errmsg = "Not supported"; |
410 | | break; |
411 | | case 130 /* EOPNOTSUPP */: |
412 | | errmsg = "Operation not supported"; |
413 | | break; |
414 | | case 132 /* EOVERFLOW */: |
415 | | errmsg = "Value too large for defined data type"; |
416 | | break; |
417 | | case 133 /* EOWNERDEAD */: |
418 | | errmsg = "Owner died"; |
419 | | break; |
420 | | case 134 /* EPROTO */: |
421 | | errmsg = "Protocol error"; |
422 | | break; |
423 | | case 135 /* EPROTONOSUPPORT */: |
424 | | errmsg = "Protocol not supported"; |
425 | | break; |
426 | | case 136 /* EPROTOTYPE */: |
427 | | errmsg = "Protocol wrong type for socket"; |
428 | | break; |
429 | | case 138 /* ETIMEDOUT */: |
430 | | errmsg = "Connection timed out"; |
431 | | break; |
432 | | case 140 /* EWOULDBLOCK */: |
433 | | errmsg = "Operation would block"; |
434 | | break; |
435 | | default: |
436 | | errmsg = NULL; |
437 | | break; |
438 | | } |
439 | | if (errmsg != NULL) |
440 | | ret = safe_copy (buf, buflen, errmsg); |
441 | | } |
442 | | #endif |
443 | |
|
444 | 0 | if (ret == EINVAL && !*buf) |
445 | 0 | { |
446 | | /* gcc 14 produces a |
447 | | "warning: 'Unknown error ' directive output truncated |
448 | | writing 14 bytes into a region of size 2" |
449 | | Thanks for the warning, but here the truncation is intentional. */ |
450 | | #if _GL_GNUC_PREREQ (7, 1) |
451 | | # pragma GCC diagnostic ignored "-Wformat-truncation" |
452 | | #endif |
453 | | #if defined __HAIKU__ |
454 | | /* For consistency with perror(). */ |
455 | | snprintf (buf, buflen, "Unknown Application Error (%d)", errnum); |
456 | | #else |
457 | 0 | snprintf (buf, buflen, "Unknown error %d", errnum); |
458 | 0 | #endif |
459 | 0 | } |
460 | |
|
461 | | errno = saved_errno; |
462 | 0 | return ret; |
463 | 0 | } |
464 | 0 | } |