libfdisk: consolidate strdup() use

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2019-10-23 13:00:40 +02:00
parent eee7ea558c
commit 7cbde88185
4 changed files with 40 additions and 84 deletions

View File

@ -9,6 +9,7 @@
#include "loopdev.h" #include "loopdev.h"
#include "fdiskP.h" #include "fdiskP.h"
#include "strutils.h"
/** /**
* SECTION: context * SECTION: context
@ -84,6 +85,8 @@ static int init_nested_from_parent(struct fdisk_context *cxt, int isnew)
parent = cxt->parent; parent = cxt->parent;
INIT_LIST_HEAD(&cxt->wipes);
cxt->alignment_offset = parent->alignment_offset; cxt->alignment_offset = parent->alignment_offset;
cxt->ask_cb = parent->ask_cb; cxt->ask_cb = parent->ask_cb;
cxt->ask_data = parent->ask_data; 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 = NULL;
cxt->dev_model_probed = 0; cxt->dev_model_probed = 0;
free(cxt->dev_path); return strdup_between_structs(cxt, parent, 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;
} }
/** /**

View File

@ -7,6 +7,7 @@
#endif #endif
#include "fdiskP.h" #include "fdiskP.h"
#include "strutils.h"
/** /**
* SECTION: partition * SECTION: partition
@ -82,6 +83,7 @@ void fdisk_reset_partition(struct fdisk_partition *pa)
static struct fdisk_partition *__copy_partition(struct fdisk_partition *o) static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
{ {
struct fdisk_partition *n = fdisk_new_partition(); struct fdisk_partition *n = fdisk_new_partition();
int rc;
if (!n) if (!n)
return NULL; return NULL;
@ -94,23 +96,27 @@ static struct fdisk_partition *__copy_partition(struct fdisk_partition *o)
if (n->type) if (n->type)
fdisk_ref_parttype(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; 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) int fdisk_partition_set_name(struct fdisk_partition *pa, const char *name)
{ {
char *p = NULL;
if (!pa) if (!pa)
return -EINVAL; return -EINVAL;
if (name) { return strdup_to_struct_member(pa, name, name);
p = strdup(name);
if (!p)
return -ENOMEM;
}
free(pa->name);
pa->name = p;
return 0;
} }
const char *fdisk_partition_get_name(struct fdisk_partition *pa) 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) int fdisk_partition_set_uuid(struct fdisk_partition *pa, const char *uuid)
{ {
char *p = NULL;
if (!pa) if (!pa)
return -EINVAL; return -EINVAL;
if (uuid) { return strdup_to_struct_member(pa, uuid, uuid);
p = strdup(uuid);
if (!p)
return -ENOMEM;
}
free(pa->uuid);
pa->uuid = p;
return 0;
} }
/** /**
@ -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) int fdisk_partition_set_attrs(struct fdisk_partition *pa, const char *attrs)
{ {
char *p = NULL;
if (!pa) if (!pa)
return -EINVAL; return -EINVAL;
if (attrs) { return strdup_to_struct_member(pa, attrs, attrs);
p = strdup(attrs);
if (!p)
return -ENOMEM;
}
free(pa->attrs);
pa->attrs = p;
return 0;
} }
/** /**

View File

@ -2,6 +2,7 @@
#include <ctype.h> #include <ctype.h>
#include "fdiskP.h" #include "fdiskP.h"
#include "strutils.h"
/** /**
* SECTION: parttype * 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) int fdisk_parttype_set_name(struct fdisk_parttype *t, const char *str)
{ {
char *p = NULL;
if (!t || !fdisk_parttype_is_allocated(t)) if (!t || !fdisk_parttype_is_allocated(t))
return -EINVAL; return -EINVAL;
if (str) { return strdup_to_struct_member(t, name, str);
p = strdup(str);
if (!p)
return -ENOMEM;
}
free(t->name);
t->name = p;
return 0;
} }
/** /**
@ -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) int fdisk_parttype_set_typestr(struct fdisk_parttype *t, const char *str)
{ {
char *p = NULL;
if (!t || !fdisk_parttype_is_allocated(t)) if (!t || !fdisk_parttype_is_allocated(t))
return -EINVAL; return -EINVAL;
if (str) { return strdup_to_struct_member(t, typestr, str);
p = strdup(str);
if (!p)
return -ENOMEM;
}
free(t->typestr);
t->typestr = p;
return 0;
} }
/** /**

View File

@ -298,6 +298,8 @@ int fdisk_script_set_header(struct fdisk_script *dp,
} }
if (!fi) { if (!fi) {
int rc;
DBG(SCRIPT, ul_debugobj(dp, "setting new header %s='%s'", name, data)); DBG(SCRIPT, ul_debugobj(dp, "setting new header %s='%s'", name, data));
/* new header */ /* new header */
@ -305,11 +307,13 @@ int fdisk_script_set_header(struct fdisk_script *dp,
if (!fi) if (!fi)
return -ENOMEM; return -ENOMEM;
INIT_LIST_HEAD(&fi->headers); INIT_LIST_HEAD(&fi->headers);
fi->name = strdup(name);
fi->data = strdup(data); rc = strdup_to_struct_member(fi, name, name);
if (!fi->data || !fi->name) { if (!rc)
rc = strdup_to_struct_member(fi, data, data);
if (rc) {
fdisk_script_free_header(fi); fdisk_script_free_header(fi);
return -ENOMEM; return rc;
} }
list_add_tail(&fi->headers, &dp->headers); list_add_tail(&fi->headers, &dp->headers);
} else { } else {