-
Notifications
You must be signed in to change notification settings - Fork 343
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
Fix realloc memory leak #1458
base: production
Are you sure you want to change the base?
Fix realloc memory leak #1458
Conversation
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.
Seems fine. Should it attempt to still log however many bytes were written to the buffer before it failed to realloc to try and eke out potentially useful logs in an OOM situation? If realloc actually returns nullptr all bets are off though. Overcommit means that it's unlikely to ever actually happen (allocation will succeed, program will just trap on access).
I'm not sure there's anything guaranteed to be written if there's not enough space: "A negative number is returned on failure, including when the resulting string to be written to ws would be longer than n characters." |
Nothing is guaranteed, but the implementation in glibc writes directly to the buffer as it goes, so you will likely have some large fraction of This is mostly an idle thought - I think we are unlikely to hit this codepath regardless. |
wchar_t* wsnext = (wchar_t*)realloc(wsallocated, wslen*sizeof(wchar_t)); \ | ||
/* realloc() can return NULL, need to remember previous allocation to avoid memory leaks */ \ | ||
if (wsnext != NULL) { \ | ||
wsallocated = wsnext; \ |
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.
I'm confused here. Don't you have to change wslen
back to the original value? Otherwise vswprintf
will happily write beyond the buffer, and now we have memory corruption.
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.
When we get a NULL
, wr
becomes -1
and we get straight to this part (skipping printing):
if (wsallocated != NULL) { \
free(wsallocated); \
wsallocated = NULL; \
} \
realloc()
may returnNULL
, so when we do:we risk leaking memory, better implementation would be: