All of lore.kernel.org
 help / color / mirror / Atom feed
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 v2 4/8] libmpathutil: vector_del_slot: modify v->allocated if realloc fails
Date: Mon, 12 May 2025 14:34:46 -0400	[thread overview]
Message-ID: <aCI_RnU7azD4LQ1_@redhat.com> (raw)
In-Reply-To: <20250507190552.209637-5-mwilck@suse.com>

On Wed, May 07, 2025 at 09:05:48PM +0200, Martin Wilck wrote:
> We use v->allocated with the semantics of "length" or "number of used
> elements" of a vector. If realloc() fails while deleting an element,
> we should keep the previously allocated v->slot array but still
> decrement "allocated" in order to avoid accessing invalid vector
> elements.
> 
> The next successful realloc will bring v->allocated and the actual
> size of the array in sync again.
> 
> Signed-off-by: Martin Wilck <mwilck@suse.com>

Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>

> ---
>  libmpathutil/vector.c | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/libmpathutil/vector.c b/libmpathutil/vector.c
> index 7f763cb..f69d644 100644
> --- a/libmpathutil/vector.c
> +++ b/libmpathutil/vector.c
> @@ -113,7 +113,7 @@ vector_del_slot(vector v, int slot)
>  		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;
>  
> @@ -125,9 +125,15 @@ vector_del_slot(vector v, int slot)
>  		void *new_slot;
>  
>  		new_slot = realloc(v->slot, sizeof (void *) * v->allocated);
> -		if (!new_slot)
> -			v->allocated += VECTOR_DEFAULT_SIZE;
> -		else
> +		/*
> +		 * If realloc() fails, v->allocated will be smaller than the
> +		 * actual allocated size vector size.
> +		 * This is intentional; we want VECTOR_SIZE() to return
> +		 * the number of used elements. Otherwise, vector_for_each_slot()
> +		 * et al. wouldn't work as intended; there might be duplicate
> +		 * or stale elements at the end of the vector.
> +		 */
> +		if (new_slot)
>  			v->slot = new_slot;
>  	}
>  }
> -- 
> 2.49.0


  reply	other threads:[~2025-05-12 18:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-07 19:05 [PATCH v2 0/8] multipath-tools: static analyzer fixes Martin Wilck
2025-05-07 19:05 ` [PATCH v2 1/8] kpartx_id: fix shellcheck reported errors Martin Wilck
2025-05-07 19:05 ` [PATCH v2 2/8] kpartx: fix file descriptor leak Martin Wilck
2025-05-07 19:05 ` [PATCH v2 3/8] libmpathpersist: fix memory leak in mpath_prout_rel() Martin Wilck
2025-05-07 19:05 ` [PATCH v2 4/8] libmpathutil: vector_del_slot: modify v->allocated if realloc fails Martin Wilck
2025-05-12 18:34   ` Benjamin Marzinski [this message]
2025-05-07 19:05 ` [PATCH v2 5/8] libmultipath: fix undefined behavior in 31-bit shift Martin Wilck
2025-05-07 19:05 ` [PATCH v2 6/8] libmultipath: prioritizers/iet: fix possible NULL dereference Martin Wilck
2025-05-07 19:05 ` [PATCH v2 7/8] libmpathutil: remove vector_repack() Martin Wilck
2025-05-12 18:35   ` Benjamin Marzinski
2025-05-07 19:05 ` [PATCH v2 8/8] libmpathutil: remove VECTOR_DEFAULT_SIZE macro Martin Wilck
2025-05-12 18:44   ` 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=aCI_RnU7azD4LQ1_@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.