@@ -383,7 +383,7 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
383
383
384
384
if (status == 0 ) {
385
385
os_mutex_lock (& tmpbuf_lock );
386
- snprintf (tmpbuf , MAX_PACKET_SIZE , "W%02" PRIx32 , status );
386
+ ( void ) snprintf (tmpbuf , MAX_PACKET_SIZE , "W%02" PRIx32 , status );
387
387
write_packet (server , tmpbuf );
388
388
os_mutex_unlock (& tmpbuf_lock );
389
389
return ;
@@ -399,18 +399,38 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
399
399
400
400
os_mutex_lock (& tmpbuf_lock );
401
401
// TODO: how name a wasm thread?
402
- len += snprintf (tmpbuf , MAX_PACKET_SIZE ,
403
- "T%02" PRIx32 "thread:%" PRIx64 ";name:%s;" , gdb_status ,
404
- (uint64 )(uintptr_t )tid , "nobody" );
402
+ len = snprintf (tmpbuf , MAX_PACKET_SIZE ,
403
+ "T%02" PRIx32 "thread:%" PRIx64 ";name:%s;" , gdb_status ,
404
+ (uint64 )(uintptr_t )tid , "nobody" );
405
+ if (len < 0 || len >= MAX_PACKET_SIZE ) {
406
+ os_mutex_unlock (& tmpbuf_lock );
407
+ return ;
408
+ }
409
+
405
410
if (tids_count > 0 ) {
406
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len , "threads:" );
411
+ int n = snprintf (tmpbuf + len , MAX_PACKET_SIZE - len , "threads:" );
412
+ if (n < 0 || n >= MAX_PACKET_SIZE - len ) {
413
+ os_mutex_unlock (& tmpbuf_lock );
414
+ return ;
415
+ }
416
+
417
+ len += n ;
407
418
while (i < tids_count ) {
408
- if (i == tids_count - 1 )
409
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
410
- "%" PRIx64 ";" , (uint64 )(uintptr_t )tids [i ]);
411
- else
412
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
413
- "%" PRIx64 "," , (uint64 )(uintptr_t )tids [i ]);
419
+ if (i == tids_count - 1 ) {
420
+ n = snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
421
+ "%" PRIx64 ";" , (uint64 )(uintptr_t )tids [i ]);
422
+ }
423
+ else {
424
+ n = snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
425
+ "%" PRIx64 "," , (uint64 )(uintptr_t )tids [i ]);
426
+ }
427
+
428
+ if (n < 0 || n >= MAX_PACKET_SIZE - len ) {
429
+ os_mutex_unlock (& tmpbuf_lock );
430
+ return ;
431
+ }
432
+
433
+ len += n ;
414
434
i ++ ;
415
435
}
416
436
}
@@ -427,32 +447,45 @@ send_thread_stop_status(WASMGDBServer *server, uint32 status, korp_tid tid)
427
447
/* When exception occurs, use reason:exception so the description can be
428
448
* correctly processed by LLDB */
429
449
uint32 exception_len = strlen (exception );
430
- len + =
450
+ int n =
431
451
snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
432
452
"thread-pcs:%" PRIx64 ";00:%s;reason:%s;description:" , pc ,
433
453
pc_string , "exception" );
454
+ if (n < 0 || n >= MAX_PACKET_SIZE - len ) {
455
+ os_mutex_unlock (& tmpbuf_lock );
456
+ return ;
457
+ }
458
+
459
+ len += n ;
434
460
/* The description should be encoded as HEX */
435
461
for (i = 0 ; i < exception_len ; i ++ ) {
436
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len , "%02x" ,
437
- exception [i ]);
462
+ n = snprintf (tmpbuf + len , MAX_PACKET_SIZE - len , "%02x" ,
463
+ exception [i ]);
464
+ if (n < 0 || n >= MAX_PACKET_SIZE - len ) {
465
+ os_mutex_unlock (& tmpbuf_lock );
466
+ return ;
467
+ }
468
+
469
+ len += n ;
438
470
}
439
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len , ";" );
471
+
472
+ (void )snprintf (tmpbuf + len , MAX_PACKET_SIZE - len , ";" );
440
473
}
441
474
else {
442
475
if (status == WAMR_SIG_TRAP ) {
443
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
444
- "thread-pcs:%" PRIx64 ";00:%s;reason:%s;" , pc ,
445
- pc_string , "breakpoint" );
476
+ ( void ) snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
477
+ "thread-pcs:%" PRIx64 ";00:%s;reason:%s;" , pc ,
478
+ pc_string , "breakpoint" );
446
479
}
447
480
else if (status == WAMR_SIG_SINGSTEP ) {
448
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
449
- "thread-pcs:%" PRIx64 ";00:%s;reason:%s;" , pc ,
450
- pc_string , "trace" );
481
+ ( void ) snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
482
+ "thread-pcs:%" PRIx64 ";00:%s;reason:%s;" , pc ,
483
+ pc_string , "trace" );
451
484
}
452
485
else { /* status > 0 (== 0 is checked at the function beginning) */
453
- len += snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
454
- "thread-pcs:%" PRIx64 ";00:%s;reason:%s;" , pc ,
455
- pc_string , "signal" );
486
+ ( void ) snprintf (tmpbuf + len , MAX_PACKET_SIZE - len ,
487
+ "thread-pcs:%" PRIx64 ";00:%s;reason:%s;" , pc ,
488
+ pc_string , "signal" );
456
489
}
457
490
}
458
491
write_packet (server , tmpbuf );
0 commit comments