]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libuuid: (test_uuid) make reading UUIDs from file more robust
authorThomas Weißschuh <thomas@t-8ch.de>
Sun, 24 Dec 2023 11:35:25 +0000 (12:35 +0100)
committerThomas Weißschuh <thomas@t-8ch.de>
Sun, 31 Dec 2023 12:06:02 +0000 (13:06 +0100)
Instead of relying on fixed width entries, read line-by-line.

Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
libuuid/src/test_uuid.c

index 990791194a065a7c17bbf549deb1871628b2de2f..06d1589364e325a2f27c4b6952267b009b762893 100644 (file)
@@ -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;
 }