All of lore.kernel.org
 help / color / mirror / Atom feed
* [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func
@ 2020-07-31 10:41 Zhiqiang Liu
  2020-07-31 17:11 ` Benjamin Marzinski
  2020-08-10 13:48 ` Martin Wilck
  0 siblings, 2 replies; 4+ messages in thread
From: Zhiqiang Liu @ 2020-07-31 10:41 UTC (permalink / raw)
  To: bmarzins, Martin Wilck, christophe.varoqui, kabelac
  Cc: linfeilong, Yanxiaodan, dm-devel, lixiaokeng


In vector_alloc_slot func, if REALLOC fails, it means new slot
allocation fails. However, it just update v->allocated and then
return the old v->slot without new slot. So, the caller will take
the last old slot as the new allocated slot, and use it by calling
vector_set_slot func. Finally, the data of last slot is lost.

Here, if REALLOC or MALLOC fails, we will return NULL.

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
---
 libmultipath/vector.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/libmultipath/vector.c b/libmultipath/vector.c
index 501cf4c5..29dc9848 100644
--- a/libmultipath/vector.c
+++ b/libmultipath/vector.c
@@ -49,12 +49,14 @@ vector_alloc_slot(vector v)
 	else
 		new_slot = (void *) MALLOC(sizeof (void *) * v->allocated);

-	if (!new_slot)
+	/* If REALLOC or MALLOC fails, it means new slot allocation fails, so return NULL. */
+	if (!new_slot) {
 		v->allocated -= VECTOR_DEFAULT_SIZE;
-	else
-		v->slot = new_slot;
+		return NULL;
+	}

-	return v->slot;
+	v->slot = new_slot;
+	return v->slot[VECTOR_SIZE(v) - 1];
 }

 int
