From 7cbde881854367173d8c493b1a4ac9c2d5cdb632 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 23 Oct 2019 13:00:40 +0200 Subject: [PATCH] libfdisk: consolidate strdup() use Signed-off-by: Karel Zak --- libfdisk/src/context.c | 16 +++------ libfdisk/src/partition.c | 71 ++++++++++++++-------------------------- libfdisk/src/parttype.c | 25 ++------------ libfdisk/src/script.c | 12 ++++--- 4 files changed, 40 insertions(+), 84 deletions(-) diff --git a/libfdisk/src/context.c b/libfdisk/src/context.c index cb2164c2e..743a6bc1d 100644 --- a/libfdisk/src/context.c +++ b/libfdisk/src/context.c @@ -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); } /** diff --git a/libfdisk/src/partition.c b/libfdisk/src/partition.c index 7cfca3fa4..60feb74a6 100644 --- a/libfdisk/src/partition.c +++ b/libfdisk/src/partition.c @@ -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); } /** diff --git a/libfdisk/src/parttype.c b/libfdisk/src/parttype.c index 110ef3ba7..d5ad434f0 100644 --- a/libfdisk/src/parttype.c +++ b/libfdisk/src/parttype.c @@ -2,6 +2,7 @@ #include #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); } /** diff --git a/libfdisk/src/script.c b/libfdisk/src/script.c index 50899664f..a21771b6a 100644 --- a/libfdisk/src/script.c +++ b/libfdisk/src/script.c @@ -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 {