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