@@ -242,9 +242,9 @@ int main(int argc, char* const* argv)
242
242
}
243
243
}
244
244
245
- CScript script;
246
245
if (script_str) {
247
- if (instance.parse_script (script_str)) {
246
+ int witprogver = ((flags & SCRIPT_VERIFY_TAPROOT) != 0 ? 1 : 0 );
247
+ if (instance.parse_script (witprogver, script_str)) {
248
248
if (verbose) btc_logf (" valid script\n " );
249
249
} else {
250
250
fprintf (stderr, " invalid script\n " );
@@ -300,6 +300,7 @@ int main(int argc, char* const* argv)
300
300
p2sh_script = CScript (p2sh_script_payload.begin (), p2sh_script_payload.end ());
301
301
}
302
302
}
303
+
303
304
if (has_p2sh) {
304
305
script_ptrs.push_back (&p2sh_script);
305
306
script_headers.push_back (" <<< P2SH script >>>" );
@@ -316,19 +317,53 @@ int main(int argc, char* const* argv)
316
317
script_lines[i++] = strdup (strprintf (" #%04d %s" , i, s).c_str ());
317
318
}
318
319
}
320
+
319
321
for (size_t siter = 0 ; siter < script_ptrs.size (); ++siter) {
320
322
CScript* script = script_ptrs[siter];
321
323
const std::string& header = script_headers[siter];
322
324
if (header != " " ) script_lines[i++] = strdup (header.c_str ());
323
325
it = script->begin ();
326
+
324
327
while (script->GetOp (it, opcode, vchPushValue)) {
328
+ // log opcode and data
325
329
char * pbuf = buf;
326
- pbuf += snprintf (pbuf, 1024 , " #%04d " , i);
330
+
331
+ // Write the line number
332
+ int written = snprintf (pbuf, sizeof (buf), " #%04d " , i);
333
+ if (written < 0 || written >= (int )sizeof (buf)) {
334
+ // Handle error or truncation
335
+ // For safety, we can bail out or clamp
336
+ written = (int )(sizeof (buf) - 1 );
337
+ }
338
+ pbuf += written;
339
+
340
+ // Write the opcode
341
+ size_t remain = sizeof (buf) - (pbuf - buf);
327
342
if (vchPushValue.size () > 0 ) {
328
- snprintf (pbuf, 1024 + pbuf - buf, " %s" , HexStr (std::vector<uint8_t >(vchPushValue.begin (), vchPushValue.end ())).c_str ());
343
+ written = snprintf (
344
+ pbuf,
345
+ remain,
346
+ " %s" ,
347
+ HexStr (std::vector<uint8_t >(vchPushValue.begin (), vchPushValue.end ())).c_str ()
348
+ );
329
349
} else {
330
- snprintf (pbuf, 1024 + pbuf - buf, " %s" , GetOpName (opcode).c_str ());
350
+ written = snprintf (
351
+ pbuf,
352
+ remain,
353
+ " %s" ,
354
+ GetOpName (opcode).c_str ()
355
+ );
331
356
}
357
+
358
+ // Handle error or truncation
359
+ if (written < 0 || (size_t )written >= remain) {
360
+ // Handle error or truncation
361
+ written = (int )(remain - 1 );
362
+ }
363
+
364
+ pbuf += written;
365
+
366
+ // Write the buffer
332
367
script_lines[i++] = strdup (buf);
333
368
}
334
369
}
@@ -349,6 +384,7 @@ int main(int argc, char* const* argv)
349
384
}
350
385
351
386
print_stack (env->stack , true );
387
+
352
388
return 0 ;
353
389
} else {
354
390
kerl_set_history_file (" .btcdeb_history" );
@@ -371,6 +407,7 @@ int main(int argc, char* const* argv)
371
407
if (env->curr_op_seq < count) {
372
408
printf (" %s\n " , script_lines[env->curr_op_seq ]);
373
409
}
410
+
374
411
kerl_run (" btcdeb> " );
375
412
}
376
413
}
0 commit comments