blob: f7458274d9466ec75d5814ae4da421fa8dd831f0 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2011 The Chromium Authors
[email protected]d8617a62009-10-09 23:52:202// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
brettw6ee6fd62015-06-09 18:05:245#ifndef BASE_POSIX_SAFE_STRERROR_H_
6#define BASE_POSIX_SAFE_STRERROR_H_
[email protected]d8617a62009-10-09 23:52:207
avi9b6f42932015-12-26 22:15:148#include <stddef.h>
9
[email protected]d8617a62009-10-09 23:52:2010#include <string>
11
[email protected]0bea7252011-08-05 15:34:0012#include "base/base_export.h"
[email protected]68a008e82011-05-02 17:54:1413
brettw6ee6fd62015-06-09 18:05:2414namespace base {
15
[email protected]d8617a62009-10-09 23:52:2016// BEFORE using anything from this file, first look at PLOG and friends in
17// logging.h and use them instead if applicable.
18//
19// This file declares safe, portable alternatives to the POSIX strerror()
20// function. strerror() is inherently unsafe in multi-threaded apps and should
21// never be used. Doing so can cause crashes. Additionally, the thread-safe
22// alternative strerror_r varies in semantics across platforms. Use these
23// functions instead.
24
25// Thread-safe strerror function with dependable semantics that never fails.
26// It will write the string form of error "err" to buffer buf of length len.
27// If there is an error calling the OS's strerror_r() function then a message to
28// that effect will be printed into buf, truncating if necessary. The final
29// result is always null-terminated. The value of errno is never changed.
30//
31// Use this instead of strerror_r().
Peter Kasting134ef9af2024-12-28 02:30:0932BASE_EXPORT void safe_strerror_r(int err, char* buf, size_t len);
[email protected]d8617a62009-10-09 23:52:2033
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.
[email protected]0bea7252011-08-05 15:34:0040BASE_EXPORT std::string safe_strerror(int err);
[email protected]d8617a62009-10-09 23:52:2041
brettw6ee6fd62015-06-09 18:05:2442} // namespace base
43
44#endif // BASE_POSIX_SAFE_STRERROR_H_