From: Thomas Weißschuh Date: Sun, 24 Dec 2023 11:35:25 +0000 (+0100) Subject: libuuid: (test_uuid) make reading UUIDs from file more robust X-Git-Tag: v2.40-rc1~84^2~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7a2ad9a59ece4332cd34fdc6cc6826c2fba3c887;p=thirdparty%2Futil-linux.git libuuid: (test_uuid) make reading UUIDs from file more robust Instead of relying on fixed width entries, read line-by-line. Signed-off-by: Thomas Weißschuh --- diff --git a/libuuid/src/test_uuid.c b/libuuid/src/test_uuid.c index 990791194a..06d1589364 100644 --- a/libuuid/src/test_uuid.c +++ b/libuuid/src/test_uuid.c @@ -66,24 +66,30 @@ static int test_uuid(const char * uuid, int isValid) static int check_uuids_in_file(const char *file) { - int fd, ret = 0; - size_t sz; - char str[UUID_STR_LEN]; + int ret = 0; + size_t alloc = 0; + ssize_t sz; + char *str = NULL; + FILE *f; uuid_t uuidBits; - if ((fd = open(file, O_RDONLY)) < 0) { + if ((f = fopen(file, "r")) == NULL) { warn("%s", file); return 1; } - while ((sz = read(fd, str, sizeof(str))) != 0) { - str[sizeof(str) - 1] = '\0'; + while ((sz = getline(&str, &alloc, f)) != -1) { + if (sz == 0 || str[0] == '\n') + continue; + if (str[sz - 1] == '\n') + str[sz - 1] = '\0'; if (uuid_parse(str, uuidBits)) { warnx("%s: %s", file, str); ret++; } } - close(fd); + fclose(f); + free(str); return ret; }