Home | History | Annotate | Download | only in base
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/safe_strerror_posix.h"
      6 
      7 #include <errno.h>
      8 #include <stdio.h>
      9 #include <string.h>
     10 
     11 #if defined(__GLIBC__) && defined(__GNUC__)
     12 // 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
     14 // attribute is for.
     15 #define POSSIBLY_UNUSED __attribute__((unused))
     16 #else
     17 #define POSSIBLY_UNUSED
     18 #endif
     19 
     20 #if defined(__GLIBC__)
     21 // 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
     23 // that returns int. This wraps the GNU-specific one.
     24 static void POSSIBLY_UNUSED wrap_posix_strerror_r(
     25     char *(*strerror_r_ptr)(int, char *, size_t),
     26     int err,
     27     char *buf,
     28     size_t len) {
     29   // GNU version.
     30   char *rc = (*strerror_r_ptr)(err, buf, len);
     31   if (rc != buf) {
     32     // glibc did not use buf and returned a static string instead. Copy it
     33     // into buf.
     34     buf[0] = '\0';
     35     strncat(buf, rc, len - 1);
     36   }
     37   // The GNU version never fails. Unknown errors get an "unknown error" message.
     38   // The result is always null terminated.
     39 }
     40 #endif  // __GLIBC__
     41 
     42 // 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
     44 // 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
     46 // being used (see below).
     47 static void POSSIBLY_UNUSED wrap_posix_strerror_r(
     48     int (*strerror_r_ptr)(int, char *, size_t),
     49     int err,
     50     char *buf,
     51     size_t len) {
     52   int old_errno = errno;
     53   // Have to cast since otherwise we get an error if this is the GNU version
     54   // (but in such a scenario this function is never called). Sadly we can't use
     55   // C++-style casts because the appropriate one is reinterpret_cast but it's
     56   // considered illegal to reinterpret_cast a type to itself, so we get an
     57   // error in the opposite case.
     58   int result = (*strerror_r_ptr)(err, buf, len);
     59   if (result == 0) {
     60     // POSIX is vague about whether the string will be terminated, although
     61     // it indirectly implies that typically ERANGE will be returned, instead
     62     // of truncating the string. We play it safe by always terminating the
     63     // string explicitly.
     64     buf[len - 1] = '\0';
     65   } else {
     66     // Error. POSIX is vague about whether the return value is itself a system
     67     // error code or something else. On Linux currently it is -1 and errno is
     68     // set. On BSD-derived systems it is a system error and errno is unchanged.
     69     // We try and detect which case it is so as to put as much useful info as
     70     // we can into our message.
     71     int strerror_error;  // The error encountered in strerror
     72     int new_errno = errno;
     73     if (new_errno != old_errno) {
     74       // errno was changed, so probably the return value is just -1 or something
     75       // else that doesn't provide any info, and errno is the error.
     76       strerror_error = new_errno;
     77     } else {
     78       // Either the error from strerror_r was the same as the previous value, or
     79       // errno wasn't used. Assume the latter.
     80       strerror_error = result;
     81     }
     82     // snprintf truncates and always null-terminates.
     83     snprintf(buf,
     84              len,
     85              "Error %d while retrieving error %d",
     86              strerror_error,
     87              err);
     88   }
     89   errno = old_errno;
     90 }
     91 
     92 void safe_strerror_r(int err, char *buf, size_t len) {
     93   if (buf == NULL || len <= 0) {
     94     return;
     95   }
     96   // If using glibc (i.e., Linux), the compiler will automatically select the
     97   // appropriate overloaded function based on the function type of strerror_r.
     98   // The other one will be elided from the translation unit since both are
     99   // static.
    100   wrap_posix_strerror_r(&strerror_r, err, buf, len);
    101 }
    102 
    103 std::string safe_strerror(int err) {
    104   const int buffer_size = 256;
    105   char buf[buffer_size];
    106   safe_strerror_r(err, buf, sizeof(buf));
    107   return std::string(buf);
    108 }
    109