D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 19338 - std.bitmanip.BitArray.count gives segfault for empy BitArray
Summary: std.bitmanip.BitArray.count gives segfault for empy BitArray
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: bootcamp
Depends on:
Blocks:
 
Reported: 2018-10-28 13:11 UTC by Richard Palme
Modified: 2018-10-30 21:08 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Richard Palme 2018-10-28 13:11:21 UTC
The following code gives a segmentation fault:

--------------------------------------------------------------------
import std.bitmanip;

void main() {
    BitArray b;
    auto count = b.count;
}
--------------------------------------------------------------------
Comment 1 Richard Palme 2018-10-28 13:31:19 UTC
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 Richard Palme 2018-10-28 13:52:36 UTC
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 github-bugzilla 2018-10-30 21:08:47 UTC
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>