LCOV - code coverage report
Current view: top level - common - errno_to_string.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core c2b6f1024d3a Lines: 4 6 66.7 %
Date: 2019-05-16 09:13:18 Functions: 1 1 100.0 %
Branches: 3 8 37.5 %

           Branch data     Line data    Source code
       1                 :            : /** @file errno_to_string.cc
       2                 :            :  * @brief Convert errno value to std::string, thread-safely if possible
       3                 :            :  */
       4                 :            : /* Copyright (C) 2014,2015,2016 Olly Betts
       5                 :            :  *
       6                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       7                 :            :  * of this software and associated documentation files (the "Software"), to
       8                 :            :  * deal in the Software without restriction, including without limitation the
       9                 :            :  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      10                 :            :  * sell copies of the Software, and to permit persons to whom the Software is
      11                 :            :  * furnished to do so, subject to the following conditions:
      12                 :            :  *
      13                 :            :  * The above copyright notice and this permission notice shall be included in
      14                 :            :  * all copies or substantial portions of the Software.
      15                 :            :  *
      16                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      17                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      18                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      19                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      20                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      21                 :            :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
      22                 :            :  * IN THE SOFTWARE.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <config.h>
      26                 :            : 
      27                 :            : #include "errno_to_string.h"
      28                 :            : 
      29                 :            : // <cstring> doesn't give us strerror_r() with Sun C++ 5.9.
      30                 :            : #include <string.h>
      31                 :            : #if defined HAVE__SYS_ERRLIST_AND__SYS_NERR || \
      32                 :            :     defined HAVE_SYS_ERRLIST_AND_SYS_NERR
      33                 :            : # include <stdio.h>
      34                 :            : // Under mingw, these are in stdlib.h.
      35                 :            : # include <stdlib.h>
      36                 :            : #endif
      37                 :            : 
      38                 :            : #include "str.h"
      39                 :            : 
      40                 :            : using namespace std;
      41                 :            : 
      42                 :            : void
      43                 :         12 : errno_to_string(int e, string & s) {
      44                 :            : #if defined HAVE__SYS_ERRLIST_AND__SYS_NERR
      45 [ +  - ][ +  - ]:         12 :     if (e >= 0 && e < _sys_nerr && _sys_errlist[e]) {
                 [ +  - ]
      46                 :         12 :         s += _sys_errlist[e];
      47                 :            :     } else {
      48                 :          0 :         s += "Unknown error ";
      49         [ #  # ]:          0 :         s += str(e);
      50                 :            :     }
      51                 :            : #elif defined HAVE_SYS_ERRLIST_AND_SYS_NERR
      52                 :            :     if (e >= 0 && e < sys_nerr && sys_errlist[e]) {
      53                 :            :         s += sys_errlist[e];
      54                 :            :     } else {
      55                 :            :         s += "Unknown error ";
      56                 :            :         s += str(e);
      57                 :            :     }
      58                 :            : #elif defined HAVE_STRERROR_R
      59                 :            :     // Actual longest on Linux in English is EILSEQ which needs 50 bytes.
      60                 :            :     char buf[128];
      61                 :            : # ifdef STRERROR_R_CHAR_P
      62                 :            :     // Returns char* containing string.
      63                 :            :     s += strerror_r(e, buf, sizeof(buf));
      64                 :            : # else
      65                 :            :     // XSI-compliant strerror_r returns int:  0 means success; a positive error
      66                 :            :     // number should be returned on error, but glibc < 2.13 returns -1 and sets
      67                 :            :     // errno.
      68                 :            :     int r = strerror_r(e, buf, sizeof(buf));
      69                 :            :     if (r == 0) {
      70                 :            :         s += buf;
      71                 :            :     } else {
      72                 :            :         s += "Unknown error ";
      73                 :            :         s += str(e);
      74                 :            :     }
      75                 :            : # endif
      76                 :            : #else
      77                 :            :     // Not thread safe.  "C99 and POSIX.1-2008 require the return value to be
      78                 :            :     // non-NULL" and we require C++11 which incorporates C99.
      79                 :            :     s += strerror(e);
      80                 :            : #endif
      81                 :         12 : }

Generated by: LCOV version 1.11