From: Lennart Poettering Date: Thu, 18 Oct 2018 11:30:20 +0000 (+0200) Subject: clock-util: excorcise fgets() X-Git-Tag: v240~514^2~13 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6d3db278fe8465d8bef4e215e2b0d20b12f8602b;p=thirdparty%2Fsystemd.git clock-util: excorcise fgets() --- diff --git a/src/basic/clock-util.c b/src/basic/clock-util.c index 8555cc79525..b77e780bed8 100644 --- a/src/basic/clock-util.c +++ b/src/basic/clock-util.c @@ -10,8 +10,11 @@ #include #include +#include "alloc-util.h" #include "clock-util.h" +#include "def.h" #include "fd-util.h" +#include "fileio.h" #include "macro.h" #include "string-util.h" #include "util.h" @@ -54,6 +57,7 @@ int clock_set_hwclock(const struct tm *tm) { int clock_is_localtime(const char* adjtime_path) { _cleanup_fclose_ FILE *f; + int r; if (!adjtime_path) adjtime_path = "/etc/adjtime"; @@ -67,24 +71,30 @@ int clock_is_localtime(const char* adjtime_path) { */ f = fopen(adjtime_path, "re"); if (f) { - char line[LINE_MAX]; - bool b; + _cleanup_free_ char *line = NULL; + unsigned i; + + for (i = 0; i < 2; i++) { /* skip the first two lines */ + r = read_line(f, LONG_LINE_MAX, NULL); + if (r < 0) + return r; + if (r == 0) + return false; /* less than three lines → default to UTC */ + } + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + return false; /* less than three lines → default to UTC */ - b = fgets(line, sizeof(line), f) && - fgets(line, sizeof(line), f) && - fgets(line, sizeof(line), f); - if (!b) - /* less than three lines -> default to UTC */ - return 0; - - truncate_nl(line); return streq(line, "LOCAL"); } else if (errno != ENOENT) return -errno; - /* adjtime not present -> default to UTC */ - return 0; + /* adjtime not present → default to UTC */ + return false; } int clock_set_timezone(int *min) {