safe_strerror.h 1.6 KB

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