From: Dongwon Jang Date: Tue, 17 Nov 2026 01:33:04 +0000 (+0900) Subject: MINOR: tools: add read_line_to_mem() to read a line into a growing buffer X-Git-Tag: v3.5-dev14~29 X-Git-Url: https://git.kaiwu.me/http/%22../static/gitweb.js?a=commitdiff_plain;h=44556c0c0aabb9190f4cbf46863148a3d98f77eb;p=haproxy.git MINOR: tools: add read_line_to_mem() to read a line into a growing buffer 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. --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index bea024e50..118f8539a 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -949,6 +949,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 2747c4eea..188f614ba 100644 --- a/src/tools.c +++ b/src/tools.c @@ -6288,6 +6288,49 @@ ssize_t read_line_to_trash(const char *path_fmt, ...) return ret; } +/* Reads the next line of 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 or + * realloction failure (the caller must free even in this case). + */ +int read_line_to_mem(FILE *file, char **buf, size_t *size) +{ + size_t len = 0; + char *new_buf; + + if (!*buf) { + *buf = malloc(LINESIZE); + if (!*buf) + return -1; + *size = LINESIZE; + } + + while (1) { + /* fgets() only overwrites the last byte when it fills the buffer. + * Checking it instead of the string length also works when the + * line contains NUL bytes. + */ + (*buf)[*size - 1] = 1; + if (fgets(*buf + len, *size - len, file) == NULL) + return (len && !ferror(file)) ? 1 : 0; + + if ((*buf)[*size - 1] || (*buf)[*size - 2] == '\n') + return 1; + + /* the buffer is full and the line is not complete yet */ + len = *size - 1; + new_buf = realloc(*buf, *size * 2); + if (!new_buf) + return -1; + *buf = new_buf; + *size *= 2; + } +} + /* This is used to sanitize an input line that's about to be used for error reporting. * It will adjust to print approximately chars around , trying to * preserve the beginning, with leading or trailing "..." when the line is truncated.