From 5b7191280b7709d51b585c4f517e7ede763e01b5 Mon Sep 17 00:00:00 2001 From: Francisco Casas Date: Mon, 6 May 2024 16:51:36 -0400 Subject: [PATCH] vkd3d-shader: Return a valid pointer when count=0 in param allocator (ubsan). After compiling and linking with '-fsanitize=undefined' the following error pops up in many tests: vkd3d_shader_main.c:2024:12: runtime error: member access within null pointer of type 'struct vkd3d_shader_param_node' This happens in the scenario where shader_param_allocator_get() gets called with 'count = 0' but no allocation has been made yet, so allocator->current is NULL. In this case the result of the function, given by: params = &allocator->current->param[allocator->index * allocator->stride]; is an invalid non-NULL pointer. Functions like shader_sm4_read_instruction() may call vsir_program_get_src_params() or vsir_program_get_dst_params() with 0 counts for various DCL_ instructions, as well as things like NOP, ELSE, and SYNC. We could avoid calling the functions in question with 0 counts, but it doesn't seem worth the effort. Alternatively, we could just return NULL on 'count == 0', but this is also complicated because NULL is interpreted as a memory allocation failure on the callers. So we force allocation of the next node even if 'count = 0' when allocator->current is NULL. --- libs/vkd3d-shader/vkd3d_shader_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index a7b42217..18456480 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -2004,7 +2004,7 @@ void *shader_param_allocator_get(struct vkd3d_shader_param_allocator *allocator, { void *params; - if (count > allocator->count - allocator->index) + if (!allocator->current || count > allocator->count - allocator->index) { struct vkd3d_shader_param_node *next;