Bug 19338 – std.bitmanip.BitArray.count gives segfault for empy BitArray

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-10-28T13:11:21Z
Last change time
2018-10-30T21:08:48Z
Keywords
bootcamp
Assigned to
No Owner
Creator
Richard Palme

Comments

Comment #0 by richardpalme5b — 2018-10-28T13:11:21Z
The following code gives a segmentation fault: -------------------------------------------------------------------- import std.bitmanip; void main() { BitArray b; auto count = b.count; } --------------------------------------------------------------------
Comment #1 by richardpalme5b — 2018-10-28T13:31:19Z
I forgot to mention: the expected return value of b.count for an empty BitArray b would be 0, similar to how b.length returns 0 for empty BitArray b.
Comment #2 by richardpalme5b — 2018-10-28T13:52:36Z
The phobos implementation of this function is: -------------------------------------------------------------------- size_t count() { size_t bitCount; foreach (i; 0 .. fullWords) bitCount += countBitsSet(_ptr[i]); bitCount += countBitsSet(_ptr[fullWords] & endMask); return bitCount; } -------------------------------------------------------------------- My guess would be that for an empty BitArray, _ptr is null. count() then tries to access _ptr[fullWords], which leads to the segfault.
Comment #3 by github-bugzilla — 2018-10-30T21:08:47Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/9d7f453f1f960c0cc0e4e67e27ef63c5a0825c16 fixed issue 19338 https://github.com/dlang/phobos/commit/9fcf1f1b77a3a10f5369466cce58e8af7dc8ebcf Merge pull request #6742 from RichardPalme/fix-bitmanip-count fixed issue 19338 merged-on-behalf-of: Nathan Sashihara <[email protected]>