This is an automated email from Gerrit. "Antonio Borneo <[email protected]>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9659
-- gerrit commit dc9ec73349b432652b52f2db70e230aab1366bfe Author: Antonio Borneo <[email protected]> Date: Mon May 18 10:56:07 2026 +0200 target: profile: avoid division by zero Issue detected by scan-build. The function target_profiling() can return with num_of_samples==0 and this will trigger a division by zero. Check the value of num_of_samples before continuing. While there, simplify the return path and the return value. Fixes: dcf628298534 ("target: filter and sort pc samples before storing") Change-Id: I74faa7de5c7fac6f8c862e338d2de5492a0dd14a Signed-off-by: Antonio Borneo <[email protected]> diff --git a/src/target/target.c b/src/target/target.c index 4d87412381..d68e9afb19 100644 --- a/src/target/target.c +++ b/src/target/target.c @@ -4331,6 +4331,12 @@ COMMAND_HANDLER(handle_profile_command) return retval; } + if (!num_of_samples) { + command_print(CMD, "Wrote no samples"); + free(samples); + return ERROR_OK; + } + if (with_range) { uint32_t num_filtered_samples = 0; for (uint32_t in = 0; in < num_of_samples; ++in) { @@ -4342,19 +4348,21 @@ COMMAND_HANDLER(handle_profile_command) if (duration_ms < 1) duration_ms = 0; num_of_samples = num_filtered_samples; + + if (!num_of_samples) { + command_print(CMD, "Wrote no samples in the requested range"); + free(samples); + return ERROR_OK; + } } - if (num_of_samples) { - qsort(samples, num_of_samples, sizeof(samples[0]), compare_pc32); + qsort(samples, num_of_samples, sizeof(samples[0]), compare_pc32); - write_gmon(samples, num_of_samples, CMD_ARGV[1], target, duration_ms); - command_print(CMD, "Wrote %s", CMD_ARGV[1]); - } else { - command_print(CMD, "Wrote no samples"); - } + write_gmon(samples, num_of_samples, CMD_ARGV[1], target, duration_ms); + command_print(CMD, "Wrote %s", CMD_ARGV[1]); free(samples); - return retval; + return ERROR_OK; } COMMAND_HANDLER(handle_target_read_memory) --
