From e46ff49b6d65a5969b658698445d284099516ef9 Mon Sep 17 00:00:00 2001 From: krw Date: Wed, 21 Jun 2023 12:50:09 +0000 Subject: [PATCH] Add a parameter 'action' to mpfree() so it can optionally discard or keep the memory pointed at by the char ** parameter. Allows mpfree() to be used against the global mountpoints[] and eliminates a couple of manual loops to free the current contents of mountpoints[]. No intentional functional change. --- sbin/disklabel/disklabel.c | 4 ++-- sbin/disklabel/editor.c | 33 ++++++++++++++++----------------- sbin/disklabel/extern.h | 8 ++++++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/sbin/disklabel/disklabel.c b/sbin/disklabel/disklabel.c index 031e9197593..b520ac4c29d 100644 --- a/sbin/disklabel/disklabel.c +++ b/sbin/disklabel/disklabel.c @@ -1,4 +1,4 @@ -/* $OpenBSD: disklabel.c,v 1.250 2023/06/19 13:45:19 krw Exp $ */ +/* $OpenBSD: disklabel.c,v 1.251 2023/06/21 12:50:09 krw Exp $ */ /* * Copyright (c) 1987, 1993 @@ -1005,7 +1005,7 @@ next: if (errors > 0) mpcopy(mountpoints, omountpoints); - mpfree(omountpoints); + mpfree(omountpoints, DISCARD); return (errors > 0); } diff --git a/sbin/disklabel/editor.c b/sbin/disklabel/editor.c index 183f7e6de1a..64c534c7651 100644 --- a/sbin/disklabel/editor.c +++ b/sbin/disklabel/editor.c @@ -1,4 +1,4 @@ -/* $OpenBSD: editor.c,v 1.410 2023/06/19 13:45:19 krw Exp $ */ +/* $OpenBSD: editor.c,v 1.411 2023/06/21 12:50:09 krw Exp $ */ /* * Copyright (c) 1997-2000 Todd C. Miller @@ -501,9 +501,9 @@ editor(int f) } } done: - mpfree(omountpoints); - mpfree(origmountpoints); - mpfree(tmpmountpoints); + mpfree(omountpoints, DISCARD); + mpfree(origmountpoints, DISCARD); + mpfree(tmpmountpoints, DISCARD); return (error); } @@ -556,10 +556,7 @@ again: if (index >= alloc_table_nitems) return 1; lp = &label; - for (i = 0; i < MAXPARTITIONS; i++) { - free(mountpoints[i]); - mountpoints[i] = NULL; - } + mpfree(mountpoints, KEEP); memcpy(lp, lp_org, sizeof(struct disklabel)); lp->d_npartitions = MAXPARTITIONS; lastalloc = alloc_table[index].sz; @@ -1541,17 +1538,22 @@ mpsave(const struct disklabel *lp) } void -mpfree(char **mp) +mpfree(char **mp, int action) { int part; if (mp == NULL) return; - for (part = 0; part < MAXPARTITIONS; part++) + for (part = 0; part < MAXPARTITIONS; part++) { free(mp[part]); + mp[part] = NULL; + } - free(mp); + if (action == DISCARD) { + free(mp); + mp = NULL; + } } int @@ -1784,13 +1786,10 @@ zero_partitions(struct disklabel *lp) { int i; - for (i = 0; i < MAXPARTITIONS; i++) { - memset(&lp->d_partitions[i], 0, sizeof(struct partition)); - free(mountpoints[i]); - mountpoints[i] = NULL; - } - + memset(lp->d_partitions, 0, sizeof(lp->d_partitions)); DL_SETPSIZE(&lp->d_partitions[RAW_PART], DL_GETDSIZE(lp)); + + mpfree(mountpoints, KEEP); } u_int64_t diff --git a/sbin/disklabel/extern.h b/sbin/disklabel/extern.h index 174ebc4a442..a6c7e3a1459 100644 --- a/sbin/disklabel/extern.h +++ b/sbin/disklabel/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.35 2023/01/04 21:08:08 krw Exp $ */ +/* $OpenBSD: extern.h,v 1.36 2023/06/21 12:50:09 krw Exp $ */ /* * Copyright (c) 2003 Theo de Raadt @@ -19,6 +19,10 @@ #define MEG(x) ((x) * 1024LL * (1024 / DEV_BSIZE)) #define GIG(x) (MEG(x) * 1024LL) +/* 'actions' for mpfree(). */ +#define DISCARD 0 +#define KEEP 1 + u_short dkcksum(const struct disklabel *); char canonical_unit(const struct disklabel *, char); double scale(u_int64_t, char, const struct disklabel *); @@ -30,7 +34,7 @@ int editor(int); int editor_allocspace(struct disklabel *); void mpsave(const struct disklabel *); void mpcopy(char **, char **); -void mpfree(char **); +void mpfree(char **, int); void parse_autotable(char *); int writelabel(int, struct disklabel *); -- 2.20.1