From 55348b420e618b1c5c50106261437fe948ef2132 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 23 Jan 2013 10:15:05 -0700 Subject: [PATCH] axmap: fix bug in axmap_find_first_free() If the map is completely full, we could return an invalid value for the first free bit. So check if we actually found a match, return failure if we didn't. Signed-off-by: Jens Axboe --- lib/axmap.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/axmap.c b/lib/axmap.c index d9ad30bf..7351e297 100644 --- a/lib/axmap.c +++ b/lib/axmap.c @@ -309,7 +309,7 @@ static uint64_t axmap_find_first_free(struct axmap *axmap, unsigned int level, uint64_t index) { unsigned long j; - int i; + int i, found = 0; /* * Start at the bottom, then converge towards first free bit at the top @@ -331,11 +331,12 @@ static uint64_t axmap_find_first_free(struct axmap *axmap, unsigned int level, * free bit at the next higher level */ index = (j << UNIT_SHIFT) + ffz(al->map[j]); + found = 1; break; } } - return index; + return found ? index : (uint64_t) -1ULL; } uint64_t axmap_first_free(struct axmap *axmap) -- 2.25.1