dd65c9
From d7b56e186521ce2e48e27edda121d780a3d62d27 Mon Sep 17 00:00:00 2001
dd65c9
From: Jan Synacek <jsynacek@redhat.com>
dd65c9
Date: Thu, 23 Nov 2017 08:53:50 +0100
dd65c9
Subject: [PATCH] fileio: add new helper call read_line() as bounded getline()
dd65c9
 replacement
dd65c9
dd65c9
read_line() is much like getline(), and returns a line read from a
dd65c9
FILE*, of arbitrary sizes. In contrast to gets() it will grow the buffer
dd65c9
dynamically, and in contrast to getline() it will place a user-specified
dd65c9
boundary on the line.
dd65c9
dd65c9
(cherry-picked from commit 4f9a66a32dda1d9a28f9bb3fa31c2148524bc46a)
dd65c9
dd65c9
Resolves: #1503106
dd65c9
---
23b3cf
 src/shared/fileio.c    | 77 ++++++++++++++++++++++++++++++++++++++++++
dd65c9
 src/shared/fileio.h    |  2 ++
23b3cf
 src/test/test-fileio.c | 44 ++++++++++++++++++++++++
dd65c9
 3 files changed, 123 insertions(+)
dd65c9
dd65c9
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
c62b8e
index ff6b1a7ed7..1077375735 100644
dd65c9
--- a/src/shared/fileio.c
dd65c9
+++ b/src/shared/fileio.c
dd65c9
@@ -815,3 +815,80 @@ int get_status_field(const char *filename, const char *pattern, char **field) {
dd65c9
 
dd65c9
         return 0;
dd65c9
 }
dd65c9
+
dd65c9
+int read_line(FILE *f, size_t limit, char **ret) {
dd65c9
+        _cleanup_free_ char *buffer = NULL;
dd65c9
+        size_t n = 0, allocated = 0, count = 0;
dd65c9
+        int r;
dd65c9
+
dd65c9
+        assert(f);
dd65c9
+
dd65c9
+        /* Something like a bounded version of getline().
dd65c9
+         *
dd65c9
+         * Considers EOF, \n and \0 end of line delimiters, and does not include these delimiters in the string
dd65c9
+         * returned.
dd65c9
+         *
dd65c9
+         * Returns the number of bytes read from the files (i.e. including delimiters — this hence usually differs from
dd65c9
+         * the number of characters in the returned string). When EOF is hit, 0 is returned.
dd65c9
+         *
dd65c9
+         * The input parameter limit is the maximum numbers of characters in the returned string, i.e. excluding
dd65c9
+         * delimiters. If the limit is hit we fail and return -ENOBUFS.
dd65c9
+         *
dd65c9
+         * If a line shall be skipped ret may be initialized as NULL. */
dd65c9
+
dd65c9
+        if (ret) {
dd65c9
+                if (!GREEDY_REALLOC(buffer, allocated, 1))
dd65c9
+                        return -ENOMEM;
dd65c9
+        }
dd65c9
+
dd65c9
+        flockfile(f);
dd65c9
+
dd65c9
+        for (;;) {
dd65c9
+                int c;
dd65c9
+
dd65c9
+                if (n >= limit) {
dd65c9
+                        funlockfile(f);
dd65c9
+                        return -ENOBUFS;
dd65c9
+                }
dd65c9
+
dd65c9
+                errno = 0;
dd65c9
+                c = fgetc_unlocked(f);
dd65c9
+                if (c == EOF) {
dd65c9
+                        /* if we read an error, and have no data to return, then propagate the error */
dd65c9
+                        if (ferror_unlocked(f) && n == 0) {
dd65c9
+                                r = errno > 0 ? -errno : -EIO;
dd65c9
+                                funlockfile(f);
dd65c9
+                                return r;
dd65c9
+                        }
dd65c9
+
dd65c9
+                        break;
dd65c9
+                }
dd65c9
+
dd65c9
+                count++;
dd65c9
+
dd65c9
+                if (IN_SET(c, '\n', 0)) /* Reached a delimiter */
dd65c9
+                        break;
dd65c9
+
dd65c9
+                if (ret) {
dd65c9
+                        if (!GREEDY_REALLOC(buffer, allocated, n + 2)) {
dd65c9
+                                funlockfile(f);
dd65c9
+                                return -ENOMEM;
dd65c9
+                        }
dd65c9
+
dd65c9
+                        buffer[n] = (char) c;
dd65c9
+                }
dd65c9
+
dd65c9
+                n++;
dd65c9
+        }
dd65c9
+
dd65c9
+        funlockfile(f);
dd65c9
+
dd65c9
+        if (ret) {
dd65c9
+                buffer[n] = 0;
dd65c9
+
dd65c9
+                *ret = buffer;
dd65c9
+                buffer = NULL;
dd65c9
+        }
dd65c9
+
dd65c9
+        return (int) count;
dd65c9
+}
dd65c9
diff --git a/src/shared/fileio.h b/src/shared/fileio.h
c62b8e
index 5ae51c1e28..f33464dce7 100644
dd65c9
--- a/src/shared/fileio.h
dd65c9
+++ b/src/shared/fileio.h
dd65c9
@@ -43,3 +43,5 @@ int write_env_file(const char *fname, char **l);
dd65c9
 int executable_is_script(const char *path, char **interpreter);
