From 38e1a43f53d62260dcff1c6c5973eaf4514eb792 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Tue, 7 Oct 2025 18:36:22 +0000 Subject: [PATCH] intel/mda: Fix potential underflow in printing code The actual chances of this happening seem dubious, but the cleaned up code seems nice. printf returns a value >= 0 on success, which is the number of characters it writes a return < 0 means that an error occurred, and then errno is set. Which negative value doesn't seem to be specified, but it also seems unlikely that any implementation would return `-MAX_INT`... Anyway, this is fixed by converting the generic `print_repeated` to a `print_separator` that avoids the need to do arithmetic at all by just stopping the loop at 1 instead of 0, and then printing a newline. CID: 1666497 CID: 1666256 CID: 1666531 Reviewed-by: Caio Oliveira Part-of: --- src/intel/mda/mda.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/intel/mda/mda.c b/src/intel/mda/mda.c index 5a6446a755c..1bef29c3e40 100644 --- a/src/intel/mda/mda.c +++ b/src/intel/mda/mda.c @@ -165,10 +165,12 @@ last_version(object *obj) } static void -print_repeated(char c, int count) +print_separator(int count) { - for (; count > 0; count--) - putchar(c); + /* Negative numbers always result in just a new line*/ + for (; count > 1; count--) + putchar('#'); + putchar('\n'); } static mesa_archive * @@ -585,8 +587,8 @@ cmd_diff(context *ctx) int x = printf("# A: %.*s\n", SLICE_FMT(a.content->fullname)); int y = printf("# B: %.*s\n", SLICE_FMT(b.content->fullname)); - print_repeated('#', MAX2(x, y) - 1); - printf("\n\n"); + print_separator(MAX2(x, y)); + printf("\n"); diff(ctx, a.content->data, b.content->data); printf("\n"); @@ -647,8 +649,8 @@ cmd_log(context *ctx) for (const content *c = start.content; c <= end.content; c++) { int x = printf("# %.*s/\n", SLICE_FMT(obj->fullname)); int y = printf("# %.*s\n", SLICE_FMT(c->name)); - print_repeated('#', MAX2(x, y) - 1); - printf("\n\n"); + print_separator(MAX2(x, y)); + printf("\n"); printf("%.*s\n", SLICE_FMT(c->data)); } @@ -659,8 +661,8 @@ cmd_log(context *ctx) int x = printf("# %.*s/\n", SLICE_FMT(obj->fullname)); int y = printf("# %.*s -> %.*s\n", SLICE_FMT(c->name), SLICE_FMT(next->name)); - print_repeated('#', MAX2(x, y) - 1); - printf("\n\n"); + print_separator(MAX2(x, y)); + printf("\n"); diff(ctx, c->data, next->data); printf("\n"); @@ -788,8 +790,8 @@ cmd_print(context *ctx) return print_disassembled_spirv(ctx, m.object); int x = printf("### %.*s\n", SLICE_FMT(m.content->fullname)); - print_repeated('#', x-1); - printf("\n\n"); + print_separator(x); + printf("\n"); } printf("%.*s", SLICE_FMT(m.content->data));