Files
Mahdi ramezaan zaadeandAlexander Medvednikov e55f74fc35 bitfield: clamp a negative start in slice() to 0 (#30232)
* bitfield: clamp a negative start in slice() to 0

slice() clamped `end` to the size and swapped start/end, but never
checked start < 0, so the shift amount `start % slot_size` went
negative and the mask produced an all-zero result instead of data:
slice(-3, 5) of '1010101010' returned '00000000' instead of '10101'.

Clamp start to 0, between the end clamp and the start > end clamp, so
a negative end still yields the empty result it already did. Every
other accessor in the module rejects a negative index; slice's own
`// or panic?` comments declare clamping intent, so this keeps that
style instead of panicking.

Fixes #30149

* bitfield: document negative slice bound clamping

---------

Co-authored-by: Alexander Medvednikov <alexander@medvednikov.com>
2026-10-12 22:02:17 +03:00
..

Description

bitfield is a module for manipulating arrays of bits, i.e. a series of zeroes and ones spread across an array of storage units.

BitField Structure

Bit arrays are stored in data structures called BitField. The structure is 'opaque', i.e. its internals are not available to the end user. This module provides API (functions and methods) for accessing and modifying bit arrays.

BitField.slice(start, end) clamps a negative start to zero. An end beyond the bitfield size is clamped to the size, and reversed bounds produce an empty bitfield.