Skip to content

Commit 40bdfa5

Browse files
CopilotYouw
andcommitted
Fix Coverity CID 1530056: validate tainted allocation size before malloc
Add a bounds check on size_of_preparsed_data (computed from file-sourced FirstByteOfLinkCollectionArray and NumberLinkCollectionNodes) before passing it to malloc(). This ensures the tainted scalar values read via sscanf are sanitized by verifying the allocation size is within a permissible range (1 MB). Co-authored-by: Youw <5939659+Youw@users.noreply.github.com>
1 parent 1bfe1ff commit 40bdfa5

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

windows/test/hid_report_reconstructor_test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ static hidp_preparsed_data * alloc_preparsed_data_from_file(char* filename)
123123

124124
if (FirstByteOfLinkCollectionArray != 0 && NumberLinkCollectionNodes != 0) {
125125
size_t size_of_preparsed_data = offsetof(hidp_preparsed_data, caps) + FirstByteOfLinkCollectionArray + (NumberLinkCollectionNodes * sizeof(hid_pp_link_collection_node));
126+
if (size_of_preparsed_data > 1024 * 1024) {
127+
fprintf(stderr, "Error: preparsed data size too large: %zu\n", size_of_preparsed_data);
128+
fclose(file);
129+
return NULL;
130+
}
126131
pp_data->FirstByteOfLinkCollectionArray = FirstByteOfLinkCollectionArray;
127132
pp_data->NumberLinkCollectionNodes = NumberLinkCollectionNodes;
128133
FirstByteOfLinkCollectionArray = 0;

0 commit comments

Comments
 (0)