mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
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.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <threadinterrupt.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/sock.h>
|
||||
#include <util/syserror.h>
|
||||
#include <util/system.h>
|
||||
#include <util/time.h>
|
||||
|
||||
@@ -344,19 +345,8 @@ std::string NetworkErrorString(int err)
|
||||
#else
|
||||
std::string NetworkErrorString(int err)
|
||||
{
|
||||
char buf[256];
|
||||
buf[0] = 0;
|
||||
/* Too bad there are two incompatible implementations of the
|
||||
* thread-safe strerror. */
|
||||
const char *s;
|
||||
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
|
||||
s = strerror_r(err, buf, sizeof(buf));
|
||||
#else /* POSIX variant always returns message in buffer */
|
||||
s = buf;
|
||||
if (strerror_r(err, buf, sizeof(buf)))
|
||||
buf[0] = 0;
|
||||
#endif
|
||||
return strprintf("%s (%d)", s, err);
|
||||
// On BSD sockets implementations, NetworkErrorString is the same as SysErrorString.
|
||||
return SysErrorString(err);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
29
src/util/syserror.cpp
Normal file
29
src/util/syserror.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright (c) 2020-2022 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#if defined(HAVE_CONFIG_H)
|
||||
#include <config/bitcoin-config.h>
|
||||
#endif
|
||||
|
||||
#include <tinyformat.h>
|
||||
#include <util/syserror.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
std::string SysErrorString(int err)
|
||||
{
|
||||
char buf[256];
|
||||
buf[0] = 0;
|
||||
/* Too bad there are two incompatible implementations of the
|
||||
* thread-safe strerror. */
|
||||
const char *s;
|
||||
#ifdef STRERROR_R_CHAR_P /* GNU variant can return a pointer outside the passed buffer */
|
||||
s = strerror_r(err, buf, sizeof(buf));
|
||||
#else /* POSIX variant always returns message in buffer */
|
||||
s = buf;
|
||||
if (strerror_r(err, buf, sizeof(buf)))
|
||||
buf[0] = 0;
|
||||
#endif
|
||||
return strprintf("%s (%d)", s, err);
|
||||
}
|
||||
16
src/util/syserror.h
Normal file
16
src/util/syserror.h
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) 2010-2022 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_UTIL_SYSERROR_H
|
||||
#define BITCOIN_UTIL_SYSERROR_H
|
||||
|
||||
#include <string>
|
||||
|
||||
/** Return system error string from errno value. Use this instead of
|
||||
* std::strerror, which is not thread-safe. For network errors use
|
||||
* NetworkErrorString from sock.h instead.
|
||||
*/
|
||||
std::string SysErrorString(int err);
|
||||
|
||||
#endif // BITCOIN_UTIL_SYSERROR_H
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <util/getuniquepath.h>
|
||||
#include <util/strencodings.h>
|
||||
#include <util/string.h>
|
||||
#include <util/syserror.h>
|
||||
#include <util/translation.h>
|
||||
|
||||
|
||||
@@ -1374,7 +1375,7 @@ void ScheduleBatchPriority()
|
||||
const static sched_param param{};
|
||||
const int rc = pthread_setschedparam(pthread_self(), SCHED_BATCH, ¶m);
|
||||
if (rc != 0) {
|
||||
LogPrintf("Failed to pthread_setschedparam: %s\n", strerror(rc));
|
||||
LogPrintf("Failed to pthread_setschedparam: %s\n", SysErrorString(rc));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user