1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "build/build_config.h"
6 #include "base/safe_strerror_posix.h"
12 #define USE_HISTORICAL_STRERRO_R (defined(__GLIBC__) || defined(__BIONIC__) || \
15 #if USE_HISTORICAL_STRERRO_R && defined(__GNUC__)
16 // GCC will complain about the unused second wrap function unless we tell it
17 // that we meant for them to be potentially unused, which is exactly what this
19 #define POSSIBLY_UNUSED __attribute__((unused))
21 #define POSSIBLY_UNUSED
24 #if USE_HISTORICAL_STRERRO_R
25 // glibc has two strerror_r functions: a historical GNU-specific one that
26 // returns type char *, and a POSIX.1-2001 compliant one available since 2.3.4
27 // that returns int. This wraps the GNU-specific one.
28 static void POSSIBLY_UNUSED
wrap_posix_strerror_r(
29 char *(*strerror_r_ptr
)(int, char *, size_t),
34 char *rc
= (*strerror_r_ptr
)(err
, buf
, len
);
36 // glibc did not use buf and returned a static string instead. Copy it
39 strncat(buf
, rc
, len
- 1);
41 // The GNU version never fails. Unknown errors get an "unknown error" message.
42 // The result is always null terminated.
44 #endif // USE_HISTORICAL_STRERRO_R
46 // Wrapper for strerror_r functions that implement the POSIX interface. POSIX
47 // does not define the behaviour for some of the edge cases, so we wrap it to
48 // guarantee that they are handled. This is compiled on all POSIX platforms, but
49 // it will only be used on Linux if the POSIX strerror_r implementation is
50 // being used (see below).
51 static void POSSIBLY_UNUSED
wrap_posix_strerror_r(
52 int (*strerror_r_ptr
)(int, char *, size_t),
56 int old_errno
= errno
;
57 // Have to cast since otherwise we get an error if this is the GNU version
58 // (but in such a scenario this function is never called). Sadly we can't use
59 // C++-style casts because the appropriate one is reinterpret_cast but it's
60 // considered illegal to reinterpret_cast a type to itself, so we get an
61 // error in the opposite case.
62 int result
= (*strerror_r_ptr
)(err
, buf
, len
);
64 // POSIX is vague about whether the string will be terminated, although
65 // it indirectly implies that typically ERANGE will be returned, instead
66 // of truncating the string. We play it safe by always terminating the
70 // Error. POSIX is vague about whether the return value is itself a system
71 // error code or something else. On Linux currently it is -1 and errno is
72 // set. On BSD-derived systems it is a system error and errno is unchanged.
73 // We try and detect which case it is so as to put as much useful info as
74 // we can into our message.
75 int strerror_error
; // The error encountered in strerror
76 int new_errno
= errno
;
77 if (new_errno
!= old_errno
) {
78 // errno was changed, so probably the return value is just -1 or something
79 // else that doesn't provide any info, and errno is the error.
80 strerror_error
= new_errno
;
82 // Either the error from strerror_r was the same as the previous value, or
83 // errno wasn't used. Assume the latter.
84 strerror_error
= result
;
86 // snprintf truncates and always null-terminates.
89 "Error %d while retrieving error %d",
96 void safe_strerror_r(int err
, char *buf
, size_t len
) {
97 if (buf
== NULL
|| len
<= 0) {
100 // If using glibc (i.e., Linux), the compiler will automatically select the
101 // appropriate overloaded function based on the function type of strerror_r.
102 // The other one will be elided from the translation unit since both are
104 wrap_posix_strerror_r(&strerror_r
, err
, buf
, len
);
107 std::string
safe_strerror(int err
) {
108 const int buffer_size
= 256;
109 char buf
[buffer_size
];
110 safe_strerror_r(err
, buf
, sizeof(buf
));
111 return std::string(buf
);