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.
+ */
+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.
-- 
2.50.1 (Apple Git-155)



Reply via email to