Skip to content

vec: remove capacity == 0 early return - #176

Merged
tylov merged 1 commit into
stclib:mainfrom
wmww:zero-size-malloc
Sep 28, 2026
Merged

tylov merged 1 commit into
stclib:mainfrom
wmww:zero-size-malloc

Conversation

@wmww

@wmww wmww commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

*_with_capacity(0) allocates, and malloc may return a valid pointer that needs to be freed, even when 0 size is specified. This behavior might be valgrind-specifiec? but malloc(0) is indeed implemenation-defined. An alternative would be to have cap ? _i_new_n(_m_value, cap) : NULL in the *_with_capacity implementation, and a few other places.

*_with_capacity(0) allocates, and malloc may return a valid pointer that
needs to be freed, even when 0 size is specified.
@tylov

tylov commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks for making me aware. I will accept the commit, but will modify the code to test if the data pointer is NULL instead of removing the test. This is to avoid calling the user-provided free-function with NULL argument. free() does allows this, but the user may assume there is a non-NULL pointer passed, which may lead to UB (like memset(NULL, 0, 0)). Next commit updates the other relevant containers the same way.

@tylov
tylov merged commit a3a55d0 into stclib:main Sep 28, 2026
22 checks passed
tylov added a commit that referenced this pull request Sep 28, 2026
- Fixes the _drop()/_clear() functions for other contiguous data containers than vec.h and updates the original PR for vec.h.
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.

2 participants