Index: generic/src/adt/bitmap.c
===================================================================
--- generic/src/adt/bitmap.c	(revision 0e00b8a5cc5882f0ad156a2e02658b465311a7b2)
+++ generic/src/adt/bitmap.c	(revision 4fd61ba9ecb8f0f9026f3fa3a2b1cd920756fdb4)
@@ -40,4 +40,5 @@
 #include <align.h>
 #include <debug.h>
+#include <macros.h>
 
 #define ALL_ONES 	0xff
@@ -69,11 +70,13 @@
 	index_t aligned_start;
 	count_t lub;		/* leading unaligned bits */
-	count_t tub;		/* trailing unaligned bits */
+	count_t amb;		/* aligned middle bits */
+	count_t tab;		/* trailing aligned bits */
 	
 	ASSERT(start + bits <= bitmap->bits);
 	
 	aligned_start = ALIGN_UP(start, 8);
-	lub = aligned_start - start;
-	tub = (bits - lub) % 8;
+	lub = min(aligned_start - start, bits);
+	amb = bits > lub ? bits - lub : 0;
+	tab = amb % 8;
 	
 	if (lub) {
@@ -83,5 +86,5 @@
 		bitmap->map[start / 8] |= ~((1 << (8 - lub)) - 1);
 	}
-	for (i = 0; i < (bits - lub) / 8; i++) {
+	for (i = 0; i < amb / 8; i++) {
 		/*
 		 * The middle bits can be set byte by byte.
@@ -89,9 +92,9 @@
 		bitmap->map[aligned_start / 8 + i] = ALL_ONES;
 	}
-	if (tub) {
+	if (tab) {
 		/*
-		 * Make sure to set any trailing unaligned bits.
+		 * Make sure to set any trailing aligned bits.
 		 */
-		bitmap->map[aligned_start / 8 + i] |= (1 << tub) - 1;
+		bitmap->map[aligned_start / 8 + i] |= (1 << tab) - 1;
 	}
 	
@@ -109,12 +112,14 @@
 	index_t aligned_start;
 	count_t lub;		/* leading unaligned bits */
-	count_t tub;		/* trailing unaligned bits */
+	count_t amb;		/* aligned middle bits */
+	count_t tab;		/* trailing aligned bits */
 	
 	ASSERT(start + bits <= bitmap->bits);
 	
 	aligned_start = ALIGN_UP(start, 8);
-	lub = aligned_start - start;
-	tub = (bits - lub) % 8;
-	
+	lub = min(aligned_start - start, bits);
+	amb = bits > lub ? bits - lub : 0;
+	tab = amb % 8;
+
 	if (lub) {
 		/*
@@ -123,5 +128,5 @@
 		bitmap->map[start / 8] &= (1 << (8 - lub)) - 1;
 	}
-	for (i = 0; i < (bits - lub) / 8; i++) {
+	for (i = 0; i < amb / 8; i++) {
 		/*
 		 * The middle bits can be cleared byte by byte.
@@ -129,11 +134,11 @@
 		bitmap->map[aligned_start / 8 + i] = ALL_ZEROES;
 	}
-	if (tub) {
+	if (tab) {
 		/*
-		 * Make sure to clear any trailing unaligned bits.
+		 * Make sure to clear any trailing aligned bits.
 		 */
-		bitmap->map[aligned_start / 8 + i] &= ~((1 << tub) - 1);
+		bitmap->map[aligned_start / 8 + i] &= ~((1 << tab) - 1);
 	}
-	
+
 }
 
