Skip to content

Commit 5e1aaca

Browse files
committed
Merge bitcoin/bitcoin#24933: util: Replace non-threadsafe strerror
e3a06a3 test: Add `strerror` to locale-dependence linter (laanwj) f00fb12 util: Increase buffer size to 1024 in SysErrorString (laanwj) 718da30 util: Refactor SysErrorString logic (laanwj) e7f2f77 util: Use strerror_s for SysErrorString on Windows (laanwj) 46971c6 util: Replace non-threadsafe strerror (laanwj) Pull request description: 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 (with code from `NetworkErrorString`) and replace all uses of `strerror` with this. Edit: I've also added a commit that refactors the code so that buf[] is never read at all if the function fails, making some fragile-looking code unnecessary. Edit2: from the linux manpage: ``` ATTRIBUTES For an explanation of the terms used in this section, see attributes(7). ┌───────────────────┬───────────────┬─────────────────────────┐ │Interface │ Attribute │ Value │ ├───────────────────┼───────────────┼─────────────────────────┤ │strerror() │ Thread safety │ MT-Unsafe race:strerror │ ├───────────────────┼───────────────┼─────────────────────────┤ … ├───────────────────┼───────────────┼─────────────────────────┤ │strerror_r(), │ Thread safety │ MT-Safe │ │strerror_l() │ │ │ └───────────────────┴───────────────┴─────────────────────────┘ ``` As the function can be called from any thread at any time, using a non-thread-safe function is unacceptable. ACKs for top commit: jonatack: ACK e3a06a3 Tree-SHA512: 20e71ebb9e979d4e1d8cafbb2e32e20c2a63f09115fe72cdde67c8f80ae98c531d286f935fd8a6e92a18b72607d7bd3e846b2d871d9691a6036b0676de8aaf25
2 parents fe6a299 + e3a06a3 commit 5e1aaca

File tree

9 files changed

+69
-20
lines changed

9 files changed

+69
-20
lines changed

src/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ BITCOIN_CORE_H = \
275275
util/spanparsing.h \
276276
util/string.h \
277277
util/syscall_sandbox.h \
278+
util/syserror.h \
278279
util/system.h \
279280
util/thread.h \
280281
util/threadnames.h \
@@ -657,6 +658,7 @@ libbitcoin_util_a_SOURCES = \
657658
util/getuniquepath.cpp \
658659
util/hasher.cpp \
659660
util/sock.cpp \
661+
util/syserror.cpp \
660662
util/system.cpp \
661663
util/message.cpp \
662664
util/moneystr.cpp \
@@ -918,6 +920,7 @@ libbitcoinkernel_la_SOURCES = \
918920
util/settings.cpp \
919921
util/strencodings.cpp \
920922
util/syscall_sandbox.cpp \
923+
util/syserror.cpp \
921924
util/system.cpp \
922925
util/thread.cpp \
923926
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>
@@ -150,7 +151,7 @@ static fs::path GetPidFile(const ArgsManager& args)
150151
#endif
151152
return true;
152153
} else {
153-
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), std::strerror(errno)));
154+
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), fs::PathToString(GetPidFile(args)), SysErrorString(errno)));
154155
}
155156
}
156157

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: 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
@@ -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
}

test/lint/lint-locale-dependence.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
"src/test/fuzz/locale.cpp:.*setlocale",
5050
"src/test/fuzz/string.cpp:.*strtol",
5151
"src/test/fuzz/string.cpp:.*strtoul",
52-
"src/test/util_tests.cpp:.*strtoll"
52+
"src/test/util_tests.cpp:.*strtoll",
53+
"src/wallet/bdb.cpp:.*DbEnv::strerror", # False positive
54+
"src/util/syserror.cpp:.*strerror", # Outside this function use `SysErrorString`
5355
]
5456

5557
REGEXP_EXTERNAL_DEPENDENCIES_EXCLUSIONS = [
@@ -144,7 +146,7 @@
144146
"strcasecmp",
145147
"strcasestr",
146148
"strcoll", # LC_COLLATE
147-
#"strerror",
149+
"strerror",
148150
"strfmon",
149151
"strftime", # LC_TIME
150152
"strncasecmp",
@@ -218,7 +220,7 @@
218220
def find_locale_dependent_function_uses():
219221
regexp_locale_dependent_functions = "|".join(LOCALE_DEPENDENT_FUNCTIONS)
220222
exclude_args = [":(exclude)" + excl for excl in REGEXP_EXTERNAL_DEPENDENCIES_EXCLUSIONS]
221-
git_grep_command = ["git", "grep", "-E", "[^a-zA-Z0-9_\\`'\"<>](" + regexp_locale_dependent_functions + "(_r|_s)?)[^a-zA-Z0-9_\\`'\"<>]", "--", "*.cpp", "*.h"] + exclude_args
223+
git_grep_command = ["git", "grep", "-E", "[^a-zA-Z0-9_\\`'\"<>](" + regexp_locale_dependent_functions + ")(_r|_s)?[^a-zA-Z0-9_\\`'\"<>]", "--", "*.cpp", "*.h"] + exclude_args
222224
git_grep_output = list()
223225

224226
try:

0 commit comments

Comments
 (0)