On Fri, Jul 12, 2024 at 07:14:24PM +0200, Martin Wilck wrote:
> This allows us to get rid of a lot of goto statements, and generally
> obtain cleaner code.
>
> Signed-off-by: Martin Wilck <[email protected]>
> ---
> libmultipath/devmapper.c | 258 ++++++++++++++++-----------------------
> 1 file changed, 107 insertions(+), 151 deletions(-)
>
> diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c
> index 8996c1d..4bff62d 100644
> --- a/libmultipath/devmapper.c
> +++ b/libmultipath/devmapper.c
> @@ -1419,10 +1396,10 @@ do_foreach_partmaps (const char * mapname,
> int (*partmap_func)(const char *, void *),
> void *data)
> {
> - struct dm_task *dmt;
> + struct dm_task __attribute__((cleanup(cleanup_dm_task))) *dmt = NULL;
> + char __attribute__((cleanup(cleanup_charp))) *params = NULL;
> struct dm_names *names;
> unsigned next = 0;
> - char *params = NULL;
> unsigned long long size;
> char dev_t[32];
> int r = 1;
Nitpick: You can remove 'r' now.
-Ben
> @@ -1431,21 +1408,18 @@ do_foreach_partmaps (const char * mapname,
> if (!(dmt = libmp_dm_task_create(DM_DEVICE_LIST)))
> return 1;
>
> - if (!libmp_dm_task_run(dmt)) {
> - dm_log_error(3, DM_DEVICE_LIST, dmt);
> - goto out;
> - }
> + if (!libmp_dm_task_run(dmt))
> + return 1;
>
> if (!(names = dm_task_get_names(dmt)))
> - goto out;
> + return 1;
>
> - if (!names->dev) {
> - r = 0; /* this is perfectly valid */
> - goto out;
> - }
> + if (!names->dev)
> + /* this is perfectly valid */
> + return 0;
>
> if (dm_dev_t(mapname, &dev_t[0], 32))
> - goto out;
> + return 1;
>
> do {
> if (
> @@ -1472,7 +1446,7 @@ do_foreach_partmaps (const char * mapname,
> !isdigit(*(p + strlen(dev_t)))
> ) {
> if ((r = partmap_func(names->name, data)) != 0)
> - goto out;
> + return 1;
> }
>
> free(params);
> @@ -1481,11 +1455,7 @@ do_foreach_partmaps (const char * mapname,
> names = (void *) names + next;
> } while (next);
>
> - r = 0;
> -out:
> - free(params);
> - dm_task_destroy (dmt);
> - return r;
> + return 0;
> }