On Tue, Nov 17, 2026 at 10:33:04AM +0900, Dongwon Jang wrote:
> read_line_to_trash() reads the first line of a file into the trash, so
> longer lines are truncated to the trash size. Some callers need to read
> lines of any length from an already opened file instead, such as the
> pattern file readers, which run before the trash is resized to
> tune.bufsize.
>
> read_line_to_mem() reads the next line of a file into a caller-provided
> heap buffer that grows as needed, as parse_cfg() does for configuration
> lines. Unlike read_line_to_trash(), the trailing LF is kept. A full buffer
> is detected by checking whether fgets() overwrote its last byte rather
> than from the string length, so that lines containing NUL bytes are read
> entirely as well. getline() is not used since it was removed for
> portability reasons by commit fbc3d46b9 ("BUILD: stats: remove non
> portable getline() usage").
>
> It will be used by the next commit to fix the pattern file readers, and
> will have to be backported along with it.
> ---
> include/haproxy/tools.h | 1 +
> src/tools.c | 43 +++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 44 insertions(+)
>
> diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h
> index be8f5e020..19c1b674a 100644
> --- a/include/haproxy/tools.h
> +++ b/include/haproxy/tools.h
> @@ -1019,6 +1019,7 @@ int is_file_present(const char *path_fmt, ...);
> int is_dir_present(const char *path_fmt, ...);
> uint32_t parse_line(char *in, char *out, size_t *outlen, char **args, int
> *nbargs, uint32_t opts, const char **errptr);
> ssize_t read_line_to_trash(const char *path_fmt, ...);
> +int read_line_to_mem(FILE *file, char **buf, size_t *size);
> size_t sanitize_for_printing(char *line, size_t pos, size_t width);
> void update_word_fingerprint_with_len(uint8_t *fp, struct ist word);
> void update_word_fingerprint(uint8_t *fp, const char *word);
> diff --git a/src/tools.c b/src/tools.c
> index 668fcfc00..1c5ada753 100644
> --- a/src/tools.c
> +++ b/src/tools.c
> @@ -6975,6 +6975,49 @@ ssize_t read_line_to_trash(const char *path_fmt, ...)
> return ret;
> }
>
> +/* Reads the next line of <file> into <*buf>, whose allocated size is
> <*size>,
> + * growing the buffer as needed so that the whole line is read whatever its
> + * length. Unlike read_line_to_trash(), the trailing LF if any is kept.
> <*buf>
> + * must either be NULL, in which case it is allocated, or point to the area
> of
> + * <*size> bytes it was given by a previous call, and it must be released by
> the
> + * caller with free(), including after a failure. Returns 1 if a line was
> read,
> + * 0 at the end of the file or on read error, and -1 on memory allocation
> + * failure.
-1 is on allocation *or* reallocation failure. The difference is subtle,
but means that the caller must release <buf> it even if -1 is returned.
I might adjust the comment when committing. Also to mention that <buf>
if not null, must be compatible with realloc().
But OK for the rest of the function!
Willy