Skip to content

Commit

Permalink
Fix bitfield inconsistency after 2 ** 15 (#631)
Browse files Browse the repository at this point in the history
* Failing tests for large-bitfield inconsistency

* test setting bitfield range with page offset

* fix bitfield setRange offset and remove test

---------

Co-authored-by: Christophe Diederichs <chm-diederichs@protonmail.com>
  • Loading branch information
HDegroote and chm-diederichs authored Feb 4, 2025
1 parent cb7c1be commit d198285
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/bitfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,15 @@ module.exports = class Bitfield {
p = this._pages.set(i, new BitfieldPage(i, s))
}

const last = Math.min(end, BITS_PER_PAGE)
const offset = p.index * BITS_PER_PAGE
const last = Math.min(end - offset, BITS_PER_PAGE)
const range = last - j

if (p) p.setRange(j, last, val)

j = 0
i++
end -= range
start += range
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/bitfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,25 @@ test('set last bits in segment and findFirst', async function (t) {
t.is(b.findFirst(false, 2097151), 2097152)
})

test('bitfield - setRange over multiple pages', async function (t) {
const storage = await createStorage(t)
const b = await Bitfield.open(storage, 0)

b.setRange(32768, 32769, true)

t.is(b.get(0), false)
t.is(b.get(32768), true)
t.is(b.get(32769), false)

b.setRange(0, 32768 * 2, false)
b.setRange(32768, 32768 * 2 + 1, true)

t.is(b.get(0), false)
t.is(b.get(32768), true)
t.is(b.get(32768 * 2), true)
t.is(b.get(32768 * 2 + 1), false)
})

async function createStorage (t, dir) {
if (!dir) dir = await createTempDir(t)

Expand Down

0 comments on commit d198285

Please sign in to comment.