dd65c9
 
dd65c9
 int get_status_field(const char *filename, const char *pattern, char **field);
dd65c9
+
dd65c9
+int read_line(FILE *f, size_t limit, char **ret);
dd65c9
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
c62b8e
index 63e4a19b76..fc59693228 100644
dd65c9
--- a/src/test/test-fileio.c
dd65c9
+++ b/src/test/test-fileio.c
dd65c9
@@ -392,6 +392,49 @@ static void test_load_env_file_pairs(void) {
dd65c9
         unlink(fn);
dd65c9
 }
dd65c9
 
dd65c9
+static void test_read_line(void) {
dd65c9
+        _cleanup_fclose_ FILE *f = NULL;
dd65c9
+        _cleanup_free_ char *line = NULL;
dd65c9
+
dd65c9
+        char buffer[] =
dd65c9
+                "Some test data\n"
dd65c9
+                "With newlines, and a NUL byte\0"
dd65c9
+                "\n"
dd65c9
+                "an empty line\n"
dd65c9
+                "an ignored line\n"
dd65c9
+                "and a very long line that is supposed to be truncated, because it is so long\n";
dd65c9
+
dd65c9
+        f = fmemopen(buffer, sizeof(buffer), "re");
dd65c9
+        assert_se(f);
dd65c9
+
dd65c9
+        assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        assert_se(read_line(f, 1024, &line) == 14 && streq(line, "an empty line"));
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        assert_se(read_line(f, (size_t) -1, NULL) == 16);
dd65c9
+
dd65c9
+        assert_se(read_line(f, 16, &line) == -ENOBUFS);
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        /* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
dd65c9
+         * character after the previous limit. Let's make use of tha to continue our test. */
dd65c9
+        assert_se(read_line(f, 1024, &line) == 61 && streq(line, "line that is supposed to be truncated, because it is so long"));
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
dd65c9
+        line = mfree(line);
dd65c9
+
dd65c9
+        assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
dd65c9
+}
dd65c9
+
dd65c9
 int main(int argc, char *argv[]) {
dd65c9
         log_parse_environment();
dd65c9
         log_open();
dd65c9
@@ -405,6 +448,7 @@ int main(int argc, char *argv[]) {
dd65c9
         test_write_string_file();
dd65c9
         test_write_string_file_no_create();
dd65c9
         test_load_env_file_pairs();
dd65c9
+        test_read_line();
dd65c9
 
dd65c9
         return 0;
dd65c9
 }