Skip to content

Commit fe31b31

Browse files
Empactfurszy
authored andcommitted
Assert CPubKey::ValidLength to the pubkey's header-relevent size
Previously this was an inline test where the specificity was probably judged overly specific. As a class method it makes sense to maintain consistency. And replace some magic values with their constant equivalents. Excludes changes to the following functions we don't have: - ExtractPubKey (bitcoin#6415) - IsCompressedPubKey (bitcoin#8499)
1 parent 2f6b249 commit fe31b31

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/pubkey.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ class CPubKey
6969
}
7070

7171
public:
72+
73+
bool static ValidSize(const std::vector<unsigned char> &vch) {
74+
return vch.size() > 0 && GetLen(vch[0]) == vch.size();
75+
}
76+
7277
//! Construct an invalid public key.
7378
CPubKey()
7479
{

src/script/interpreter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ static inline void popstack(std::vector<valtype>& stack)
6464
}
6565

6666
bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
67-
if (vchPubKey.size() < 33) {
67+
if (vchPubKey.size() < CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
6868
// Non-canonical public key: too short
6969
return false;
7070
}
7171
if (vchPubKey[0] == 0x04) {
72-
if (vchPubKey.size() != 65) {
72+
if (vchPubKey.size() != CPubKey::PUBLIC_KEY_SIZE) {
7373
// Non-canonical public key: invalid length for uncompressed key
7474
return false;
7575
}
7676
} else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
77-
if (vchPubKey.size() != 33) {
77+
if (vchPubKey.size() != CPubKey::COMPRESSED_PUBLIC_KEY_SIZE) {
7878
// Non-canonical public key: invalid length for compressed key
7979
return false;
8080
}

src/script/standard.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
124124
// Template matching opcodes:
125125
if (opcode2 == OP_PUBKEYS)
126126
{
127-
while (vch1.size() >= 33 && vch1.size() <= 65)
127+
while (CPubKey::ValidSize(vch1))
128128
{
129129
vSolutionsRet.push_back(vch1);
130130
if (!script1.GetOp(pc1, opcode1, vch1))
@@ -138,7 +138,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::v
138138

139139
if (opcode2 == OP_PUBKEY)
140140
{
141-
if (vch1.size() < 33 || vch1.size() > 65)
141+
if (!CPubKey::ValidSize(vch1))
142142
break;
143143
vSolutionsRet.push_back(vch1);
144144
}

0 commit comments

Comments
 (0)