mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
Fix Racy ParseOpCode function initialization
This commit is contained in:
@@ -21,13 +21,15 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
class OpCodeParser
|
||||||
opcodetype ParseOpCode(const std::string& s)
|
|
||||||
{
|
{
|
||||||
static std::map<std::string, opcodetype> mapOpNames;
|
private:
|
||||||
|
std::map<std::string, opcodetype> mapOpNames;
|
||||||
|
|
||||||
if (mapOpNames.empty()) {
|
public:
|
||||||
for (unsigned int op = 0; op <= MAX_OPCODE; op++) {
|
OpCodeParser()
|
||||||
|
{
|
||||||
|
for (unsigned int op = 0; op <= MAX_OPCODE; ++op) {
|
||||||
// Allow OP_RESERVED to get into mapOpNames
|
// Allow OP_RESERVED to get into mapOpNames
|
||||||
if (op < OP_NOP && op != OP_RESERVED) {
|
if (op < OP_NOP && op != OP_RESERVED) {
|
||||||
continue;
|
continue;
|
||||||
@@ -44,10 +46,18 @@ opcodetype ParseOpCode(const std::string& s)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
opcodetype Parse(const std::string& s) const
|
||||||
|
{
|
||||||
auto it = mapOpNames.find(s);
|
auto it = mapOpNames.find(s);
|
||||||
if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
|
if (it == mapOpNames.end()) throw std::runtime_error("script parse error: unknown opcode");
|
||||||
return it->second;
|
return it->second;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
opcodetype ParseOpCode(const std::string& s)
|
||||||
|
{
|
||||||
|
static const OpCodeParser ocp;
|
||||||
|
return ocp.Parse(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
Reference in New Issue
Block a user