OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include "build/build_config.h" |
5 #include "base/safe_strerror_posix.h" | 6 #include "base/safe_strerror_posix.h" |
6 | 7 |
7 #include <errno.h> | 8 #include <errno.h> |
8 #include <stdio.h> | 9 #include <stdio.h> |
9 #include <string.h> | 10 #include <string.h> |
10 | 11 |
11 #if defined(__GLIBC__) && defined(__GNUC__) | 12 #define USE_HISTORICAL_STRERRO_R (defined(__GLIBC__) || defined(OS_NACL)) |
| 13 |
| 14 #if USE_HISTORICAL_STRERRO_R && defined(__GNUC__) |
12 // GCC will complain about the unused second wrap function unless we tell it | 15 // GCC will complain about the unused second wrap function unless we tell it |
13 // that we meant for them to be potentially unused, which is exactly what this | 16 // that we meant for them to be potentially unused, which is exactly what this |
14 // attribute is for. | 17 // attribute is for. |
15 #define POSSIBLY_UNUSED __attribute__((unused)) | 18 #define POSSIBLY_UNUSED __attribute__((unused)) |
16 #else | 19 #else |
17 #define POSSIBLY_UNUSED | 20 #define POSSIBLY_UNUSED |
18 #endif | 21 #endif |
19 | 22 |
20 #if defined(__GLIBC__) | 23 #if USE_HISTORICAL_STRERRO_R |
21 // glibc has two strerror_r functions: a historical GNU-specific one that | 24 // glibc has two strerror_r functions: a historical GNU-specific one that |
22 // returns type char *, and a POSIX.1-2001 compliant one available since 2.3.4 | 25 // returns type char *, and a POSIX.1-2001 compliant one available since 2.3.4 |
23 // that returns int. This wraps the GNU-specific one. | 26 // that returns int. This wraps the GNU-specific one. |
24 static void POSSIBLY_UNUSED wrap_posix_strerror_r( | 27 static void POSSIBLY_UNUSED wrap_posix_strerror_r( |
25 char *(*strerror_r_ptr)(int, char *, size_t), | 28 char *(*strerror_r_ptr)(int, char *, size_t), |
26 int err, | 29 int err, |
27 char *buf, | 30 char *buf, |
28 size_t len) { | 31 size_t len) { |
29 // GNU version. | 32 // GNU version. |
30 char *rc = (*strerror_r_ptr)(err, buf, len); | 33 char *rc = (*strerror_r_ptr)(err, buf, len); |
31 if (rc != buf) { | 34 if (rc != buf) { |
32 // glibc did not use buf and returned a static string instead. Copy it | 35 // glibc did not use buf and returned a static string instead. Copy it |
33 // into buf. | 36 // into buf. |
34 buf[0] = '\0'; | 37 buf[0] = '\0'; |
35 strncat(buf, rc, len - 1); | 38 strncat(buf, rc, len - 1); |
36 } | 39 } |
37 // The GNU version never fails. Unknown errors get an "unknown error" message. | 40 // The GNU version never fails. Unknown errors get an "unknown error" message. |
38 // The result is always null terminated. | 41 // The result is always null terminated. |
39 } | 42 } |
40 #endif // __GLIBC__ | 43 #endif // USE_HISTORICAL_STRERRO_R |
41 | 44 |
42 // Wrapper for strerror_r functions that implement the POSIX interface. POSIX | 45 // Wrapper for strerror_r functions that implement the POSIX interface. POSIX |
43 // does not define the behaviour for some of the edge cases, so we wrap it to | 46 // does not define the behaviour for some of the edge cases, so we wrap it to |
44 // guarantee that they are handled. This is compiled on all POSIX platforms, but | 47 // guarantee that they are handled. This is compiled on all POSIX platforms, but |
45 // it will only be used on Linux if the POSIX strerror_r implementation is | 48 // it will only be used on Linux if the POSIX strerror_r implementation is |
46 // being used (see below). | 49 // being used (see below). |
47 static void POSSIBLY_UNUSED wrap_posix_strerror_r( | 50 static void POSSIBLY_UNUSED wrap_posix_strerror_r( |
48 int (*strerror_r_ptr)(int, char *, size_t), | 51 int (*strerror_r_ptr)(int, char *, size_t), |
49 int err, | 52 int err, |
50 char *buf, | 53 char *buf, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // static. | 102 // static. |
100 wrap_posix_strerror_r(&strerror_r, err, buf, len); | 103 wrap_posix_strerror_r(&strerror_r, err, buf, len); |
101 } | 104 } |
102 | 105 |
103 std::string safe_strerror(int err) { | 106 std::string safe_strerror(int err) { |
104 const int buffer_size = 256; | 107 const int buffer_size = 256; |
105 char buf[buffer_size]; | 108 char buf[buffer_size]; |
106 safe_strerror_r(err, buf, sizeof(buf)); | 109 safe_strerror_r(err, buf, sizeof(buf)); |
107 return std::string(buf); | 110 return std::string(buf); |
108 } | 111 } |
OLD | NEW |