Commit dd514513 authored by Steve Beattie's avatar Steve Beattie

Subject: parser - use DUP_STRING more widely and detect strdup errors

This patch moves the DUP_STRING macro to parser.h and modifies
it to accept a goto error target, that will be jumped to if the
call to strdup(3) fails. It also uses it in additional locations
where copying structures occurs, as well as detecting additional
cases where a structure duplication might have failed but not been
propagated outward.
Signed-off-by: Steve Beattie's avatarSteve Beattie <steve@nxnw.org>
Acked-by: Tyler Hicks's avatarTyler Hicks <tyhicks@canonical.com>
parent ebabb30a
......@@ -141,9 +141,6 @@ out:
return ent;
}
#define DUP_STRING(orig, new, field) \
(new)->field = (orig)->field ? strdup((orig)->field) : NULL
struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig)
{
struct dbus_entry *ent = NULL;
......@@ -151,12 +148,12 @@ struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig)
if (!ent)
return NULL;
DUP_STRING(orig, ent, bus);
DUP_STRING(orig, ent, name);
DUP_STRING(orig, ent, peer_label);
DUP_STRING(orig, ent, path);
DUP_STRING(orig, ent, interface);
DUP_STRING(orig, ent, member);
DUP_STRING(orig, ent, bus, err);
DUP_STRING(orig, ent, name, err);
DUP_STRING(orig, ent, peer_label, err);
DUP_STRING(orig, ent, path, err);
DUP_STRING(orig, ent, interface, err);
DUP_STRING(orig, ent, member, err);
ent->mode = orig->mode;
ent->audit = orig->audit;
ent->deny = orig->deny;
......@@ -164,6 +161,10 @@ struct dbus_entry *dup_dbus_entry(struct dbus_entry *orig)
ent->next = orig->next;
return ent;
err:
free_dbus_entry(ent);
return NULL;
}
void print_dbus_entry(struct dbus_entry *ent)
......
......@@ -466,7 +466,6 @@ void free_mnt_entry(struct mnt_entry *ent)
free(ent);
}
struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig)
{
struct mnt_entry *entry = NULL;
......@@ -475,12 +474,17 @@ struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig)
if (!entry)
return NULL;
entry->mnt_point = orig->mnt_point ? strdup(orig->mnt_point) : NULL;
entry->device = orig->device ? strdup(orig->device) : NULL;
entry->trans = orig->trans ? strdup(orig->trans) : NULL;
DUP_STRING(orig, entry, mnt_point, err);
DUP_STRING(orig, entry, device, err);
DUP_STRING(orig, entry, trans, err);
entry->dev_type = dup_value_list(orig->dev_type);
if (orig->dev_type && !(entry->dev_type))
goto err;
entry->opts = dup_value_list(orig->opts);
if (orig->opts && !(entry->opts))
goto err;
entry->flags = orig->flags;
entry->inv_flags = orig->inv_flags;
......@@ -492,6 +496,10 @@ struct mnt_entry *dup_mnt_entry(struct mnt_entry *orig)
entry->next = orig->next;
return entry;
err:
free_mnt_entry(entry);
return NULL;
}
void print_mnt_entry(struct mnt_entry *entry)
......
......@@ -261,6 +261,13 @@ extern int preprocess_only;
___tmp->next = (LISTB); \
} while (0)
#define DUP_STRING(orig, new, field, fail_target) \
do { \
(new)->field = ((orig)->field) ? strdup((orig)->field) : NULL; \
if (((orig)->field) && !((new)->field)) \
goto fail_target; \
} while (0)
/* from parser_common.c */
extern int regex_type;
extern int perms_create;
......
......@@ -840,9 +840,9 @@ struct cod_entry *copy_cod_entry(struct cod_entry *orig)
if (!entry)
return NULL;
entry->namespace = orig->namespace ? strdup(orig->namespace) : NULL;
entry->name = strdup(orig->name);
entry->link_name = orig->link_name ? strdup(orig->link_name) : NULL;
DUP_STRING(orig, entry, namespace, err);
DUP_STRING(orig, entry, name, err);
DUP_STRING(orig, entry, link_name, err);
entry->mode = orig->mode;
entry->audit = orig->audit;
entry->deny = orig->deny;
......@@ -854,6 +854,10 @@ struct cod_entry *copy_cod_entry(struct cod_entry *orig)
entry->next = orig->next;
return entry;
err:
free_cod_entries(entry);
return NULL;
}
void free_cod_entries(struct cod_entry *list)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment