From a18171968cb84e54fd939d35cd08262fb56ed2a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20No=C3=ABl?= Date: Mon, 8 Jan 2024 12:02:14 +0100 Subject: [PATCH] zink: Avoid the use of negative array offsets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix defect reported by Coverity Scan. Negative array index read A memory location at a negative offset from the beginning of the array will be read, resulting in incorrect values. CID: 1515600 Signed-off-by: Corentin Noël Part-of: --- src/gallium/drivers/zink/zink_clear.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/gallium/drivers/zink/zink_clear.c b/src/gallium/drivers/zink/zink_clear.c index dc2ca1d1c11..f38d5f4d15e 100644 --- a/src/gallium/drivers/zink/zink_clear.c +++ b/src/gallium/drivers/zink/zink_clear.c @@ -878,8 +878,13 @@ zink_fb_clear_rewrite(struct zink_context *ctx, unsigned idx, enum pipe_format b */ const struct util_format_description *bdesc = util_format_description(before); const struct util_format_description *adesc = util_format_description(after); - bool bsigned = bdesc->channel[util_format_get_first_non_void_channel(before)].type == UTIL_FORMAT_TYPE_SIGNED; - bool asigned = adesc->channel[util_format_get_first_non_void_channel(after)].type == UTIL_FORMAT_TYPE_SIGNED; + int bfirst_non_void_chan = util_format_get_first_non_void_channel(before); + int afirst_non_void_chan = util_format_get_first_non_void_channel(after); + bool bsigned = false, asigned = false; + if (bfirst_non_void_chan > 0) + bsigned = bdesc->channel[bfirst_non_void_chan].type == UTIL_FORMAT_TYPE_SIGNED; + if (afirst_non_void_chan > 0) + asigned = adesc->channel[afirst_non_void_chan].type == UTIL_FORMAT_TYPE_SIGNED; if (util_format_is_srgb(before) == util_format_is_srgb(after) && bsigned == asigned) return;