From 9c6f9786c5f2acfc1cedebe82976ab78ee7fd6fc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 18 Oct 2018 13:40:55 +0200 Subject: [PATCH] condition: fgets() excorcism --- src/shared/condition.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/shared/condition.c b/src/shared/condition.c index d0b17fe7ff7..2acf36d468e 100644 --- a/src/shared/condition.c +++ b/src/shared/condition.c @@ -384,10 +384,9 @@ static int condition_test_security(Condition *c) { } static int condition_test_capability(Condition *c) { + unsigned long long capabilities = (unsigned long long) -1; _cleanup_fclose_ FILE *f = NULL; - int value; - char line[LINE_MAX]; - unsigned long long capabilities = -1; + int value, r; assert(c); assert(c->parameter); @@ -405,11 +404,21 @@ static int condition_test_capability(Condition *c) { if (!f) return -errno; - while (fgets(line, sizeof(line), f)) { - truncate_nl(line); + for (;;) { + _cleanup_free_ char *line = NULL; + const char *p; + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; + + p = startswith(line, "CapBnd:"); + if (p) { + if (sscanf(line+7, "%llx", &capabilities) != 1) + return -EIO; - if (startswith(line, "CapBnd:")) { - (void) sscanf(line+7, "%llx", &capabilities); break; } }