Skip to content

terminate print_string_ptr output at the real end#1976

Merged
charles-lunarg merged 2 commits into
KhronosGroup:mainfrom
aizu-m:print-string-ptr-terminate
Jul 16, 2026
Merged

terminate print_string_ptr output at the real end#1976
charles-lunarg merged 2 commits into
KhronosGroup:mainfrom
aizu-m:print-string-ptr-terminate

Conversation

@aizu-m

@aizu-m aizu-m commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

loader_cJSON_PrintPreallocated into a poisoned 64-byte buffer, string value "ab\ncd\ef":

expected_len=8 printed_len=10
bytes: 61 62 0a 63 64 5c 65 66 58 58

The trailing 58 58 are two stale 'X' bytes from the caller buffer that end up inside the returned string.

print_string_ptr is the loader's fork of the cJSON string serialiser. A post-release change (commented at the escape switch) drops the leading backslash, so each escaped character writes one byte where the upstream budget reserved two, and five rather than six for a control-char uXXXX form. output_length still holds the upstream budget, so the copy loop stops short of it, yet the terminator is written at output[output_length] instead of at the real end. Whatever sits in the gap is read back by the following strlen/update_offset.

I only noticed it because the default path hides it: the print buffer is calloc'd and loader_realloc zeroes its grown tail, so the gap reads as zeros and strlen stops early. Two cases break that. PrintPreallocated writes into a caller-owned buffer that the header explicitly says the caller owns and need not zero. And loader_cJSON_Print grows through loader_realloc, whose app-allocator (pfnReallocation) branch does not zero the grown bytes, so a custom VkAllocationCallbacks leaves the gap as uninitialised heap. The stale bytes are appended to manifest-derived strings such as library_path and the layer name.

Fix terminates at the actual end (output_pointer). The added test prints an escaped string into a poisoned preallocated buffer; it shows the stale bytes before the change and a correctly terminated string after.

@ci-tester-lunarg

Copy link
Copy Markdown

Author aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

1 similar comment
@ci-tester-lunarg

Copy link
Copy Markdown

Author aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

@charles-lunarg

Copy link
Copy Markdown
Collaborator

Seems that you just need to run clang-format-16 on loader/cJSON.c

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 42044.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3641 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3641 passed.

@ci-tester-lunarg

Copy link
Copy Markdown

Author aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

1 similar comment
@ci-tester-lunarg

Copy link
Copy Markdown

Author aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

@charles-lunarg
charles-lunarg force-pushed the print-string-ptr-terminate branch from b65b04b to c9e4a9e Compare July 16, 2026 16:01
@ci-tester-lunarg

Copy link
Copy Markdown

Author aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

1 similar comment
@ci-tester-lunarg

Copy link
Copy Markdown

Author aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

@charles-lunarg
charles-lunarg merged commit e8b9908 into KhronosGroup:main Jul 16, 2026
50 checks passed
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