libc/stdio: allocate a buffer in getdelim() when *lineptr is NULL - #20034
Open
xiaoxiang781216 wants to merge 1 commit into
Open
libc/stdio: allocate a buffer in getdelim() when *lineptr is NULL#20034xiaoxiang781216 wants to merge 1 commit into
xiaoxiang781216 wants to merge 1 commit into
Conversation
POSIX requires getdelim()/getline() to allocate a new buffer whenever *lineptr is NULL, regardless of the value of *n. The previous code read the buffer size from *n unconditionally and only fell back to the initial size when *n was zero, so a caller that passes *lineptr == NULL together with an uninitialized (non-zero) *n caused lib_malloc() to be invoked with that garbage size and typically fail with ENOMEM. Treat a NULL *lineptr the same as a zero *n: (re)allocate from the known BUFSIZE_INIT and ignore the untrusted *n. This matches the glibc behaviour that portable code relies on (for example toybox grep, which calls getdelim() with an uninitialized size variable). Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
acassis
reviewed
Sep 1, 2026
acassis
left a comment
Contributor
There was a problem hiding this comment.
@xiaoxiang781216 please include a getdelim() test to apps, git grep getdelim at nuttx-apps returns nothing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POSIX requires
getdelim()/getline()to allocate a new buffer whenever*lineptris NULL, regardless of the value of*n. The previous code read the buffer size from*nunconditionally and only fell back to the initial size when*nwas zero, so a caller that passes*lineptr == NULLtogether with an uninitialized (non-zero)*ncausedlib_malloc()to be invoked with that garbage size and typically fail withENOMEM.Treat a NULL
*lineptrthe same as a zero*n: (re)allocate from the knownBUFSIZE_INITand ignore the untrusted*n. This matches the glibc behaviour that portable code relies on (for example toyboxgrep, which callsgetdelim()with an uninitialized size variable).Impact
libs/libc/stdio/lib_getdelim.c;getline()shares the same implementation and benefits from the same fix.Testing
Built and booted
sim:nshon Linux; verifiedgetline()with*lineptr == NULLand an uninitialized*nnow succeeds instead of returningENOMEM.