Skip to content

Commit 46971c6

Browse files
committed
util: Replace non-threadsafe strerror
Some uses of non-threadsafe `strerror` have snuck into the code since they were removed in #4152. Add a wrapper `SysErrorString` for thread-safe strerror alternatives and replace all uses of `strerror` with this.
1 parent 4381681 commit 46971c6

File tree

8 files changed

+59
-17
lines changed

8 files changed

+59
-17
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ BITCOIN_CORE_H = \
271271
util/spanparsing.h \
272272
util/string.h \
273273
util/syscall_sandbox.h \
274+
util/syserror.h \
274275
util/system.h \
275276
util/thread.h \
276277
util/threadnames.h \
@@ -631,6 +632,7 @@ libbitcoin_util_a_SOURCES = \
631632
util/getuniquepath.cpp \
632633
util/hasher.cpp \
633634
util/sock.cpp \
635+
util/syserror.cpp \
634636
util/system.cpp \
635637
util/message.cpp \
636638
util/moneystr.cpp \
@@ -853,6 +855,7 @@ bitcoin_chainstate_SOURCES = \
853855
util/settings.cpp \
854856
util/strencodings.cpp \
855857
util/syscall_sandbox.cpp \
858+
util/syserror.cpp \
856859
util/system.cpp \
857860
util/thread.cpp \
858861
util/threadnames.cpp \

src/bitcoind.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <util/check.h>
2121
#include <util/strencodings.h>
2222
#include <util/syscall_sandbox.h>
23+
#include <util/syserror.h>
2324
#include <util/system.h>
2425
#include <util/threadnames.h>
2526
#include <util/tokenpipe.h>
@@ -206,7 +207,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
206207
}
207208
break;
208209
case -1: // Error happened.
209-
return InitError(Untranslated(strprintf("fork_daemon() failed: %s\n", strerror(errno))));
210+
return InitError(Untranslated(strprintf("fork_daemon() failed: %s\n", SysErrorString(errno))));
210211
default: { // Parent: wait and exit.
211212
int token = daemon_ep.TokenRead();
212213
if (token) { // Success

src/fs.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <fs.h>
6+
#include <util/syserror.h>
67

78
#ifndef WIN32
89
#include <cstring>
@@ -44,7 +45,7 @@ fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
4445

4546
static std::string GetErrorReason()
4647
{
47-
return std::strerror(errno);
48+
return SysErrorString(errno);
4849
}
4950

5051
FileLock::FileLock(const fs::path& file)

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include <util/strencodings.h>
6666
#include <util/string.h>
6767
#include <util/syscall_sandbox.h>
68+
#include <util/syserror.h>
6869
#include <util/system.h>
6970
#include <util/thread.h>
7071
#include <util/threadnames.h>
@@ -149,7 +150,7 @@ static fs::path GetPidFile(const ArgsManager& args)
149150
#endif
150151
return true;
151152
} else {
152-
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), std::strerror(errno)));
153+
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));
153154
}
154155
}
155156

src/util/sock.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <threadinterrupt.h>
88
#include <tinyformat.h>
99
#include <util/sock.h>
10+
#include <util/syserror.h>
1011
#include <util/system.h>
1112
#include <util/time.h>
1213

@@ -344,19 +345,8 @@ std::string NetworkErrorString(int err)
344345
#else
345346
std::string NetworkErrorString(int err)
346347
{
347-
char buf[256];
348-
buf[0] = 0;
349-
/* Too bad there are two incompatible implementations of the
350-
* thread-safe strerror. */
351-
const char *s;
352-
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
353-
s = strerror_r(err, buf, sizeof(buf));
354-
#else /* POSIX variant always returns message in buffer */
355-
s = buf;
356-
if (strerror_r(err, buf, sizeof(buf)))
357-
buf[0] = 0;
358-
#endif
359-
return strprintf("%s (%d)", s, err);
348+
// On BSD sockets implementations, NetworkErrorString is the same as SysErrorString.
349+
return SysErrorString(err);
360350
}
361351
#endif
362352

src/util/syserror.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#if defined(HAVE_CONFIG_H)
6+
#include <config/bitcoin-config.h>
7+
#endif
8+
9+
#include <tinyformat.h>
10+
#include <util/syserror.h>
11+
12+
#include <cstring>
13+
14+
std::string SysErrorString(int err)
15+
{
16+
char buf[256];
17+
buf[0] = 0;
18+
/* Too bad there are two incompatible implementations of the
19+
* thread-safe strerror. */
20+
const char *s;
21+
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
22+
s = strerror_r(err, buf, sizeof(buf));
23+
#else /* POSIX variant always returns message in buffer */
24+
s = buf;
25+
if (strerror_r(err, buf, sizeof(buf)))
26+
buf[0] = 0;
27+
#endif
28+
return strprintf("%s (%d)", s, err);
29+
}

src/util/syserror.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2010-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+
#ifndef BITCOIN_UTIL_SYSERROR_H
6+
#define BITCOIN_UTIL_SYSERROR_H
7+
8+
#include <string>
9+
10+
/** Return system error string from errno value. Use this instead of
11+
* std::strerror, which is not thread-safe. For network errors use
12+
* NetworkErrorString from sock.h instead.
13+
*/
14+
std::string SysErrorString(int err);
15+
16+
#endif // BITCOIN_UTIL_SYSERROR_H

src/util/system.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <util/getuniquepath.h>
2626
#include <util/strencodings.h>
2727
#include <util/string.h>
28+
#include <util/syserror.h>
2829
#include <util/translation.h>
2930

3031

@@ -1374,7 +1375,7 @@ void ScheduleBatchPriority()
13741375
const static sched_param param{};
13751376
const int rc = pthread_setschedparam(pthread_self(), SCHED_BATCH, &param);
13761377
if (rc != 0) {
1377-
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(rc));
1378+
LogPrintf("Failed to pthread_setschedparam: %s\n", SysErrorString(rc));
13781379
}
13791380
#endif
13801381
}

0 commit comments

Comments
 (0)