Skip to content

Commit f4e12fd

Browse files
committed
Merge bitcoin/bitcoin#22278: Add LIFETIMEBOUND to CScript where needed
fa7e6c5 Add LIFETIMEBOUND to InitializeChainstate (MarcoFalke) fa5c896 Add LIFETIMEBOUND to CScript where needed (MarcoFalke) Pull request description: Without this, stack-use-after-scope can only be detected at runtime with ASan or code review, both of which are expensive. Use `LIFETIMEBOUND` to turn this error into a compile warning. See https://releases.llvm.org/12.0.0/tools/clang/docs/AttributeReference.html#lifetimebound Example: ```cpp const CScript a{WITH_LOCK(::cs_main, return CScript{} << OP_0 << OP_1)}; ``` Before: (no warning) After: ``` warning: returning reference to local temporary object [-Wreturn-stack-address] const CScript a{WITH_LOCK(::cs_main, return CScript{} << OP_0 << OP_1)}; ^~~~~~~~~ ./sync.h:276:65: note: expanded from macro 'WITH_LOCK' #define WITH_LOCK(cs, code) [&]() -> decltype(auto) { LOCK(cs); code; }() ^~~~ ACKs for top commit: theuni: utACK fa7e6c5. jonatack: Light ACK fa7e6c5 debug build with clang 13, reproduced the example compiler warning in the pull description, and briefly looked at `clang::lifetimebound` support in earlier versions of clang; it is in clang 7 (https://releases.llvm.org/7.0.0/tools/clang/docs/AttributeReference.html#lifetimebound-clang-lifetimebound), did not see references to it in earlier docs Tree-SHA512: e915acdc4532445205b7703fab61a5d682231ace78ecfb274cb8523ca2bddefd85828f50ac047cfb1afaff92a331f5f7b5a1472539f999e30f7cf8ac8c3222f3
2 parents 5fb6701 + fa7e6c5 commit f4e12fd

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/script/script.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_SCRIPT_SCRIPT_H
77
#define BITCOIN_SCRIPT_SCRIPT_H
88

9+
#include <attributes.h>
910
#include <crypto/common.h>
1011
#include <prevector.h>
1112
#include <serialize.h>
@@ -438,23 +439,23 @@ class CScript : public CScriptBase
438439
/** Delete non-existent operator to defend against future introduction */
439440
CScript& operator<<(const CScript& b) = delete;
440441

441-
CScript& operator<<(int64_t b) { return push_int64(b); }
442+
CScript& operator<<(int64_t b) LIFETIMEBOUND { return push_int64(b); }
442443

443-
CScript& operator<<(opcodetype opcode)
444+
CScript& operator<<(opcodetype opcode) LIFETIMEBOUND
444445
{
445446
if (opcode < 0 || opcode > 0xff)
446447
throw std::runtime_error("CScript::operator<<(): invalid opcode");
447448
insert(end(), (unsigned char)opcode);
448449
return *this;
449450
}
450451

451-
CScript& operator<<(const CScriptNum& b)
452+
CScript& operator<<(const CScriptNum& b) LIFETIMEBOUND
452453
{
453454
*this << b.getvch();
454455
return *this;
455456
}
456457

457-
CScript& operator<<(const std::vector<unsigned char>& b)
458+
CScript& operator<<(const std::vector<unsigned char>& b) LIFETIMEBOUND
458459
{
459460
if (b.size() < OP_PUSHDATA1)
460461
{

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ class ChainstateManager
929929
CChainState& InitializeChainstate(
930930
CTxMemPool* mempool,
931931
const std::optional<uint256>& snapshot_blockhash = std::nullopt)
932-
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
932+
LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
933933

934934
//! Get all chainstates currently being used.
935935
std::vector<CChainState*> GetAll();

0 commit comments

Comments
 (0)