From dbf266b9d42e23a863ff44d86978cf257ab3ce7c Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 13 Nov 2019 11:45:26 -0600 Subject: [PATCH] libs/libc/stdio/lib_getdelim.c: Initial allocation should be larger; incremental reallocs should be smaller. Update some comments. --- libs/libc/stdio/lib_getdelim.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/libs/libc/stdio/lib_getdelim.c b/libs/libc/stdio/lib_getdelim.c index b010c60d46..14168a24f5 100644 --- a/libs/libc/stdio/lib_getdelim.c +++ b/libs/libc/stdio/lib_getdelim.c @@ -61,6 +61,7 @@ # define EOLCH '/n' #endif +#define BUFSIZE_INIT 64 #define BUFSIZE_INCR 32 /**************************************************************************** @@ -94,6 +95,17 @@ * read, including any delimiter, will be stored in the object, and a * terminating NUL added when the delimiter or end-of-file is encountered. * + * Returned Value: + * Upon successful completion, the getline() and getdelim() functions will + * return the number of bytes written into the buffer, including the + * delimiter character if one was encountered before EOF, but excluding + * the terminating NUL character. If the end-of-file indicator for the + * stream is set, or if no characters were read and the stream is at + * end-of-file, the end-of-file indicator for the stream will be set and + * the function will return -1. If an error occurs, the error indicator for + * the stream will be set, and the function will return -1 and set errno to + * indicate the error. + * ****************************************************************************/ ssize_t getdelim(FAR char **lineptr, size_t *n, int delimiter, @@ -121,8 +133,8 @@ ssize_t getdelim(FAR char **lineptr, size_t *n, int delimiter, { /* Pick an initial buffer size */ - bufsize = BUFSIZE_INCR; - *n = BUFSIZE_INCR; + bufsize = BUFSIZE_INIT; + *n = BUFSIZE_INIT; /* Free any mystery buffer. It will be reallocated below. */ @@ -167,6 +179,11 @@ ssize_t getdelim(FAR char **lineptr, size_t *n, int delimiter, { FAR char *newbuffer; + /* This function should fail with EOVERFLOW if bufize exeeds + * SSIZE_MAX. However, I think we will have crashed long before + * that occurs. + */ + bufsize += BUFSIZE_INCR; newbuffer = (FAR char *)lib_realloc(*lineptr, bufsize); if (newbuffer == NULL)