Skip to content

libc/stdio: allocate a buffer in getdelim() when *lineptr is NULL - #20034

Open
xiaoxiang781216 wants to merge 1 commit into
apache:masterfrom
xiaoxiang781216:upstream-getdelim
Open

libc/stdio: allocate a buffer in getdelim() when *lineptr is NULL#20034
xiaoxiang781216 wants to merge 1 commit into
apache:masterfrom
xiaoxiang781216:upstream-getdelim

Conversation

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

Summary

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).

Impact

  • Only modifies libs/libc/stdio/lib_getdelim.c; getline() shares the same implementation and benefits from the same fix.

Testing

Built and booted sim:nsh on Linux; verified getline() with *lineptr == NULL and an uninitialized *n now succeeds instead of returning ENOMEM.

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>
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small labels Sep 1, 2026
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

@acassis acassis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@xiaoxiang781216 please include a getdelim() test to apps, git grep getdelim at nuttx-apps returns nothing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants