Skip to content
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

LibC: fix realloc in case of allocation failure #3108

Merged
merged 1 commit into from
Aug 13, 2020

Conversation

tryfinally
Copy link
Contributor

If the space cannot be allocated, the original memory block shall remain
unchanged and the function should return null

@awesomekling
Copy link
Collaborator

Note that malloc() does not fail; it will crash if unable to allocate.

@tryfinally
Copy link
Contributor Author

currently malloc_impl can return null , see commit 4291e96

void* realloc(void* ptr, size_t);
__attribute__((malloc)) __attribute__((alloc_size(2))) void* realloc(void* ptr, size_t);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right. From GCC documentation:

This tells the compiler that a function is malloc-like, i.e., that the pointer P returned by the function cannot alias any other pointer valid when the function returns, and moreover no pointers to valid objects occur in any storage addressed by P.

Using this attribute can improve optimization. Compiler predicts that a function with the attribute returns non-null in most cases. Functions like malloc and calloc have this property because they return a pointer to uninitialized or zeroed-out storage. However, functions like realloc do not have this property, as they can return a pointer to storage containing pointers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I missed that the definition of the malloc attributes was changed in recent gcc version
Up to gcc version 4.64 it was applicable.
https://gcc.gnu.org/onlinedocs/gcc-4.6.4/gcc/Function-Attributes.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@awesomekling why is it so mem allocation will crash the app? isn't it more appropriate to let the application decide how to proceed?
if malloc/calloc/realloc will never fail, then may we should mark them as returns_nonnull

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The majority of the system (both apps and libraries) is designed around many small heap-allocated objects. It would be massively obnoxious to have to null-check allocation everywhere. It's also far from obvious what to do in each case where an allocation might fail.

The thing that tends to matter in practice is large allocations and those generally go through mmap which can fail (if we run out of virtual address space, for example.)

If the space cannot be allocated, the original memory block shall remain
unchanged and the function should return null

LibC: fix realloc in case of reuested size== 0

make realloc function ISO C compliant

LibC: add missing attributes to realloc

LibCore: calloc: check for null before memset

LibC: mark realloc with alloc_size attribute
@awesomekling awesomekling merged commit 9a56d40 into SerenityOS:master Aug 13, 2020
awesomekling pushed a commit that referenced this pull request Aug 13, 2020
If the space cannot be allocated, the original memory block shall remain
unchanged and the function should return nullptr.

Also add a function attribute and some null checks.
@tryfinally tryfinally deleted the dev-libC-realloc-fix branch August 14, 2020 12:08
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.

None yet

2 participants