zink: Some return values of malloc should be checked
authorxurui <xurui@kylinos.cn>
Thu, 6 Apr 2023 10:11:48 +0000 (18:11 +0800)
committerMarge Bot <emma+marge@anholt.net>
Thu, 13 Apr 2023 13:40:22 +0000 (13:40 +0000)
Signed-off-by: xurui <xurui@kylinos.cn>
Reviewed-by: Mike Blumenkrantz <michael.blumenkrantz@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22328>

src/gallium/drivers/zink/zink_context.c
src/gallium/drivers/zink/zink_kopper.c
src/gallium/drivers/zink/zink_program.c
src/gallium/drivers/zink/zink_query.c
src/gallium/drivers/zink/zink_resource.c
src/gallium/drivers/zink/zink_screen.c
src/gallium/drivers/zink/zink_state.c
src/gallium/drivers/zink/zink_surface.c

index fd75875..f8ad140 100644 (file)
@@ -1013,6 +1013,11 @@ zink_create_sampler_view(struct pipe_context *pctx, struct pipe_resource *pres,
    struct zink_sampler_view *sampler_view = CALLOC_STRUCT_CL(zink_sampler_view);
    bool err;
 
+   if (!sampler_view) {
+      mesa_loge("ZINK: failed to allocate sampler_view!");
+      return NULL;
+   }
+      
    sampler_view->base = *state;
    sampler_view->base.texture = NULL;
    pipe_resource_reference(&sampler_view->base.texture, pres);
@@ -4993,14 +4998,26 @@ zink_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags)
          util_idalloc_alloc(&ctx->di.bindless[i].img_slots);
          if (zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_DB) {
             ctx->di.bindless[i].db.buffer_infos = malloc(sizeof(VkDescriptorAddressInfoEXT) * ZINK_MAX_BINDLESS_HANDLES);
+            if (!ctx->di.bindless[i].db.buffer_infos) {
+               mesa_loge("ZINK: failed to allocate ctx->di.bindless[%d].db.buffer_infos!",i);
+               goto fail;
+            }
             for (unsigned j = 0; j < ZINK_MAX_BINDLESS_HANDLES; j++) {
                ctx->di.bindless[i].db.buffer_infos[j].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_ADDRESS_INFO_EXT;
                ctx->di.bindless[i].db.buffer_infos[j].pNext = NULL;
             }
          } else {
             ctx->di.bindless[i].t.buffer_infos = malloc(sizeof(VkBufferView) * ZINK_MAX_BINDLESS_HANDLES);
+            if (!ctx->di.bindless[i].t.buffer_infos) {
+               mesa_loge("ZINK: failed to allocate ctx->di.bindless[%d].t.buffer_infos!",i);
+               goto fail;
+            }
          }
          ctx->di.bindless[i].img_infos = malloc(sizeof(VkDescriptorImageInfo) * ZINK_MAX_BINDLESS_HANDLES);
+         if (!ctx->di.bindless[i].img_infos) {
+            mesa_loge("ZINK: failed to allocate ctx->di.bindless[%d].img_infos!",i);
+            goto fail;
+         }
          util_dynarray_init(&ctx->di.bindless[i].updates, NULL);
          util_dynarray_init(&ctx->di.bindless[i].resident, NULL);
       }
