blob: 2f77d84a1b96df127c867ff7bcd3d31bcdc0081a [file] [log] [blame]
evan@chromium.org3e208892011-05-03 02:54:14 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +09002// 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_SAFE_STRERROR_POSIX_H_
6#define BASE_SAFE_STRERROR_POSIX_H_
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +09007
8#include <string>
9
darin@chromium.orge585bed2011-08-06 00:34:00 +090010#include "base/base_export.h"
evan@chromium.org3e208892011-05-03 02:54:14 +090011
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +090012// BEFORE using anything from this file, first look at PLOG and friends in
13// logging.h and use them instead if applicable.
14//
15// This file declares safe, portable alternatives to the POSIX strerror()
16// function. strerror() is inherently unsafe in multi-threaded apps and should
17// never be used. Doing so can cause crashes. Additionally, the thread-safe
18// alternative strerror_r varies in semantics across platforms. Use these
19// functions instead.
20
21// Thread-safe strerror function with dependable semantics that never fails.
22// It will write the string form of error "err" to buffer buf of length len.
23// If there is an error calling the OS's strerror_r() function then a message to
24// that effect will be printed into buf, truncating if necessary. The final
25// result is always null-terminated. The value of errno is never changed.
26//
27// Use this instead of strerror_r().
darin@chromium.orge585bed2011-08-06 00:34:00 +090028BASE_EXPORT void safe_strerror_r(int err, char *buf, size_t len);
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +090029
30// Calls safe_strerror_r with a buffer of suitable size and returns the result
31// in a C++ string.
32//
33// Use this instead of strerror(). Note though that safe_strerror_r will be
34// more robust in the case of heap corruption errors, since it doesn't need to
35// allocate a string.
darin@chromium.orge585bed2011-08-06 00:34:00 +090036BASE_EXPORT std::string safe_strerror(int err);
tschmelcher@chromium.orgf29a4fc2009-10-10 08:52:20 +090037
38#endif // BASE_SAFE_STRERROR_POSIX_H_