Bug 10948 – BitArray.opEquals is invalid

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2013-09-01T21:33:00Z
Last change time
2015-06-09T05:14:42Z
Keywords
pull
Assigned to
nobody
Creator
nikolay.v.belov

Attachments

IDFilenameSummaryContent-TypeSize
1245diffdiff with git masterapplication/octet-stream1315

Comments

Comment #0 by nikolay.v.belov — 2013-09-01T21:33:36Z
Created attachment 1245 diff with git master Invalid calculation n value: - auto n = this.length / bitsPerSizeT; + auto n = (this.length + (bitsPerSizeT - 1)) / bitsPerSizeT; Test: // 0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7 static bool[] bf = [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; static bool[] bg = [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]; BitArray f; f.init(bf); BitArray g; g.init(bg); assert(f != g);
Comment #1 by nikolay.v.belov — 2013-09-05T23:52:33Z
Sorry. It is hasty fix. Real problem - it is size_t mask = (1 << n) - 1; On 64 bit system size_t has 64 bits. Byt 1 on this expression it is not size_t, it it 32 bit integer. And (1 << 32) return 0; Good fix is size_t mask = (cast(size_t)1 << n) - 1; I see this problem on other methods: - opCmp: size_t mask = cast(size_t)(1 << j); must be replaced by size_t mask = (cast(size_t)1 << j);
Comment #2 by andrej.mitrovich — 2014-04-24T19:34:54Z
Tagging as x86_64. Can we get a pull request for this? It would be very welcome!
Comment #3 by jblume — 2014-05-26T19:01:51Z
I have gone ahead and packaged the fix in a PR, attributing nbelov: https://github.com/D-Programming-Language/phobos/pull/2204
Comment #4 by github-bugzilla — 2014-05-31T10:37:12Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/9211f1aad1bfdb306b1abcd67b278fda9cda78d2 fix Issue 10948 - BitArray.opEquals is invalid https://issues.dlang.org/show_bug.cgi?id=10948 Implemented the fix as proposed by nbelov on Bugzilla. Included are unit tests which would have triggered this bug and also fixed it in opCmp. The issue was that the 32 bit expression "(1 << n)" was treated as being of size_t, which lead to incorrect bit patterns on 64 bit environments. https://github.com/D-Programming-Language/phobos/commit/48afca0253bf51068bd64d26db15152e6a663204 Merge pull request #2204 from jblume/issue-10948 fix Issue 10948 - BitArray.opEquals is invalid