[PATCH 2/5] contrib/plugins/hotblocks: Fix off by one error in iteration of sorted blocks

Alex Bradbury posted 5 patches 3 months, 2 weeks ago
Maintainers: "Alex Bennée" <alex.bennee@linaro.org>, Alexandre Iooss <erdnaxe@crans.org>, Mahmoud Mandour <ma.mandourr@gmail.com>, Pierrick Bouvier <pierrick.bouvier@linaro.org>
[PATCH 2/5] contrib/plugins/hotblocks: Fix off by one error in iteration of sorted blocks
Posted by Alex Bradbury 3 months, 2 weeks ago
The logic to iterate over the hottest blocks will never reach the last
item in the list, as it checks `it->next != NULL` before entering the
loop. It's hard to trigger this off-by-one error with the default
limit=20, but it is a bug and is problematic if that default is changed
to something larger.

Signed-off-by: Alex Bradbury <asb@igalia.com>
---
 contrib/plugins/hotblocks.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/contrib/plugins/hotblocks.c b/contrib/plugins/hotblocks.c
index d3dd23ed9f..cf4d6b8c36 100644
--- a/contrib/plugins/hotblocks.c
+++ b/contrib/plugins/hotblocks.c
@@ -82,10 +82,9 @@ static void plugin_exit(qemu_plugin_id_t id, void *p)
     sorted_counts = g_list_sort_with_data(counts, cmp_exec_count, NULL);
 
     if (sorted_counts) {
-        it = sorted_counts;
         g_string_append_printf(report, "pc, tcount, icount, ecount\n");
 
-        for (i = 0; i < limit && it->next; i++, it = it->next) {
+        for (i = 0, it = sorted_counts; i < limit && it; i++, it = it->next) {
             ExecCount *rec = (ExecCount *) it->data;
             g_string_append_printf(
                 report, "0x%016"PRIx64", %d, %ld, %"PRId64"\n",
-- 
2.50.1
Re: [PATCH 2/5] contrib/plugins/hotblocks: Fix off by one error in iteration of sorted blocks
Posted by Pierrick Bouvier 3 months, 2 weeks ago
On 7/29/25 11:41 PM, Alex Bradbury wrote:
> The logic to iterate over the hottest blocks will never reach the last
> item in the list, as it checks `it->next != NULL` before entering the
> loop. It's hard to trigger this off-by-one error with the default
> limit=20, but it is a bug and is problematic if that default is changed
> to something larger.
> 
> Signed-off-by: Alex Bradbury <asb@igalia.com>
> ---
>   contrib/plugins/hotblocks.c | 3 +--
>   1 file changed, 1 insertion(+), 2 deletions(-)

Thanks for this one too.
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>