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.
4
5 #include "base/posix/safe_strerror.h"
6
7 #include <errno.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "build/build_config.h"
12
13 namespace base {
14
15 #if defined(__GLIBC__) || defined(OS_NACL)
16 #define USE_HISTORICAL_STRERRO_R 1
17 // Post-L versions of bionic define the GNU-specific strerror_r if _GNU_SOURCE
18 // is defined, but the symbol is renamed to __gnu_strerror_r which only exists
19 // on those later versions. For parity, add the same condition as bionic.
20 #elif defined(__BIONIC__) && defined(_GNU_SOURCE) && __ANDROID_API__ >= 23
21 #define USE_HISTORICAL_STRERRO_R 1
22 #else
23 #define USE_HISTORICAL_STRERRO_R 0
24 #endif
25
26 #if USE_HISTORICAL_STRERRO_R && defined(__GNUC__)
27 // GCC will complain about the unused second wrap function unless we tell it
28 // that we meant for them to be potentially unused, which is exactly what this
29 // attribute is for.
30 #define POSSIBLY_UNUSED __attribute__((unused))
31 #else
32 #define POSSIBLY_UNUSED
33 #endif
34
35 #if USE_HISTORICAL_STRERRO_R
36 // glibc has two strerror_r functions: a historical GNU-specific one that
37 // returns type char *, and a POSIX.1-2001 compliant one available since 2.3.4
38 // that returns int. This wraps the GNU-specific one.
wrap_posix_strerror_r(char * (* strerror_r_ptr)(int,char *,size_t),int err,char * buf,size_t len)39 static void POSSIBLY_UNUSED wrap_posix_strerror_r(
40 char *(*strerror_r_ptr)(int, char *, size_t),
41 int err,
42 char *buf,
43 size_t len) {
44 // GNU version.
45 char *rc = (*strerror_r_ptr)(err, buf, len);
46 if (rc != buf) {
47 // glibc did not use buf and returned a static string instead. Copy it
48 // into buf.
49 buf[0] = '\0';
50 strncat(buf, rc, len - 1);
51 }
52 // The GNU version never fails. Unknown errors get an "unknown error" message.
53 // The result is always null terminated.
54 }
55 #endif // USE_HISTORICAL_STRERRO_R
56
57 // Wrapper for strerror_r functions that implement the POSIX interface. POSIX
58 // does not define the behaviour for some of the edge cases, so we wrap it to
59 // guarantee that they are handled. This is compiled on all POSIX platforms, but
60 // it will only be used on Linux if the POSIX strerror_r implementation is
61 // being used (see below).
wrap_posix_strerror_r(int (* strerror_r_ptr)(int,char *,size_t),int err,char * buf,size_t len)62 static void POSSIBLY_UNUSED wrap_posix_strerror_r(
63 int (*strerror_r_ptr)(int, char *, size_t),
64 int err,
65 char *buf,
66 size_t len) {
67 int old_errno = errno;
68 // Have to cast since otherwise we get an error if this is the GNU version
69 // (but in such a scenario this function is never called). Sadly we can't use
70 // C++-style casts because the appropriate one is reinterpret_cast but it's
71 // considered illegal to reinterpret_cast a type to itself, so we get an
72 // error in the opposite case.
73 int result = (*strerror_r_ptr)(err, buf, len);
74 if (result == 0) {
75 // POSIX is vague about whether the string will be terminated, although
76 // it indirectly implies that typically ERANGE will be returned, instead
77 // of truncating the string. We play it safe by always terminating the
78 // string explicitly.
79 buf[len - 1] = '\0';
80 } else {
81 // Error. POSIX is vague about whether the return value is itself a system
82 // error code or something else. On Linux currently it is -1 and errno is
83 // set. On BSD-derived systems it is a system error and errno is unchanged.
84 // We try and detect which case it is so as to put as much useful info as
85 // we can into our message.
86 int strerror_error; // The error encountered in strerror
87 int new_errno = errno;
88 if (new_errno != old_errno) {
89 // errno was changed, so probably the return value is just -1 or something
90 // else that doesn't provide any info, and errno is the error.
91 strerror_error = new_errno;
92 } else {
93 // Either the error from strerror_r was the same as the previous value, or
94 // errno wasn't used. Assume the latter.
95 strerror_error = result;
96 }
97 // snprintf truncates and always null-terminates.
98 snprintf(buf,
99 len,
100 "Error %d while retrieving error %d",
101 strerror_error,
102 err);
103 }
104 errno = old_errno;
105 }
106
safe_strerror_r(int err,char * buf,size_t len)107 void safe_strerror_r(int err, char *buf, size_t len) {
108 if (buf == nullptr || len <= 0) {
109 return;
110 }
111 // If using glibc (i.e., Linux), the compiler will automatically select the
112 // appropriate overloaded function based on the function type of strerror_r.
113 // The other one will be elided from the translation unit since both are
114 // static.
115 wrap_posix_strerror_r(&strerror_r, err, buf, len);
116 }
117
safe_strerror(int err)118 std::string safe_strerror(int err) {
119 const int buffer_size = 256;
120 char buf[buffer_size];
121 safe_strerror_r(err, buf, sizeof(buf));
122 return std::string(buf);
123 }
124
125 } // namespace base
126