[ccan] [PATCH] bitmap: Don't crash if allocation fails in bitmap_alloc0() & friends

Kirill Smelkov kirr at nexedi.com
Tue Jun 2 01:10:00 AEST 2015


Currently, if allocation fails, inside bitmap_alloc0(), we'll continue
to use bitmap=NULL pointer and pass it to bitmap_zero() which will
SIGSEGV.

Cc: David Gibson <david at gibson.dropbear.id.au>
Signed-off-by: Kirill Smelkov <kirr at nexedi.com>
---
 ccan/bitmap/bitmap.h | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/ccan/bitmap/bitmap.h b/ccan/bitmap/bitmap.h
index 6cbf9a2..9e6c2bb 100644
--- a/ccan/bitmap/bitmap.h
+++ b/ccan/bitmap/bitmap.h
@@ -203,7 +203,8 @@ static inline bitmap *bitmap_alloc0(unsigned long nbits)
 	bitmap *bitmap;
 
 	bitmap = bitmap_alloc(nbits);
-	bitmap_zero(bitmap, nbits);
+	if (bitmap)
+		bitmap_zero(bitmap, nbits);
 	return bitmap;
 }
 
@@ -212,7 +213,8 @@ static inline bitmap *bitmap_alloc1(unsigned long nbits)
 	bitmap *bitmap;
 
 	bitmap = bitmap_alloc(nbits);
-	bitmap_fill(bitmap, nbits);
+	if (bitmap)
+		bitmap_fill(bitmap, nbits);
 	return bitmap;
 }
 
@@ -221,7 +223,7 @@ static inline bitmap *bitmap_realloc0(bitmap *bitmap,
 {
 	bitmap = realloc(bitmap, bitmap_sizeof(nbits));
 
-	if (nbits > obits)
+	if ((nbits > obits) && bitmap)
 		bitmap_zero_range(bitmap, obits, nbits);
 
 	return bitmap;
@@ -232,7 +234,7 @@ static inline bitmap *bitmap_realloc1(bitmap *bitmap,
 {
 	bitmap = realloc(bitmap, bitmap_sizeof(nbits));
 
-	if (nbits > obits)
+	if ((nbits > obits) && bitmap)
 		bitmap_fill_range(bitmap, obits, nbits);
 
 	return bitmap;
-- 
2.4.2.530.ge51b0dd


More information about the ccan mailing list