Skip to content

Guard against zero size allocations - #2087

Merged
charles-lunarg merged 1 commit into
KhronosGroup:mainfrom
charles-lunarg:guard_zero_size_allocations
Oct 28, 2026
Merged

charles-lunarg merged 1 commit into
KhronosGroup:mainfrom
charles-lunarg:guard_zero_size_allocations

Conversation

@charles-lunarg

Copy link
Copy Markdown
Collaborator

With the recent replacement of alloca with calloc & realloc, several places in the codebase needed extra checks to prevent calling calloc & realloc with a size of zero. Alloca() is defined to return a non-null pointer that is invalid to dereference. Calloc & realloc make no such guarantees. Some platforms return NULL, which the loader would wrongly interpret as out-of-host-memory.

Fixes are to check the count before calling the allocation function, add assertions inside the allocation functions to catch the situation in debug builds, and enable the optin.portability.UnixAPI check in clang-tidy. Clang-tidy is not robust however because of internal limits to its search depth/breadth, so was unable to find all instances of this issue.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 173219.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 173236.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3919 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3919 passed.

@charles-lunarg
charles-lunarg force-pushed the guard_zero_size_allocations branch from 773ce92 to 253d64c Compare October 27, 2026 22:47
@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 173260.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 173277.

With the recent replacement of alloca with calloc & realloc, several
places in the codebase needed extra checks to prevent calling calloc &
realloc with a size of zero. Alloca() is defined to return a non-null
pointer that is invalid to dereference. Calloc & realloc make no such
guarantees. Some platforms return NULL, which the loader would wrongly
interpret as out-of-host-memory.

Fixes are to check the count before calling the allocation function and
add assertions inside the allocation functions to catch the situation
in debug builds.
@charles-lunarg
charles-lunarg force-pushed the guard_zero_size_allocations branch from 253d64c to 47052da Compare October 27, 2026 22:49
@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 173278.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3922 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 173315.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3923 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3923 passed.

@aitor-lunarg aitor-lunarg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@charles-lunarg
charles-lunarg merged commit dfe4acf into KhronosGroup:main Oct 28, 2026
52 checks passed
@charles-lunarg
charles-lunarg deleted the guard_zero_size_allocations branch October 28, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants