Skip to contentSkip to conversation

vector: return null when realloc fails in vector_alloc_slot func

PATCH Superseded Zhiqiang Liu 3 participants 4 messages Jul 31, 2020 – Aug 10, 2020 Device mapper

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vector: return null when realloc fails in vector_alloc_slot func

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.

---
 libmultipath/vector.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libmultipath/vector.c +6 -4
index 501cf4c5..29dc9848 100644
@@ -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
Signature · 1 line
2.24.0.windows.2
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Zhiqiang Liu

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.

Quoted 33 lines

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

---
 libmultipath/vector.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libmultipath/vector.c +6 -4
index 501cf4c5..29dc9848 100644
@@ -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
Signature · 1 line
2.24.0.windows.2
#2 lore raw
In reply to Zhiqiang Liu

Hello Liu,

Quoted 26 lines · Zhiqiang Liu

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.

---
 libmultipath/vector.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libmultipath/vector.c +1 -1
index 501cf4c5..29dc9848 100644
@@ -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. */

libmultipath/vector.c vector_alloc_slot(vector v) · line 59

Quoted 10 lines
libmultipath/vector.c +5 -3
+	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

#3 lore raw
Quoted 51 lines · Martin Wilck

Hello Liu,

Quoted 26 lines · Zhiqiang Liu

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.

---
 libmultipath/vector.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libmultipath/vector.c +1 -1
index 501cf4c5..29dc9848 100644
@@ -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. */

libmultipath/vector.c vector_alloc_slot(vector v) · line 59

Quoted 10 lines
libmultipath/vector.c +5 -3
+	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:

Quoted 8 lines

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

Quoted 4 lines

.