diff --git a/libdm/libdm-stats.c b/libdm/libdm-stats.c index 6f05c01e3..6ccb60ab9 100644 --- a/libdm/libdm-stats.c +++ b/libdm/libdm-stats.c @@ -1954,10 +1954,9 @@ bad: static int _stats_set_aux(struct dm_stats *dms, uint64_t region_id, const char *user_data) { - const char *group_tag = NULL; + char *group_tag = NULL, *group_tag_escaped = NULL; struct dm_task *dmt = NULL; char msg[STATS_MSG_BUF_LEN]; - char *group_tag_escaped = NULL; /* group data required? */ if (_stats_group_id_present(dms, region_id)) { @@ -1970,8 +1969,7 @@ static int _stats_set_aux(struct dm_stats *dms, group_tag_escaped = _stats_escape_aux_data(group_tag); if (!group_tag_escaped) goto bad; - } else - group_tag_escaped = dm_strdup(""); + } if (dm_snprintf(msg, sizeof(msg), "@stats_set_aux " FMTu64 " %s%s%s ", region_id, (group_tag_escaped) ? group_tag_escaped : "", @@ -1984,7 +1982,7 @@ static int _stats_set_aux(struct dm_stats *dms, if (!(dmt = _stats_send_message(dms, msg))) goto_bad; - dm_free((char *) group_tag); + dm_free(group_tag); dm_free(group_tag_escaped); /* no response to a @stats_set_aux message */ diff --git a/test/shell/dmstats-group-ungroup-nouserdata.sh b/test/shell/dmstats-group-ungroup-nouserdata.sh new file mode 100644 index 000000000..ea72b596e --- /dev/null +++ b/test/shell/dmstats-group-ungroup-nouserdata.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Copyright (C) 2023 Red Hat, Inc. All rights reserved. +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions +# of the GNU General Public License v.2. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +SKIP_WITH_LVMPOLLD=1 +SKIP_WITH_LVMLOCKD=1 + +. lib/inittest + +# Don't attempt to test stats with driver < 4.33.00 +aux driver_at_least 4 33 || skip + +# ensure we can create devices (uses dmsetup, etc) +aux prepare_devs 1 + +GROUP_NAME="group0" +BAD_USERDATA="#-" + +# Create a region and make it part of a group with an alias +dmstats create "$dev1" +dmstats group --alias "$GROUP_NAME" --regions 0 "$dev1" +dmstats list -ostats_name |& tee out +grep "$GROUP_NAME" out + +# Ungroup the regions then remove them +dmstats ungroup --groupid 0 "$dev1" + +# Verify userdata does not contain '#-' +dmstats list -ouserdata |& tee out +not grep "$BAD_USERDATA" out + +# Clean up +dmstats delete --allregions "$dev1" diff --git a/test/shell/dmstats-group-ungroup-userdata.sh b/test/shell/dmstats-group-ungroup-userdata.sh new file mode 100644 index 000000000..109eb6f96 --- /dev/null +++ b/test/shell/dmstats-group-ungroup-userdata.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +# Copyright (C) 2023 Red Hat, Inc. All rights reserved. +# +# This copyrighted material is made available to anyone wishing to use, +# modify, copy, or redistribute it subject to the terms and conditions +# of the GNU General Public License v.2. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +SKIP_WITH_LVMPOLLD=1 +SKIP_WITH_LVMLOCKD=1 + +. lib/inittest + +# Don't attempt to test stats with driver < 4.33.00 +aux driver_at_least 4 33 || skip + +# ensure we can create devices (uses dmsetup, etc) +aux prepare_devs 1 + +GROUP_NAME="group0" +USERDATA="foo" + +# Create a region and make it part of a group with an alias +dmstats create "$dev1" --userdata "$USERDATA" +dmstats group --alias "$GROUP_NAME" --regions 0 "$dev1" +dmstats list -ostats_name |& tee out +grep "$GROUP_NAME" out +dmstats list -ouserdata |& tee out +grep "$USERDATA" out + +# Ungroup the regions then remove them +dmstats ungroup --groupid 0 "$dev1" + +# Verify userdata is still valid +dmstats list -ouserdata |& tee out +grep "$USERDATA" out + +# Clean up +dmstats delete --allregions "$dev1"