LCOV - code coverage report
Current view: top level - src/util - syserror.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 85.7 % 7 6
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 1 1
Branches: 50.0 % 2 1

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2020-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <config/bitcoin-config.h> // IWYU pragma: keep
       6                 :             : 
       7                 :             : #include <tinyformat.h>
       8                 :             : #include <util/syserror.h>
       9                 :             : 
      10                 :             : #include <cstring>
      11                 :             : #include <string>
      12                 :             : 
      13                 :             : #if defined(WIN32)
      14                 :             : #include <windows.h>
      15                 :             : #include <locale>
      16                 :             : #include <codecvt>
      17                 :             : #endif
      18                 :             : 
      19                 :           2 : std::string SysErrorString(int err)
      20                 :             : {
      21                 :           2 :     char buf[1024];
      22                 :             :     /* Too bad there are three incompatible implementations of the
      23                 :             :      * thread-safe strerror. */
      24                 :           2 :     const char *s = nullptr;
      25                 :             : #ifdef WIN32
      26                 :             :     if (strerror_s(buf, sizeof(buf), err) == 0) s = buf;
      27                 :             : #else
      28                 :             : #ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
      29                 :           2 :     s = strerror_r(err, buf, sizeof(buf));
      30                 :             : #else /* POSIX variant always returns message in buffer */
      31                 :             :     if (strerror_r(err, buf, sizeof(buf)) == 0) s = buf;
      32                 :             : #endif
      33                 :             : #endif
      34         [ +  - ]:           2 :     if (s != nullptr) {
      35                 :           2 :         return strprintf("%s (%d)", s, err);
      36                 :             :     } else {
      37                 :           0 :         return strprintf("Unknown error (%d)", err);
      38                 :             :     }
      39                 :             : }
      40                 :             : 
      41                 :             : #if defined(WIN32)
      42                 :             : std::string Win32ErrorString(int err)
      43                 :             : {
      44                 :             :     wchar_t buf[256];
      45                 :             :     buf[0] = 0;
      46                 :             :     if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_MAX_WIDTH_MASK,
      47                 :             :                        nullptr, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      48                 :             :                        buf, ARRAYSIZE(buf), nullptr))
      49                 :             :     {
      50                 :             :         return strprintf("%s (%d)", std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,wchar_t>().to_bytes(buf), err);
      51                 :             :     }
      52                 :             :     else
      53                 :             :     {
      54                 :             :         return strprintf("Unknown error (%d)", err);
      55                 :             :     }
      56                 :             : }
      57                 :             : #endif
        

Generated by: LCOV version 2.0-1