From 7487fade751a70fe58f5ba106abc4ed5ecda8a8e Mon Sep 17 00:00:00 2001 From: NikLeberg Date: Mon, 11 Aug 2025 13:19:08 +0200 Subject: [PATCH] jtag/drivers/jtag_dpi: fix wraparound bug in runtest Commit 0847a4d7fb98 ("jtag/commands: Use 'unsigned int' data type") introduced bug when changing loop variable from `int` to `unsigned int`. Instead of getting negative and terminating the loop, the value wraps around to `INT_MAX` and the loop never finishes. Change-Id: I055025a1f8eb4abe50955607b3e89530dfd92af4 Signed-off-by: NikLeberg Fixes: 0847a4d7fb98 ("jtag/commands: Use 'unsigned int' data type") Reviewed-on: https://review.openocd.org/c/openocd/+/9078 Reviewed-by: Evgeniy Naydanov Reviewed-by: zapb Tested-by: jenkins Reviewed-by: Antonio Borneo --- src/jtag/drivers/jtag_dpi.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/jtag/drivers/jtag_dpi.c b/src/jtag/drivers/jtag_dpi.c index d6418d39c..35dd24507 100644 --- a/src/jtag/drivers/jtag_dpi.c +++ b/src/jtag/drivers/jtag_dpi.c @@ -189,7 +189,7 @@ static int jtag_dpi_runtest(unsigned int num_cycles) return ERROR_FAIL; } snprintf(buf, sizeof(buf), "ib %d\n", num_bits); - while (num_cycles > 0) { + for (unsigned int cycle = 0; cycle < num_cycles; cycle += num_bits + 6) { ret = write_sock(buf, strlen(buf)); if (ret != ERROR_OK) { LOG_ERROR("write_sock() fail, file %s, line %d", @@ -208,8 +208,6 @@ static int jtag_dpi_runtest(unsigned int num_cycles) __FILE__, __LINE__); goto out; } - - num_cycles -= num_bits + 6; } out: