Mesa (main): zink: avoid overflow when calculating size

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Aug 27 18:42:40 UTC 2021


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

Author: Erik Faye-Lund <erik.faye-lund at collabora.com>
Date:   Wed Aug 25 21:44:46 2021 +0200

zink: avoid overflow when calculating size

If we multiply before we (implicitly) cast the result to the target
type, we needlessly risk overflowing the result.

CID: 1490790, 1475922

Reviewed-By: Mike Blumenkrantz <michael.blumenkrantz at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12559>

---

 src/gallium/drivers/zink/zink_resource.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/gallium/drivers/zink/zink_resource.c b/src/gallium/drivers/zink/zink_resource.c
index bd4415ee42a..67fb4aae982 100644
--- a/src/gallium/drivers/zink/zink_resource.c
+++ b/src/gallium/drivers/zink/zink_resource.c
@@ -1317,7 +1317,7 @@ zink_image_map(struct pipe_context *pctx,
                         (box->y / desc->block.height) * srl.rowPitch +
                         (box->x / desc->block.width) * (desc->block.bits / 8);
       if (!res->obj->coherent) {
-         VkDeviceSize size = box->width * box->height * desc->block.bits / 8;
+         VkDeviceSize size = (VkDeviceSize)box->width * box->height * desc->block.bits / 8;
          VkMappedMemoryRange range = zink_resource_init_mem_range(screen, res->obj, res->obj->offset + offset, size);
          vkFlushMappedMemoryRanges(screen->dev, 1, &range);
       }
@@ -1357,7 +1357,7 @@ zink_transfer_flush_region(struct pipe_context *pctx,
          size = box->width;
          offset = trans->offset;
       } else {
-         size = box->width * box->height * util_format_get_blocksize(m->base.b.format);
+         size = (VkDeviceSize)box->width * box->height * util_format_get_blocksize(m->base.b.format);
          offset = trans->offset +
                   box->z * trans->depthPitch +
                   util_format_get_2d_size(m->base.b.format, trans->base.b.stride, box->y) +



More information about the mesa-commit mailing list