Re: [PATCH 4/6] libmpathutil: silence compiler warning in vector_del_slot()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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).

I agree that VECTOR_DEFAULT_SIZE is pointless. But we shoud also stop
adding VECTOR_DEFAULT_SIZE to v->allocated in vector_alloc_slot() and
dividing v->allocated by VECTOR_DEFAULT_SIZE in VECTOR_SIZE(), otherwise
vector_del_slot() doesn't match vector_alloc_slot() and VECTOR_SIZE().
My vote is for VECTOR_DEFAULT_SIZE to go away completely.

-Ben

> 
> 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@xxxxxxxx>
> ---
>  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;
> +		}
>  	}
>  }
>  
> -- 
> 2.49.0





[Index of Archives]     [DM Crypt]     [Fedora Desktop]     [ATA RAID]     [Fedora Marketing]     [Fedora Packaging]     [Fedora SELinux]     [Yosemite Discussion]     [KDE Users]     [Fedora Docs]

  Powered by Linux