bitmap: Don't crash if allocation fails in bitmap_alloc0() & friends
authorKirill Smelkov <kirr@nexedi.com>
Mon, 1 Jun 2015 15:10:00 +0000 (18:10 +0300)
committerDavid Gibson <david@gibson.dropbear.id.au>
Tue, 2 Jun 2015 09:58:36 +0000 (19:58 +1000)
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@gibson.dropbear.id.au>
Signed-off-by: Kirill Smelkov <kirr@nexedi.com>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
ccan/bitmap/bitmap.h

index 6cbf9a22140655a3f55277edc44a5ff23c06f9e0..9e6c2bbc51912d797eec56000963756598d48592 100644 (file)
@@ -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;