]> sourceware.org Git - lvm2.git/commitdiff
hints: allocate hint only when needed
authorZdenek Kabelac <zkabelac@redhat.com>
Thu, 14 Nov 2019 16:57:43 +0000 (17:57 +0100)
committerZdenek Kabelac <zkabelac@redhat.com>
Thu, 14 Nov 2019 17:06:42 +0000 (18:06 +0100)
Avoid mem leaking hint on every loop continue and
allocate hint only when it's going to be added into list.

Switch to use 'dm_strncpy()' and validate sizes.

lib/label/hints.c

index 79648b90969823afc1ce4761f94233da5a4a5880..ec43dd35c70d651b91b575efd132071a39fdc848 100644 (file)
@@ -625,7 +625,8 @@ static int _read_hint_file(struct cmd_context *cmd, struct dm_list *hints, int *
        char devpath[PATH_MAX];
        FILE *fp;
        struct dev_iter *iter;
-       struct hint *hint;
+       struct hint hint;
+       struct hint *alloc_hint;
        struct device *dev;
        char *split[HINT_LINE_WORDS];
        char *name, *pvid, *devn, *vgname, *p, *filter_str = NULL;
@@ -649,11 +650,7 @@ static int _read_hint_file(struct cmd_context *cmd, struct dm_list *hints, int *
                split[i] = NULL;
 
        while (fgets(_hint_line, sizeof(_hint_line), fp)) {
-               if (!(hint = zalloc(sizeof(struct hint)))) {
-                       ret = 0;
-                       break;
-               }
-
+               memset(&hint, 0, sizeof(hint));
                if (_hint_line[0] == '#')
                        continue;
 
@@ -751,19 +748,28 @@ static int _read_hint_file(struct cmd_context *cmd, struct dm_list *hints, int *
                vgname = split[3];
 
                if (name && !strncmp(name, "scan:", 5))
-                       strncpy(hint->name, name+5, PATH_MAX);
+                       if (!dm_strncpy(hint.name, name + 5, sizeof(hint.name)))
+                               continue;
 
                if (pvid && !strncmp(pvid, "pvid:", 5))
-                       strncpy(hint->pvid, pvid+5, ID_LEN);
+                       if (!dm_strncpy(hint.pvid, pvid + 5, sizeof(hint.pvid)))
+                               continue;
 
                if (devn && sscanf(devn, "devn:%d:%d", &major, &minor) == 2)
-                       hint->devt = makedev(major, minor);
+                       hint.devt = makedev(major, minor);
 
                if (vgname && (strlen(vgname) > 3) && (vgname[4] != '-'))
-                       strncpy(hint->vgname, vgname+3, NAME_LEN);
+                       if (!dm_strncpy(hint.vgname, vgname + 3, sizeof(hint.vgname)))
+                               continue;
+
+               if (!(alloc_hint = malloc(sizeof(struct hint)))) {
+                       ret = 0;
+                       break;
+               }
+               memcpy(alloc_hint, &hint, sizeof(hint));
 
-               log_debug("add hint %s %s %d:%d %s", hint->name, hint->pvid, major, minor, vgname);
-               dm_list_add(hints, &hint->list);
+               log_debug("add hint %s %s %d:%d %s", hint.name, hint.pvid, major, minor, vgname);
+               dm_list_add(hints, &alloc_hint->list);
                found++;
        }
 
This page took 0.039389 seconds and 5 git commands to generate.