-
Notifications
You must be signed in to change notification settings - Fork 760
Add null pointer check in XNNWeightsCache::look_up_or_insert #16242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,6 +208,12 @@ size_t XNNWeightsCache::look_up_or_insert( | |
|
|
||
| if (offset != SIZE_MAX) { | ||
| void* saved_ptr = context->offset_to_addr(context, offset); | ||
| // Check for null pointers before calling memcmp | ||
| if (ptr == nullptr || saved_ptr == nullptr) { | ||
| // If either pointer is null (due to deleted data or allocation failure), treat as cache miss | ||
| // and return SIZE_MAX to trigger reinsertion. | ||
| return SIZE_MAX; | ||
| } | ||
|
Comment on lines
+211
to
+216
|
||
| if (0 == memcmp(ptr, saved_ptr, size)) { | ||
| return offset; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The null pointer check prevents an immediate crash, but introduces a potential issue when
ptris null. Ifptris null and SIZE_MAX is returned, the function continues to the insertion path (lines 224-252) whereptrwill be stored inpacked_data_ptrs_at line 250. This means a null pointer gets cached, which could cause the same crash later when another call tolook_up_or_insertretrieves it viaoffset_to_addr.Consider handling the two null scenarios differently:
saved_ptris null (cache corruption/deletion), returning SIZE_MAX to re-insert is correctptris null (XNNPACK packing failure), the function should either return an error/special value that doesn't trigger insertion, or validateptris non-null before insertion at line 250A safer approach would be to check if
ptris null before attempting insertion and handle it as an error case, while only checkingsaved_ptrfor null at line 212.