]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libfdisk: consolidate strdup() use
authorKarel Zak <kzak@redhat.com>
Wed, 23 Oct 2019 11:00:40 +0000 (13:00 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 31 Oct 2019 11:25:08 +0000 (12:25 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libfdisk/src/context.c
libfdisk/src/partition.c
libfdisk/src/parttype.c
libfdisk/src/script.c

index cb2164c2e6fd72b5fc33a0d33d02de4ced110777..743a6bc1d2328cca9f59ba6700c79ed368f239eb 100644 (file)
@@ -9,6 +9,7 @@
 #include "loopdev.h"
 #include "fdiskP.h"
 
+#include "strutils.h"
 
 /**
  * SECTION: context
@@ -84,6 +85,8 @@ static int init_nested_from_parent(struct fdisk_context *cxt, int isnew)
 
        parent = cxt->parent;
 
+       INIT_LIST_HEAD(&cxt->wipes);
+
        cxt->alignment_offset = parent->alignment_offset;
        cxt->ask_cb =           parent->ask_cb;
        cxt->ask_data =         parent->ask_data;
@@ -120,18 +123,7 @@ static int init_nested_from_parent(struct fdisk_context *cxt, int isnew)
        cxt->dev_model = NULL;
        cxt->dev_model_probed = 0;
 
-       free(cxt->dev_path);
-       cxt->dev_path = NULL;
-
-       if (parent->dev_path) {
-               cxt->dev_path = strdup(parent->dev_path);
-               if (!cxt->dev_path)
-                       return -ENOMEM;
-       }
-
-       INIT_LIST_HEAD(&cxt->wipes);
-
-       return 0;
+       return strdup_between_structs(cxt, parent, dev_path);
 }
 
 /**
index 7cfca3fa40dbadc2678a2322abfb38d9fa1fc44f..60feb74a621323d129c410c14bdaf1b1e13ba398 100644 (file)
@@ -7,6 +7,7 @@
 #endif
 
 #include "fdiskP.h"
+#include "strutils.h"
 
 /**
  * SECTION: partition
@@ -82,6 +83,7 @@ void fdisk_reset_partition(struct fdisk_partition *pa)
 static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
 {
        struct fdisk_partition *n = fdisk_new_partition();
+       int rc;
 
        if (!n)
                return NULL;
@@ -94,23 +96,27 @@ static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
 
        if (n->type)
                fdisk_ref_parttype(n->type);
-       if (o->name)
-               n->name = strdup(o->name);
-       if (o->uuid)
-               n->uuid = strdup(o->uuid);
-       if (o->attrs)
-               n->attrs = strdup(o->attrs);
-       if (o->fstype)
-               n->fstype = strdup(o->fstype);
-       if (o->fsuuid)
-               n->fsuuid = strdup(o->fsuuid);
-       if (o->fslabel)
-               n->fslabel = strdup(o->fslabel);
-       if (o->start_chs)
-               n->start_chs = strdup(o->start_chs);
-       if (o->end_chs)
-               n->end_chs = strdup(o->end_chs);
 
+       rc = strdup_between_structs(n, o, name);
+       if (!rc)
+               rc = strdup_between_structs(n, o, uuid);
+       if (!rc)
+               rc = strdup_between_structs(n, o, attrs);
+       if (!rc)
+               rc = strdup_between_structs(n, o, fstype);
+       if (!rc)
+               rc = strdup_between_structs(n, o, fsuuid);
+       if (!rc)
+               rc = strdup_between_structs(n, o, fslabel);
+       if (!rc)
+               rc = strdup_between_structs(n, o, start_chs);
+       if (!rc)
+               rc = strdup_between_structs(n, o, end_chs);
+
+       if (rc) {
+               fdisk_unref_partition(n);
+               n = NULL;
+       }
        return n;
 }
 
@@ -486,18 +492,9 @@ struct fdisk_parttype *fdisk_partition_get_type(struct fdisk_partition *pa)
 
 int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
 {
-       char *p = NULL;
-
        if (!pa)
                return -EINVAL;
-       if (name) {
-              p = strdup(name);
-              if (!p)
-                      return -ENOMEM;
-       }
-       free(pa->name);
-       pa->name = p;
-       return 0;
+       return strdup_to_struct_member(pa, name, name);
 }
 
 const char *fdisk_partition_get_name(struct fdisk_partition *pa)
@@ -507,18 +504,9 @@ const char *fdisk_partition_get_name(struct fdisk_partition *pa)
 
 int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
 {
-       char *p = NULL;
-
        if (!pa)
                return -EINVAL;
-       if (uuid) {
-              p = strdup(uuid);
-              if (!p)
-                      return -ENOMEM;
-       }
-       free(pa->uuid);
-       pa->uuid = p;
-       return 0;
+       return strdup_to_struct_member(pa, uuid, uuid);
 }
 
 /**
@@ -612,18 +600,9 @@ const char *fdisk_partition_get_attrs(struct fdisk_partition *pa)
  */
 int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs)
 {
-       char *p = NULL;
-
        if (!pa)
                return -EINVAL;
-       if (attrs) {
-              p = strdup(attrs);
-              if (!p)
-                      return -ENOMEM;
-       }
-       free(pa->attrs);
-       pa->attrs = p;
-       return 0;
+       return strdup_to_struct_member(pa, attrs, attrs);
 }
 
 /**
index 110ef3ba76ece60055b867124ce1732546542470..d5ad434f0bda8cf23b23118ef04c92b475ec8875 100644 (file)
@@ -2,6 +2,7 @@
 #include <ctype.h>
 
 #include "fdiskP.h"
+#include "strutils.h"
 
 /**
  * SECTION: parttype
@@ -76,19 +77,9 @@ void fdisk_unref_parttype(struct fdisk_parttype *t)
  */
 int fdisk_parttype_set_name(struct fdisk_parttype *t, const char *str)
 {
-       char *p = NULL;
-
        if (!t || !fdisk_parttype_is_allocated(t))
                return -EINVAL;
-       if (str) {
-               p = strdup(str);
-               if (!p)
-                       return -ENOMEM;
-       }
-
-       free(t->name);
-       t->name = p;
-       return 0;
+       return strdup_to_struct_member(t, name, str);
 }
 
 /**
@@ -104,19 +95,9 @@ int fdisk_parttype_set_name(struct fdisk_parttype *t, const char *str)
  */
 int fdisk_parttype_set_typestr(struct fdisk_parttype *t, const char *str)
 {
-       char *p = NULL;
-
        if (!t || !fdisk_parttype_is_allocated(t))
                return -EINVAL;
-       if (str) {
-               p = strdup(str);
-               if (!p)
-                       return -ENOMEM;
-       }
-
-       free(t->typestr);
-       t->typestr = p;
-       return 0;
+       return strdup_to_struct_member(t, typestr, str);
 }
 
 /**
index 50899664fe4f5c075020db8401e8263923072c41..a21771b6a857d21a83da29b26b67aa06780f7b2c 100644 (file)
@@ -298,6 +298,8 @@ int fdisk_script_set_header(struct fdisk_script *dp,
        }
 
        if (!fi) {
+               int rc;
+
                DBG(SCRIPT, ul_debugobj(dp, "setting new header %s='%s'", name, data));
 
                /* new header */
@@ -305,11 +307,13 @@ int fdisk_script_set_header(struct fdisk_script *dp,
                if (!fi)
                        return -ENOMEM;
                INIT_LIST_HEAD(&fi->headers);
-               fi->name = strdup(name);
-               fi->data = strdup(data);
-               if (!fi->data || !fi->name) {
+
+               rc = strdup_to_struct_member(fi, name, name);
+               if (!rc)
+                       rc = strdup_to_struct_member(fi, data, data);
+               if (rc) {
                        fdisk_script_free_header(fi);
-                       return -ENOMEM;
+                       return rc;
                }
                list_add_tail(&fi->headers, &dp->headers);
        } else {