From c38e11b508e52fb2921e67d1123b05d9bef90fd2 Mon Sep 17 00:00:00 2001 From: Kirill Smelkov Date: Mon, 1 Jun 2015 18:10:00 +0300 Subject: [PATCH] bitmap: Don't crash if allocation fails in bitmap_alloc0() & friends 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 Signed-off-by: Kirill Smelkov Signed-off-by: David Gibson --- 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 6cbf9a22..9e6c2bbc 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.39.2