xref: /aosp_15_r20/external/cronet/base/posix/safe_strerror.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_POSIX_SAFE_STRERROR_H_
6*6777b538SAndroid Build Coastguard Worker #define BASE_POSIX_SAFE_STRERROR_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #include <string>
11*6777b538SAndroid Build Coastguard Worker 
12*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h"
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker namespace base {
15*6777b538SAndroid Build Coastguard Worker 
16*6777b538SAndroid Build Coastguard Worker // BEFORE using anything from this file, first look at PLOG and friends in
17*6777b538SAndroid Build Coastguard Worker // logging.h and use them instead if applicable.
18*6777b538SAndroid Build Coastguard Worker //
19*6777b538SAndroid Build Coastguard Worker // This file declares safe, portable alternatives to the POSIX strerror()
20*6777b538SAndroid Build Coastguard Worker // function. strerror() is inherently unsafe in multi-threaded apps and should
21*6777b538SAndroid Build Coastguard Worker // never be used. Doing so can cause crashes. Additionally, the thread-safe
22*6777b538SAndroid Build Coastguard Worker // alternative strerror_r varies in semantics across platforms. Use these
23*6777b538SAndroid Build Coastguard Worker // functions instead.
24*6777b538SAndroid Build Coastguard Worker 
25*6777b538SAndroid Build Coastguard Worker // Thread-safe strerror function with dependable semantics that never fails.
26*6777b538SAndroid Build Coastguard Worker // It will write the string form of error "err" to buffer buf of length len.
27*6777b538SAndroid Build Coastguard Worker // If there is an error calling the OS's strerror_r() function then a message to
28*6777b538SAndroid Build Coastguard Worker // that effect will be printed into buf, truncating if necessary. The final
29*6777b538SAndroid Build Coastguard Worker // result is always null-terminated. The value of errno is never changed.
30*6777b538SAndroid Build Coastguard Worker //
31*6777b538SAndroid Build Coastguard Worker // Use this instead of strerror_r().
32*6777b538SAndroid Build Coastguard Worker BASE_EXPORT void safe_strerror_r(int err, char *buf, size_t len);
33*6777b538SAndroid Build Coastguard Worker 
34*6777b538SAndroid Build Coastguard Worker // Calls safe_strerror_r with a buffer of suitable size and returns the result
35*6777b538SAndroid Build Coastguard Worker // in a C++ string.
36*6777b538SAndroid Build Coastguard Worker //
37*6777b538SAndroid Build Coastguard Worker // Use this instead of strerror(). Note though that safe_strerror_r will be
38*6777b538SAndroid Build Coastguard Worker // more robust in the case of heap corruption errors, since it doesn't need to
39*6777b538SAndroid Build Coastguard Worker // allocate a string.
40*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::string safe_strerror(int err);
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker }  // namespace base
43*6777b538SAndroid Build Coastguard Worker 
44*6777b538SAndroid Build Coastguard Worker #endif  // BASE_POSIX_SAFE_STRERROR_H_
45