tu,radv: fix potentially wrong offset of flexible array.
authorHyunjun Ko <zzoon@igalia.com>
Wed, 1 Jul 2020 03:13:40 +0000 (03:13 +0000)
committerMarge Bot <eric+marge@anholt.net>
Fri, 3 Jul 2020 00:45:16 +0000 (00:45 +0000)
v2. Remove redundant memset and make the expression simpler.

Signed-off-by: Hyunjun Ko <zzoon@igalia.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5703>

src/amd/vulkan/radv_descriptor_set.c
src/freedreno/vulkan/tu_descriptor_set.c

index 21b9de72efb0aaffce9d564de4de080bf88f75d8..241ce2c33c6436669637dc4214a92034dfa037c3 100644 (file)
@@ -102,8 +102,8 @@ VkResult radv_CreateDescriptorSetLayout(
                }
        }
 
-       uint32_t samplers_offset = sizeof(struct radv_descriptor_set_layout) +
-               (max_binding + 1) * sizeof(set_layout->binding[0]);
+       uint32_t samplers_offset =
+                       offsetof(struct radv_descriptor_set_layout, binding[max_binding + 1]);
        size_t size = samplers_offset + immutable_sampler_count * 4 * sizeof(uint32_t);
        if (ycbcr_sampler_count > 0) {
                size += ycbcr_sampler_count * sizeof(struct radv_sampler_ycbcr_conversion) + (max_binding + 1) * sizeof(uint32_t);
@@ -146,8 +146,6 @@ VkResult radv_CreateDescriptorSetLayout(
        set_layout->has_immutable_samplers = false;
        set_layout->size = 0;
 
-       memset(set_layout->binding, 0, size - sizeof(struct radv_descriptor_set_layout));
-
        uint32_t buffer_count = 0;
        uint32_t dynamic_offset_count = 0;
 
index 314088cefd90b01327585d5d1dd735739f1291d4..ec2a940e858fc7c01fe6b7e1acfc5858f1544a13 100644 (file)
@@ -139,15 +139,16 @@ tu_CreateDescriptorSetLayout(
       }
    }
 
-   uint32_t samplers_offset = sizeof(struct tu_descriptor_set_layout) +
-      (max_binding + 1) * sizeof(set_layout->binding[0]);
+   uint32_t samplers_offset =
+         offsetof(struct tu_descriptor_set_layout, binding[max_binding + 1]);
+
    /* note: only need to store TEX_SAMP_DWORDS for immutable samples,
     * but using struct tu_sampler makes things simpler */
    uint32_t size = samplers_offset +
       immutable_sampler_count * sizeof(struct tu_sampler) +
       ycbcr_sampler_count * sizeof(struct tu_sampler_ycbcr_conversion);
 
-   set_layout = vk_alloc2(&device->alloc, pAllocator, size, 8,
+   set_layout = vk_zalloc2(&device->alloc, pAllocator, size, 8,
                           VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
    if (!set_layout)
       return vk_error(device->instance, VK_ERROR_OUT_OF_HOST_MEMORY);
@@ -172,9 +173,6 @@ tu_CreateDescriptorSetLayout(
    set_layout->size = 0;
    set_layout->dynamic_ubo = 0;
 
-   memset(set_layout->binding, 0,
-          size - sizeof(struct tu_descriptor_set_layout));
-
    uint32_t dynamic_offset_count = 0;
    uint32_t buffer_count = 0;