]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-client.git/commitdiff
selftests/bpf: Fix array bounds warning in jit_disasm_helpers
authorIhor Solodrai <ihor.solodrai@linux.dev>
Mon, 23 Feb 2026 19:07:33 +0000 (11:07 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 24 Feb 2026 16:19:49 +0000 (08:19 -0800)
Compiler cannot infer upper bound for labels.cnt and warns about
potential buffer overflow in snprintf. Add an explicit bounds
check (... && i < MAX_LOCAL_LABELS) in the loop condition to fix the
warning.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Link: https://lore.kernel.org/r/20260223190736.649171-18-ihor.solodrai@linux.dev
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/jit_disasm_helpers.c

index febd6b12e372db9ee9aca985e81a8605e7d8fdf3..364c557c51158dc36acd901e489f40c297aa7b94 100644 (file)
@@ -122,15 +122,15 @@ static int disasm_one_func(FILE *text_out, uint8_t *image, __u32 len)
                pc += cnt;
        }
        qsort(labels.pcs, labels.cnt, sizeof(*labels.pcs), cmp_u32);
-       for (i = 0; i < labels.cnt; ++i)
-               /* gcc is unable to infer upper bound for labels.cnt and assumes
-                * it to be U32_MAX. U32_MAX takes 10 decimal digits.
-                * snprintf below prints into labels.names[*],
-                * which has space only for two digits and a letter.
-                * To avoid truncation warning use (i % MAX_LOCAL_LABELS),
-                * which informs gcc about printed value upper bound.
-                */
-               snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % MAX_LOCAL_LABELS);
+       /* gcc is unable to infer upper bound for labels.cnt and
+        * assumes it to be U32_MAX. U32_MAX takes 10 decimal digits.
+        * snprintf below prints into labels.names[*], which has space
+        * only for two digits and a letter.  To avoid truncation
+        * warning use (i < MAX_LOCAL_LABELS), which informs gcc about
+        * printed value upper bound.
+        */
+       for (i = 0; i < labels.cnt && i < MAX_LOCAL_LABELS; ++i)
+               snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i);
 
        /* now print with labels */
        labels.print_phase = true;