Module: Mesa
Branch: main
Commit: a18171968cb84e54fd939d35cd08262fb56ed2a3
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=a18171968cb84e54fd939d35cd08262fb56ed2a3

Author: Corentin Noël <corentin.n...@collabora.com>
Date:   Mon Jan  8 12:02:14 2024 +0100

zink: Avoid the use of negative array offsets

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 <corentin.n...@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26922>

---

 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;

Reply via email to