C-Allocators: Mention realloc
behavior if size is zero
Signed-off-by: Daiki Ueno <dueno@redhat.com>
This commit is contained in:
parent
c253c7d93e
commit
3a1876bdaf
1 changed files with 14 additions and 0 deletions
|
@ -43,6 +43,20 @@ compiler may assume that a comparison between the old and new
|
|||
pointer will always return false, so it is impossible to detect
|
||||
movement this way.
|
||||
|
||||
On a related note, `realloc` frees the memory area if the new size is
|
||||
zero. If the size unintentionally becomes zero, as a result of
|
||||
unsigned integer wrap-around for instance, the following idiom causes
|
||||
a double-free.
|
||||
|
||||
[source,c]
|
||||
----
|
||||
new_size = size + x; /* 'x' is a very large value and the result wraps around to zero */
|
||||
new_ptr = realloc(ptr, new_size);
|
||||
if (!new_ptr) {
|
||||
free(ptr);
|
||||
}
|
||||
----
|
||||
|
||||
==== Handling Memory Allocation Errors
|
||||
|
||||
Recovering from out-of-memory errors is often difficult or even
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue