Skip to content

Commit afe1423

Browse files
committed
SplHeap: Avoid memcpy on overlapping pointer
Check if data would overlap and also add an assert. Previous implementations didn't have this issue, as the direct assignment was used. Signed-off-by: Anatol Belski <[email protected]>
1 parent 525d8a8 commit afe1423

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

ext/spl/spl_heap.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ static zend_always_inline void *spl_heap_elem(spl_ptr_heap *heap, size_t i) {
9898
}
9999

100100
static zend_always_inline void spl_heap_elem_copy(spl_ptr_heap *heap, void *to, void *from) {
101+
assert(to != from);
101102
memcpy(to, from, heap->elem_size);
102103
}
103104

@@ -333,7 +334,10 @@ static int spl_ptr_heap_delete_top(spl_ptr_heap *heap, void *elem, void *cmp_use
333334
heap->flags |= SPL_HEAP_CORRUPTED;
334335
}
335336

336-
spl_heap_elem_copy(heap, spl_heap_elem(heap, i), bottom);
337+
void *to = spl_heap_elem(heap, i);
338+
if (to != bottom) {
339+
spl_heap_elem_copy(heap, to, bottom);
340+
}
337341
return SUCCESS;
338342
}
339343
/* }}} */

0 commit comments

Comments
 (0)