Skip to content

Commit 90c455a

Browse files
authored
Fix a bug in the bitset_shift calculations, resulting in a corrupted state after downsizing the allocator (#133)
Closes #128
1 parent 2f43991 commit 90c455a

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

buddy_alloc.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,9 +2068,8 @@ static void bitset_shift_left(unsigned char *bitset, size_t from_pos, size_t to_
20682068
} else {
20692069
bitset_clear(bitset, at-by);
20702070
}
2071+
bitset_clear(bitset, at);
20712072
}
2072-
bitset_clear_range(bitset, bitset_range(length, length+by-1));
2073-
20742073
}
20752074

20762075
static void bitset_shift_right(unsigned char *bitset, size_t from_pos, size_t to_pos, size_t by) {
@@ -2082,9 +2081,9 @@ static void bitset_shift_right(unsigned char *bitset, size_t from_pos, size_t to
20822081
} else {
20832082
bitset_clear(bitset, at+by);
20842083
}
2084+
bitset_clear(bitset, at);
20852085
length -= 1;
20862086
}
2087-
bitset_clear_range(bitset, bitset_range(from_pos, from_pos+by-1));
20882087
}
20892088

20902089
void bitset_debug(unsigned char *bitset, size_t length) {

tests.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ void test_bitset_range(void) {
122122
void test_bitset_shift(void) {
123123
unsigned char *buf = malloc(bitset_sizeof(16));
124124
START_TEST;
125-
for (size_t i = 0; i < bitset_sizeof(16); i++) {
126-
buf[i] = 0;
127-
}
128125
for (size_t i = 0; i < 16; i++) {
129126
bitset_clear(buf, i);
130127
}
@@ -166,6 +163,18 @@ void test_bitset_shift(void) {
166163
assert(!bitset_test(buf, 13));
167164
assert(!bitset_test(buf, 14));
168165
assert(!bitset_test(buf, 15));
166+
167+
for (size_t i = 0; i < 16; i++) {
168+
bitset_set(buf, i);
169+
}
170+
bitset_shift_left(buf, 4, 5, 1);
171+
for (size_t i = 0; i < 16; i++) {
172+
if (i == 4) {
173+
assert(!bitset_test(buf, i));
174+
} else {
175+
assert(bitset_test(buf, i));
176+
}
177+
}
169178
free(buf);
170179
}
171180

0 commit comments

Comments
 (0)