]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: tools: add read_line_to_mem() to read a line into a growing buffer
authorDongwon Jang <contact@dongwonjang.com>
Tue, 17 Nov 2026 01:33:04 +0000 (10:33 +0900)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 10:33:05 +0000 (11:33 +0100)
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
src/tools.c

index bea024e50eff8e5193ff4b330a66faf53ab53ded..118f8539a48eb969c50d55b61a0b9d69e676f25e 100644 (file)
@@ -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);
index 2747c4eea998d47de180670bc99a1984afbcdb69..188f614bab1fb1d4c8dd90b1db057b0922501858 100644 (file)
@@ -6288,6 +6288,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 or
+ * realloction failure (the caller must free <buf> 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 <line> to print approximately <width> chars around <pos>, trying to
  * preserve the beginning, with leading or trailing "..." when the line is truncated.