index 09ca173..05f045d 100644 (file)
@@ -323,6 +323,10 @@ kopper_GetSwapchainImages(struct zink_screen *screen, struct kopper_swapchain *c
    if (error != VK_SUCCESS)
       return error;
    cswap->images = calloc(cswap->num_images, sizeof(struct kopper_swapchain_image));
+   if (!cswap->images) {
+      mesa_loge("ZINK: failed to allocate cswap->images!");
+      return VK_ERROR_OUT_OF_HOST_MEMORY;
+   }
    cswap->presents = _mesa_hash_table_create_u32_keys(NULL);
    VkImage images[32];
    error = VKSCR(GetSwapchainImagesKHR)(screen->dev, cswap->swapchain, &cswap->num_images, images);
@@ -720,6 +724,11 @@ kopper_present(void *data, void *gdata, int thread_idx)
       arr = he->data;
    else {
       arr = malloc(sizeof(struct util_dynarray));
+      if (!arr) {
+         mesa_loge("ZINK: failed to allocate arr!");
+         return;
+      }
+
       util_dynarray_init(arr, NULL);
       _mesa_hash_table_insert(swapchain->presents, (void*)(uintptr_t)next, arr);
    }
@@ -746,6 +755,11 @@ zink_kopper_present_queue(struct zink_screen *screen, struct zink_resource *res)
       prune_old_swapchains(screen, cdt, false);
 
    struct kopper_present_info *cpi = malloc(sizeof(struct kopper_present_info));
+   if (!cpi) {
+      mesa_loge("ZINK: failed to allocate cpi!");
+      return;
+   }
+      
    cpi->sem = res->obj->present;
    cpi->res = res;
    cpi->swapchain = cdt->swapchain;
index e6a95c0..66bca2d 100644 (file)
@@ -1196,6 +1196,10 @@ create_gfx_program_separable(struct zink_context *ctx, struct zink_shader **stag
    prog->last_variant_hash = ctx->gfx_pipeline_state.optimal_key;
 
    struct zink_gfx_library_key *gkey = CALLOC_STRUCT(zink_gfx_library_key);
+   if (!gkey) {
+      mesa_loge("ZINK: failed to allocate gkey!");
+      goto fail;
+   }
    gkey->optimal_key = prog->last_variant_hash;
    assert(gkey->optimal_key);
    gkey->pipeline = zink_create_gfx_pipeline_combined(screen, prog, VK_NULL_HANDLE, libs, 2, VK_NULL_HANDLE, false);
@@ -1927,6 +1931,11 @@ struct zink_gfx_library_key *
 zink_create_pipeline_lib(struct zink_screen *screen, struct zink_gfx_program *prog, struct zink_gfx_pipeline_state *state)
 {
    struct zink_gfx_library_key *gkey = CALLOC_STRUCT(zink_gfx_library_key);
+   if (!gkey) {
+      mesa_loge("ZINK: failed to allocate gkey!");
+      return NULL;
+   }
+      
    gkey->optimal_key = state->optimal_key;
    assert(gkey->optimal_key);
    memcpy(gkey->modules, prog->modules, sizeof(gkey->modules));
@@ -1988,6 +1997,11 @@ print_pipeline_stats(struct zink_screen *screen, VkPipeline pipeline)
       VkPipelineExecutableStatisticKHR *stats = NULL;
       VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, NULL);
       stats = calloc(count, sizeof(VkPipelineExecutableStatisticKHR));
+      if (!stats) {
+         mesa_loge("ZINK: failed to allocate stats!");
+         return;
+      }
+         
       for (unsigned i = 0; i < count; i++)
          stats[i].sType = VK_STRUCTURE_TYPE_PIPELINE_EXECUTABLE_STATISTIC_KHR;
       VKSCR(GetPipelineExecutableStatisticsKHR)(screen->dev, &info, &count, stats);
index 3370b49..662e3d4 100644 (file)
@@ -474,6 +474,10 @@ query_pool_get_range(struct zink_context *ctx, struct zink_query *q)
             pool = find_or_allocate_qp(ctx, q, pool_idx);
          }
          vkq = CALLOC_STRUCT(zink_vk_query);
+         if (!vkq) {
+            mesa_loge("ZINK: failed to allocate vkq!");
+            return;
+         }
 
          vkq->refcount = 1;
          vkq->needs_reset = true;
index 18886c6..5af0b0d 100644 (file)
@@ -671,6 +671,11 @@ resource_object_create(struct zink_screen *screen, const struct pipe_resource *t
    pipe_reference_init(&obj->reference, 1);
    if (loader_private) {
       obj->bo = CALLOC_STRUCT(zink_bo);
+      if (!obj->bo) {
+         mesa_loge("ZINK: failed to allocate obj->bo!");
+         return NULL;
+      }
+         
       obj->transfer_dst = true;
       return obj;
    } else if (templ->target == PIPE_BUFFER) {
@@ -1169,6 +1174,11 @@ resource_create(struct pipe_screen *pscreen,
    struct zink_screen *screen = zink_screen(pscreen);
    struct zink_resource *res = CALLOC_STRUCT_CL(zink_resource);
 
+   if (!res) {
+      mesa_loge("ZINK: failed to allocate res!");
+      return NULL;
+   }
+
    if (modifiers_count > 0 && screen->info.have_EXT_image_drm_format_modifier) {
       /* for rebinds */
       res->modifiers_count = modifiers_count;
@@ -1334,6 +1344,11 @@ add_resource_bind(struct zink_context *ctx, struct zink_resource *res, unsigned
    if (bind & ZINK_BIND_DMABUF && !res->modifiers_count && screen->info.have_EXT_image_drm_format_modifier) {
       res->modifiers_count = 1;
       res->modifiers = malloc(res->modifiers_count * sizeof(uint64_t));
+      if (!res->modifiers) {
+         mesa_loge("ZINK: failed to allocate res->modifiers!");
+         return false;
+      }
+
       res->modifiers[0] = DRM_FORMAT_MOD_LINEAR;
    }
    struct zink_resource_object *new_obj = resource_object_create(screen, &res->base.b, NULL, &res->linear, res->modifiers, res->modifiers_count, NULL);
index 8825f07..3650e39 100644 (file)
@@ -1511,6 +1511,10 @@ choose_pdev(struct zink_screen *screen)
       assert(pdev_count > 0);
 
       pdevs = malloc(sizeof(*pdevs) * pdev_count);
+      if (!pdevs) {
+         mesa_loge("ZINK: failed to allocate pdevs!");
+         return;
+      }
       result = VKSCR(EnumeratePhysicalDevices)(screen->instance, &pdev_count, pdevs);
       assert(result == VK_SUCCESS);
       assert(pdev_count > 0);
@@ -1570,6 +1574,11 @@ update_queue_props(struct zink_screen *screen)
    assert(num_queues > 0);
 
    VkQueueFamilyProperties *props = malloc(sizeof(*props) * num_queues);
+   if (!props) {
+      mesa_loge("ZINK: failed to allocate props!");
+      return;
+   }
+      
    VKSCR(GetPhysicalDeviceQueueFamilyProperties)(screen->pdev, &num_queues, props);
 
    bool found_gfx = false;
index 18b1423..d702c08 100644 (file)
@@ -735,6 +735,10 @@ zink_create_vertex_state(struct pipe_screen *pscreen,
                           uint32_t full_velem_mask)
 {
    struct zink_vertex_state *zstate = CALLOC_STRUCT(zink_vertex_state);
+   if (!zstate) {
+      mesa_loge("ZINK: failed to allocate zstate!");
+      return NULL;
+   }
 
    util_init_pipe_vertex_state(pscreen, buffer, elements, num_elements, indexbuf, full_velem_mask,
                                &zstate->b);
index 38eb77b..596c0f0 100644 (file)
@@ -256,6 +256,11 @@ static struct pipe_surface *
 wrap_surface(struct pipe_context *pctx, const struct pipe_surface *psurf)
 {
    struct zink_ctx_surface *csurf = CALLOC_STRUCT(zink_ctx_surface);
+   if (!csurf) {
+      mesa_loge("ZINK: failed to allocate csurf!");
+      return NULL;
+   }
+      
    csurf->base = *psurf;
    pipe_reference_init(&csurf->base.reference, 1);
    csurf->surf = (struct zink_surface*)psurf;
@@ -493,6 +498,10 @@ zink_surface_swapchain_update(struct zink_context *ctx, struct zink_surface *sur
       free(surface->swapchain);
       surface->swapchain_size = cdt->swapchain->num_images;
       surface->swapchain = calloc(surface->swapchain_size, sizeof(VkImageView));
+      if (!surface->swapchain) {
+         mesa_loge("ZINK: failed to allocate surface->swapchain!");
+         return;
+      }
       surface->base.width = res->base.b.width0;
       surface->base.height = res->base.b.height0;
       init_surface_info(surface, res, &surface->ivci);