16d528ed9Sopenharmony_ci// Copyright (c) 2011 The Chromium Authors. All rights reserved. 26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 36d528ed9Sopenharmony_ci// found in the LICENSE file. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci#ifndef BASE_POSIX_SAFE_STRERROR_H_ 66d528ed9Sopenharmony_ci#define BASE_POSIX_SAFE_STRERROR_H_ 76d528ed9Sopenharmony_ci 86d528ed9Sopenharmony_ci#include <stddef.h> 96d528ed9Sopenharmony_ci 106d528ed9Sopenharmony_ci#include <string> 116d528ed9Sopenharmony_ci 126d528ed9Sopenharmony_cinamespace base { 136d528ed9Sopenharmony_ci 146d528ed9Sopenharmony_ci// BEFORE using anything from this file, first look at PLOG and friends in 156d528ed9Sopenharmony_ci// logging.h and use them instead if applicable. 166d528ed9Sopenharmony_ci// 176d528ed9Sopenharmony_ci// This file declares safe, portable alternatives to the POSIX strerror() 186d528ed9Sopenharmony_ci// function. strerror() is inherently unsafe in multi-threaded apps and should 196d528ed9Sopenharmony_ci// never be used. Doing so can cause crashes. Additionally, the thread-safe 206d528ed9Sopenharmony_ci// alternative strerror_r varies in semantics across platforms. Use these 216d528ed9Sopenharmony_ci// functions instead. 226d528ed9Sopenharmony_ci 236d528ed9Sopenharmony_ci// Thread-safe strerror function with dependable semantics that never fails. 246d528ed9Sopenharmony_ci// It will write the string form of error "err" to buffer buf of length len. 256d528ed9Sopenharmony_ci// If there is an error calling the OS's strerror_r() function then a message to 266d528ed9Sopenharmony_ci// that effect will be printed into buf, truncating if necessary. The final 276d528ed9Sopenharmony_ci// result is always null-terminated. The value of errno is never changed. 286d528ed9Sopenharmony_ci// 296d528ed9Sopenharmony_ci// Use this instead of strerror_r(). 306d528ed9Sopenharmony_civoid safe_strerror_r(int err, char* buf, size_t len); 316d528ed9Sopenharmony_ci 326d528ed9Sopenharmony_ci// Calls safe_strerror_r with a buffer of suitable size and returns the result 336d528ed9Sopenharmony_ci// in a C++ string. 346d528ed9Sopenharmony_ci// 356d528ed9Sopenharmony_ci// Use this instead of strerror(). Note though that safe_strerror_r will be 366d528ed9Sopenharmony_ci// more robust in the case of heap corruption errors, since it doesn't need to 376d528ed9Sopenharmony_ci// allocate a string. 386d528ed9Sopenharmony_cistd::string safe_strerror(int err); 396d528ed9Sopenharmony_ci 406d528ed9Sopenharmony_ci} // namespace base 416d528ed9Sopenharmony_ci 426d528ed9Sopenharmony_ci#endif // BASE_POSIX_SAFE_STRERROR_H_ 43