blob: 48e9db97ec006ecdd90ace56673f49646a0da5f5 [file] [log] [blame]
Scott Graham66962112018-06-08 12:42:08 -07001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
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 BASE_POSIX_SAFE_STRERROR_H_
6#define BASE_POSIX_SAFE_STRERROR_H_
7
8#include <stddef.h>
9
10#include <string>
11
Scott Graham66962112018-06-08 12:42:08 -070012namespace base {
13
14// BEFORE using anything from this file, first look at PLOG and friends in
15// logging.h and use them instead if applicable.
16//
17// This file declares safe, portable alternatives to the POSIX strerror()
18// function. strerror() is inherently unsafe in multi-threaded apps and should
19// never be used. Doing so can cause crashes. Additionally, the thread-safe
20// alternative strerror_r varies in semantics across platforms. Use these
21// functions instead.
22
23// Thread-safe strerror function with dependable semantics that never fails.
24// It will write the string form of error "err" to buffer buf of length len.
25// If there is an error calling the OS's strerror_r() function then a message to
26// that effect will be printed into buf, truncating if necessary. The final
27// result is always null-terminated. The value of errno is never changed.
28//
29// Use this instead of strerror_r().
Scott Graham98cd3ca2018-06-14 22:26:55 -070030void safe_strerror_r(int err, char* buf, size_t len);
Scott Graham66962112018-06-08 12:42:08 -070031
32// Calls safe_strerror_r with a buffer of suitable size and returns the result
33// in a C++ string.
34//
35// Use this instead of strerror(). Note though that safe_strerror_r will be
36// more robust in the case of heap corruption errors, since it doesn't need to
37// allocate a string.
Scott Graham44598072018-06-14 22:01:37 -070038std::string safe_strerror(int err);
Scott Graham66962112018-06-08 12:42:08 -070039
40} // namespace base
41
42#endif // BASE_POSIX_SAFE_STRERROR_H_