zink: avoid overflow when calculating size
authorErik Faye-Lund <erik.faye-lund@collabora.com>
Wed, 25 Aug 2021 19:44:46 +0000 (21:44 +0200)
committerMarge Bot <eric+marge@anholt.net>
Fri, 27 Aug 2021 18:32:03 +0000 (18:32 +0000)
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@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12559>

src/gallium/drivers/zink/zink_resource.c

index bd4415e..67fb4aa 100644 (file)
@@ -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) +