ryantimwilson / rpms / systemd

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