The following code gives a segmentation fault: -------------------------------------------------------------------- import std.bitmanip; void main() { BitArray b; auto count = b.count; } --------------------------------------------------------------------
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.
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.
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 <n8sh@users.noreply.github.com>