From: Benjamin Marzinski <bmarzins@redhat.com>
To: Martin Wilck <martin.wilck@suse.com>
Cc: Christophe Varoqui <christophe.varoqui@opensvc.com>,
Xose Vazquez Perez <xose.vazquez@gmail.com>,
Martin Wilck <mwilck@suse.com>,
dm-devel@lists.linux.dev
Subject: Re: [PATCH 4/6] libmpathutil: silence compiler warning in vector_del_slot()
Date: Tue, 6 May 2025 18:01:42 -0400 [thread overview]
Message-ID: <aBqGxgGHFuvADCQ4@redhat.com> (raw)
In-Reply-To: <20250505163007.59352-5-mwilck@suse.com>
On Mon, May 05, 2025 at 06:29:56PM +0200, Martin Wilck wrote:
> Try to silence a gcc warning. Also, replace the wrong-looking
> VECTOR_DEFAULT_SIZE by 1 (after all, we've just deleted a single
> element).
>
> Found by Fedora's static analysis [1].
>
> [1] https://openscanhub.fedoraproject.org/task/51915/log/device-mapper-multipath-0.11.1-1.fc43/scan-results.html
>
> Signed-off-by: Martin Wilck <mwilck@suse.com>
> ---
> libmpathutil/vector.c | 16 ++++++++--------
> 1 file changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/libmpathutil/vector.c b/libmpathutil/vector.c
> index 7f763cb..3386651 100644
> --- a/libmpathutil/vector.c
> +++ b/libmpathutil/vector.c
> @@ -107,28 +107,28 @@ int find_slot(vector v, const void *addr)
> void
> vector_del_slot(vector v, int slot)
> {
> - int i;
> + int i, allocated;
>
> if (!v || !v->allocated || slot < 0 || slot >= VECTOR_SIZE(v))
> return;
>
> for (i = slot + 1; i < VECTOR_SIZE(v); i++)
> - v->slot[i-1] = v->slot[i];
> + v->slot[i - 1] = v->slot[i];
>
> - v->allocated -= VECTOR_DEFAULT_SIZE;
> + allocated = v->allocated - 1;
>
> - if (v->allocated <= 0) {
> + if (allocated <= 0) {
> free(v->slot);
> v->slot = NULL;
> v->allocated = 0;
> } else {
> void *new_slot;
>
> - new_slot = realloc(v->slot, sizeof (void *) * v->allocated);
> - if (!new_slot)
> - v->allocated += VECTOR_DEFAULT_SIZE;
> - else
> + new_slot = realloc(v->slot, sizeof(void *) * allocated);
> + if (new_slot) {
> v->slot = new_slot;
> + v->allocated = allocated;
> + }
Actually, now that I think about this a bit more, I don't think that
this was ever the right thing to do. If the realloc() fails, I see no
harm in keeping the smaller v->allocated value. Future realloc()s and
free()s will still work correctly. On the other hand, if we restore the
old, larger v->allocated value (or with your code, never reduce it in
the first place) there will be a duplicate value on the list, which I
can definitely see causing problems.
-Ben
> }
> }
>
> --
> 2.49.0
next prev parent reply other threads:[~2025-05-06 22:01 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-05 16:29 [PATCH 0/6] multipath-tools: static analyzer fixes Martin Wilck
2025-05-05 16:29 ` [PATCH 1/6] kpartx_id: fix shellcheck reported errors Martin Wilck
2025-05-05 16:29 ` [PATCH 2/6] kpartx: fix file descriptor leak Martin Wilck
2025-05-05 16:29 ` [PATCH 3/6] libmpathpersist: fix memory leak in mpath_prout_rel() Martin Wilck
2025-05-05 16:29 ` [PATCH 4/6] libmpathutil: silence compiler warning in vector_del_slot() Martin Wilck
2025-05-06 19:41 ` Benjamin Marzinski
2025-05-06 22:01 ` Benjamin Marzinski [this message]
2025-05-07 7:19 ` Martin Wilck
2025-05-05 16:29 ` [PATCH 5/6] libmultipath: fix undefined behavior in 31-bit shift Martin Wilck
2025-05-05 16:29 ` [PATCH 6/6] libmultipath: prioritizers/iet: fix possible NULL dereference Martin Wilck
2025-05-06 22:26 ` [PATCH 0/6] multipath-tools: static analyzer fixes Benjamin Marzinski
2025-05-07 7:14 ` Martin Wilck
2025-05-07 14:24 ` Benjamin Marzinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aBqGxgGHFuvADCQ4@redhat.com \
--to=bmarzins@redhat.com \
--cc=christophe.varoqui@opensvc.com \
--cc=dm-devel@lists.linux.dev \
--cc=martin.wilck@suse.com \
--cc=mwilck@suse.com \
--cc=xose.vazquez@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.