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