@@ -242,9 +242,9 @@ int main(int argc, char* const* argv)
242242 }
243243 }
244244
245- CScript script;
246245 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)) {
248248 if (verbose) btc_logf (" valid script\n " );
249249 } else {
250250 fprintf (stderr, " invalid script\n " );
@@ -300,6 +300,7 @@ int main(int argc, char* const* argv)
300300 p2sh_script = CScript (p2sh_script_payload.begin (), p2sh_script_payload.end ());
301301 }
302302 }
303+
303304 if (has_p2sh) {
304305 script_ptrs.push_back (&p2sh_script);
305306 script_headers.push_back (" <<< P2SH script >>>" );
@@ -316,19 +317,53 @@ int main(int argc, char* const* argv)
316317 script_lines[i++] = strdup (strprintf (" #%04d %s" , i, s).c_str ());
317318 }
318319 }
320+
319321 for (size_t siter = 0 ; siter < script_ptrs.size (); ++siter) {
320322 CScript* script = script_ptrs[siter];
321323 const std::string& header = script_headers[siter];
322324 if (header != " " ) script_lines[i++] = strdup (header.c_str ());
323325 it = script->begin ();
326+
324327 while (script->GetOp (it, opcode, vchPushValue)) {
328+ // log opcode and data
325329 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);
327342 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+ );
329349 } 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+ );
331356 }
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
332367 script_lines[i++] = strdup (buf);
333368 }
334369 }
@@ -349,6 +384,7 @@ int main(int argc, char* const* argv)
349384 }
350385
351386 print_stack (env->stack , true );
387+
352388 return 0 ;
353389 } else {
354390 kerl_set_history_file (" .btcdeb_history" );
@@ -371,6 +407,7 @@ int main(int argc, char* const* argv)
371407 if (env->curr_op_seq < count) {
372408 printf (" %s\n " , script_lines[env->curr_op_seq ]);
373409 }
410+
374411 kerl_run (" btcdeb> " );
375412 }
376413}
0 commit comments