Skip to content

Commit 3e711c8

Browse files
theihorAlexei Starovoitov
authored andcommitted
selftests/bpf: Fix array bounds warning in jit_disasm_helpers
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>
1 parent 2bb270a commit 3e711c8

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

tools/testing/selftests/bpf/jit_disasm_helpers.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ static int disasm_one_func(FILE *text_out, uint8_t *image, __u32 len)
122122
pc += cnt;
123123
}
124124
qsort(labels.pcs, labels.cnt, sizeof(*labels.pcs), cmp_u32);
125-
for (i = 0; i < labels.cnt; ++i)
126-
/* gcc is unable to infer upper bound for labels.cnt and assumes
127-
* it to be U32_MAX. U32_MAX takes 10 decimal digits.
128-
* snprintf below prints into labels.names[*],
129-
* which has space only for two digits and a letter.
130-
* To avoid truncation warning use (i % MAX_LOCAL_LABELS),
131-
* which informs gcc about printed value upper bound.
132-
*/
133-
snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i % MAX_LOCAL_LABELS);
125+
/* gcc is unable to infer upper bound for labels.cnt and
126+
* assumes it to be U32_MAX. U32_MAX takes 10 decimal digits.
127+
* snprintf below prints into labels.names[*], which has space
128+
* only for two digits and a letter. To avoid truncation
129+
* warning use (i < MAX_LOCAL_LABELS), which informs gcc about
130+
* printed value upper bound.
131+
*/
132+
for (i = 0; i < labels.cnt && i < MAX_LOCAL_LABELS; ++i)
133+
snprintf(labels.names[i], sizeof(labels.names[i]), "L%d", i);
134134

135135
/* now print with labels */
136136
labels.print_phase = true;

0 commit comments

Comments
 (0)