-- 
2.24.0.windows.2

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func
  2020-07-31 10:41 [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func Zhiqiang Liu
@ 2020-07-31 17:11 ` Benjamin Marzinski
  2020-08-10 13:48 ` Martin Wilck
  1 sibling, 0 replies; 4+ messages in thread
From: Benjamin Marzinski @ 2020-07-31 17:11 UTC (permalink / raw)
  To: Zhiqiang Liu
  Cc: lixiaokeng, Yanxiaodan, linfeilong, dm-devel, kabelac, Martin Wilck

On Fri, Jul 31, 2020 at 06:41:57PM +0800, Zhiqiang Liu wrote:
> 
> In vector_alloc_slot func, if REALLOC fails, it means new slot
> allocation fails. However, it just update v->allocated and then
> return the old v->slot without new slot. So, the caller will take
> the last old slot as the new allocated slot, and use it by calling
> vector_set_slot func. Finally, the data of last slot is lost.
>

Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
 
> Here, if REALLOC or MALLOC fails, we will return NULL.
> 
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
> ---
>  libmultipath/vector.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/libmultipath/vector.c b/libmultipath/vector.c
> index 501cf4c5..29dc9848 100644
> --- a/libmultipath/vector.c
> +++ b/libmultipath/vector.c
> @@ -49,12 +49,14 @@ vector_alloc_slot(vector v)
>  	else
>  		new_slot = (void *) MALLOC(sizeof (void *) * v->allocated);
> 
> -	if (!new_slot)
> +	/* If REALLOC or MALLOC fails, it means new slot allocation fails, so return NULL. */
> +	if (!new_slot) {
>  		v->allocated -= VECTOR_DEFAULT_SIZE;
> -	else
> -		v->slot = new_slot;
> +		return NULL;
> +	}
> 
> -	return v->slot;
> +	v->slot = new_slot;
> +	return v->slot[VECTOR_SIZE(v) - 1];
>  }
> 
>  int
> -- 
> 2.24.0.windows.2

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func
  2020-07-31 10:41 [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func Zhiqiang Liu
  2020-07-31 17:11 ` Benjamin Marzinski
@ 2020-08-10 13:48 ` Martin Wilck
  2020-08-10 13:59   ` Zhiqiang Liu
  1 sibling, 1 reply; 4+ messages in thread
From: Martin Wilck @ 2020-08-10 13:48 UTC (permalink / raw)
  To: Zhiqiang Liu, bmarzins, christophe.varoqui, kabelac
  Cc: linfeilong, Yanxiaodan, dm-devel, lixiaokeng

Hello Liu,

On Fri, 2020-07-31 at 18:41 +0800, Zhiqiang Liu wrote:
> In vector_alloc_slot func, if REALLOC fails, it means new slot
> allocation fails. However, it just update v->allocated and then
> return the old v->slot without new slot. So, the caller will take
> the last old slot as the new allocated slot, and use it by calling
> vector_set_slot func. Finally, the data of last slot is lost.
> 
> Here, if REALLOC or MALLOC fails, we will return NULL.
> 
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
> ---
>  libmultipath/vector.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/libmultipath/vector.c b/libmultipath/vector.c
> index 501cf4c5..29dc9848 100644
> --- a/libmultipath/vector.c
> +++ b/libmultipath/vector.c
> @@ -49,12 +49,14 @@ vector_alloc_slot(vector v)
>  	else
>  		new_slot = (void *) MALLOC(sizeof (void *) * v-
> >allocated);
> 
> -	if (!new_slot)
> +	/* If REALLOC or MALLOC fails, it means new slot allocation
> fails, so return NULL. */
> +	if (!new_slot) {
>  		v->allocated -= VECTOR_DEFAULT_SIZE;
> -	else
> -		v->slot = new_slot;
> +		return NULL;
> +	}
> 
> -	return v->slot;
> +	v->slot = new_slot;
> +	return v->slot[VECTOR_SIZE(v) - 1];

This changes the semantics of the function by returning the last 
element of the vector rather than v->slot. That's dangerous because
these elements aren't initialized. You might as well return NULL in
case of success, which would obviously be wrong (actually, new elements
_should_ be initialized to NULL). As the return value is only ever used
to check for successful allocation, it might be best to change it into
a bool, avoiding any ambiguity about its meaning.

If you want to clean up this function (appreciated!), please do it
right:

 - increment v->allocated only after successful allocation,
 - avoid the "if (v->slot)" conditional by just calling realloc(),
 - make sure all newly allocated vector elements are set to NULL,
 - optionally, change return value to bool (see above).

Regards,
Martin

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func
  2020-08-10 13:48 ` Martin Wilck
@ 2020-08-10 13:59   ` Zhiqiang Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Zhiqiang Liu @ 2020-08-10 13:59 UTC (permalink / raw)
  To: Martin Wilck, bmarzins, christophe.varoqui, kabelac
  Cc: linfeilong, Yanxiaodan, dm-devel, lixiaokeng



On 2020/8/10 21:48, Martin Wilck wrote:
> Hello Liu,
> 
> On Fri, 2020-07-31 at 18:41 +0800, Zhiqiang Liu wrote:
>> In vector_alloc_slot func, if REALLOC fails, it means new slot
>> allocation fails. However, it just update v->allocated and then
>> return the old v->slot without new slot. So, the caller will take
>> the last old slot as the new allocated slot, and use it by calling
>> vector_set_slot func. Finally, the data of last slot is lost.
>>
>> Here, if REALLOC or MALLOC fails, we will return NULL.
>>
>> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
>> Signed-off-by: lixiaokeng <lixiaokeng@huawei.com>
>> ---
>>  libmultipath/vector.c | 10 ++++++----
>>  1 file changed, 6 insertions(+), 4 deletions(-)
>>
>> diff --git a/libmultipath/vector.c b/libmultipath/vector.c
>> index 501cf4c5..29dc9848 100644
>> --- a/libmultipath/vector.c
>> +++ b/libmultipath/vector.c
>> @@ -49,12 +49,14 @@ vector_alloc_slot(vector v)
>>  	else
>>  		new_slot = (void *) MALLOC(sizeof (void *) * v-
>>> allocated);
>>
>> -	if (!new_slot)
>> +	/* If REALLOC or MALLOC fails, it means new slot allocation
>> fails, so return NULL. */
>> +	if (!new_slot) {
>>  		v->allocated -= VECTOR_DEFAULT_SIZE;
>> -	else
>> -		v->slot = new_slot;
>> +		return NULL;
>> +	}
>>
>> -	return v->slot;
>> +	v->slot = new_slot;
>> +	return v->slot[VECTOR_SIZE(v) - 1];
> 
> This changes the semantics of the function by returning the last 
> element of the vector rather than v->slot. That's dangerous because
> these elements aren't initialized. You might as well return NULL in
> case of success, which would obviously be wrong (actually, new elements
> _should_ be initialized to NULL). As the return value is only ever used
> to check for successful allocation, it might be best to change it into
> a bool, avoiding any ambiguity about its meaning.
> 
> If you want to clean up this function (appreciated!), please do it
> right:
> 
>  - increment v->allocated only after successful allocation,
>  - avoid the "if (v->slot)" conditional by just calling realloc(),
>  - make sure all newly allocated vector elements are set to NULL,
>  - optionally, change return value to bool (see above).
> 
> Regards,
> Martin
> 
Thanks for your suggestion.
I will rewrite and send the v2 patch as your suggestion.

> 
> 
> .
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-10 13:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-31 10:41 [dm- devel][PATCH] vector: return null when realloc fails in vector_alloc_slot func Zhiqiang Liu
2020-07-31 17:11 ` Benjamin Marzinski
2020-08-10 13:48 ` Martin Wilck
2020-08-10 13:59   ` Zhiqiang Liu

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.