Skip to content

Commit 530f9b2

Browse files
committed
merge bitcoin#24933: Replace non-threadsafe strerror
1 parent f53466d commit 530f9b2

File tree

9 files changed

+68
-20
lines changed

9 files changed

+68
-20
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ BITCOIN_CORE_H = \
372372
util/sock.h \
373373
util/string.h \
374374
util/spanparsing.h \
375+
util/syserror.h \
375376
util/system.h \
376377
util/time.h \
377378
util/thread.h \
@@ -825,6 +826,7 @@ libbitcoin_util_a_SOURCES = \
825826
util/hasher.cpp \
826827
util/getuniquepath.cpp \
827828
util/sock.cpp \
829+
util/syserror.cpp \
828830
util/system.cpp \
829831
util/message.cpp \
830832
util/moneystr.cpp \

src/bitcoind.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <noui.h>
2020
#include <shutdown.h>
2121
#include <util/check.h>
22+
#include <util/syserror.h>
2223
#include <util/system.h>
2324
#include <util/strencodings.h>
2425
#include <util/threadnames.h>
@@ -212,7 +213,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
212213
}
213214
break;
214215
case -1: // Error happened.
215-
return InitError(Untranslated(strprintf("fork_daemon() failed: %s\n", strerror(errno))));
216+
return InitError(Untranslated(strprintf("fork_daemon() failed: %s\n", SysErrorString(errno))));
216217
default: { // Parent: wait and exit.
217218
int token = daemon_ep.TokenRead();
218219
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>
@@ -41,7 +42,7 @@ fs::path AbsPathJoin(const fs::path& base, const fs::path& path)
4142

4243
static std::string GetErrorReason()
4344
{
44-
return std::strerror(errno);
45+
return SysErrorString(errno);
4546
}
4647

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

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include <util/moneystr.h>
6767
#include <util/strencodings.h>
6868
#include <util/string.h>
69+
#include <util/syserror.h>
6970
#include <util/system.h>
7071
#include <util/thread.h>
7172
#include <util/threadnames.h>
@@ -165,7 +166,7 @@ static fs::path GetPidFile(const ArgsManager& args)
165166
#endif
166167
return true;
167168
} else {
168-
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), std::strerror(errno)));
169+
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));
169170
}
170171
}
171172

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

@@ -359,19 +360,8 @@ std::string NetworkErrorString(int err)
359360
#else
360361
std::string NetworkErrorString(int err)
361362
{
362-
char buf[256];
363-
buf[0] = 0;
364-
/* Too bad there are two incompatible implementations of the
365-
* thread-safe strerror. */
366-
const char *s;
367-
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
368-
s = strerror_r(err, buf, sizeof(buf));
369-
#else /* POSIX variant always returns message in buffer */
370-
s = buf;
371-
if (strerror_r(err, buf, sizeof(buf)))
372-
buf[0] = 0;
373-
#endif
374-
return strprintf("%s (%d)", s, err);
363+
// On BSD sockets implementations, NetworkErrorString is the same as SysErrorString.
364+
return SysErrorString(err);
375365
}
376366
#endif
377367

src/util/syserror.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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[1024];
17+
/* Too bad there are three incompatible implementations of the
18+
* thread-safe strerror. */
19+
const char *s = nullptr;
20+
#ifdef WIN32
21+
if (strerror_s(buf, sizeof(buf), err) == 0) s = buf;
22+
#else
23+
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
24+
s = strerror_r(err, buf, sizeof(buf));
25+
#else /* POSIX variant always returns message in buffer */
26+
if (strerror_r(err, buf, sizeof(buf)) == 0) s = buf;
27+
#endif
28+
#endif
29+
if (s != nullptr) {
30+
return strprintf("%s (%d)", s, err);
31+
} else {
32+
return strprintf("Unknown error (%d)", err);
33+
}
34+
}

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
@@ -21,6 +21,7 @@
2121
#include <util/getuniquepath.h>
2222
#include <util/strencodings.h>
2323
#include <util/string.h>
24+
#include <util/syserror.h>
2425
#include <util/threadnames.h>
2526
#include <util/translation.h>
2627

@@ -1492,7 +1493,7 @@ void ScheduleBatchPriority()
14921493
const static sched_param param{};
14931494
const int rc = pthread_setschedparam(pthread_self(), SCHED_BATCH, &param);
14941495
if (rc != 0) {
1495-
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(rc));
1496+
LogPrintf("Failed to pthread_setschedparam: %s\n", SysErrorString(rc));
14961497
}
14971498
#endif
14981499
}

test/lint/lint-locale-dependence.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
"src/test/fuzz/locale.cpp",
5353
"src/test/fuzz/string.cpp",
5454
"src/util/strencodings.cpp:.*strtoll",
55-
"src/util/system.cpp:.*fprintf"
55+
"src/util/system.cpp:.*fprintf",
56+
"src/wallet/bdb.cpp:.*DbEnv::strerror", # False positive
57+
"src/util/syserror.cpp:.*strerror", # Outside this function use `SysErrorString`
5658
]
5759

5860
REGEXP_EXTERNAL_DEPENDENCIES_EXCLUSIONS = [
@@ -149,7 +151,7 @@
149151
"strcasecmp",
150152
"strcasestr",
151153
"strcoll", # LC_COLLATE
152-
#"strerror",
154+
"strerror",
153155
"strfmon",
154156
"strftime", # LC_TIME
155157
"strncasecmp",
@@ -223,7 +225,7 @@
223225
def find_locale_dependent_function_uses():
224226
regexp_locale_dependent_functions = "|".join(LOCALE_DEPENDENT_FUNCTIONS)
225227
exclude_args = [":(exclude)" + excl for excl in REGEXP_EXTERNAL_DEPENDENCIES_EXCLUSIONS]
226-
git_grep_command = ["git", "grep", "-E", "[^a-zA-Z0-9_\\`'\"<>](" + regexp_locale_dependent_functions + "(_r|_s)?)[^a-zA-Z0-9_\\`'\"<>]", "--", "*.cpp", "*.h"] + exclude_args
228+
git_grep_command = ["git", "grep", "-E", "[^a-zA-Z0-9_\\`'\"<>](" + regexp_locale_dependent_functions + ")(_r|_s)?[^a-zA-Z0-9_\\`'\"<>]", "--", "*.cpp", "*.h"] + exclude_args
227229
git_grep_output = list()
228230

229231
try:

0 commit comments

Comments
 (0)