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