Skip to content

Commit 1253678

Browse files
laanwjDuddino
authored andcommitted
[script] simplify CheckMinimalPush checks, add safety assert (bitcoin#11900)
0749808 CheckMinimalPush comments are prescriptive (Gregory Sanders) 176db61 simplify CheckMinimalPush checks, add safety assert (Gregory Sanders) Pull request description: the two conditions could simply never be hit as `true`, as those opcodes have a push payload of size 0 in `data`. Added the assert for clarity for future readers(matching the gating in the interpreter) and safety for future use. This effects policy only. Tree-SHA512: f49028a1d5e907ef697b9bf5104c81ba8f6a331dbe5d60d8d8515ac17d2d6bfdc9dcc856a7e3dbd54814871b7d0695584d28da6553e2d9d7715430223f0b3690
1 parent 4914933 commit 1253678

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

src/script/interpreter.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,25 @@ bool static CheckPubKeyEncoding(const valtype &vchSig, unsigned int flags, Scrip
212212
}
213213

214214
bool static CheckMinimalPush(const valtype& data, opcodetype opcode) {
215+
// Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
216+
assert(0 <= opcode && opcode <= OP_PUSHDATA4);
215217
if (data.size() == 0) {
216-
// Could have used OP_0.
218+
// Should have used OP_0.
217219
return opcode == OP_0;
218220
} else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
219-
// Could have used OP_1 .. OP_16.
220-
return opcode == OP_1 + (data[0] - 1);
221+
// Should have used OP_1 .. OP_16.
222+
return false;
221223
} else if (data.size() == 1 && data[0] == 0x81) {
222-
// Could have used OP_1NEGATE.
223-
return opcode == OP_1NEGATE;
224+
// Should have used OP_1NEGATE.
225+
return false;
224226
} else if (data.size() <= 75) {
225-
// Could have used a direct push (opcode indicating number of bytes pushed + those bytes).
227+
// Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
226228
return opcode == data.size();
227229
} else if (data.size() <= 255) {
228-
// Could have used OP_PUSHDATA.
230+
// Must have used OP_PUSHDATA.
229231
return opcode == OP_PUSHDATA1;
230232
} else if (data.size() <= 65535) {
231-
// Could have used OP_PUSHDATA2.
233+
// Must have used OP_PUSHDATA2.
232234
return opcode == OP_PUSHDATA2;
233235
}
234236
return true;

0 commit comments

Comments
 (0)