Skip to content

> 32 bit length incompatibility #5

@TomNaessens

Description

@TomNaessens

I'm working with some large bitsets and I'm experiencing some issues:

ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [x86_64-darwin21]

Creating a bitset larger than 2**31 fails:

[1]dev(main)> Bitset.new(2**30); # Is fine
[2]dev(main)> Bitset.new(2**31);
RangeError: integer 2147483648 too big to convert to `int'
from (pry):2:in `initialize'

The cause is probably the NUM2INT used here: https://github.com/ericboesch/bitset/blob/master/ext/bitset/bitset.c#L73

Working around it with the if branch of that method, passing an array gets interesting:

[10]dev(main)> b = Bitset.new([nil] * (2**30)); # Is fine
[13]dev(main)> b.size
=> 1073741824

[14]dev(main)> b = Bitset.new([nil] * (2**31)); # Does not crash!
[16]dev(main)> b.size # The size gets signed though, probaby due to https://github.com/ericboesch/bitset/blob/master/ext/bitset/bitset.c#L80
=> -2147483648

[17]dev(main)> b.set(1); # Indexes get set out of bounds
IndexError: Index out of bounds
from (pry):17:in `set`

[18]dev(main) > b # printing it segfaults

=> [redacted]/1.2.0/lib/bitset.rb:19: [BUG] Segmentation fault at 0xffffffff80081270
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [x86_64-darwin21]

-- Crash Report log information --------------------------------------------
   See Crash Report log file under the one of following:
     * ~/Library/Logs/DiagnosticReports
     * /Library/Logs/DiagnosticReports
   for more details.
Don't forget to include the above Crash Report log file in bug reports.

-- Control frame information -----------------------------------------------
c:0053 p:---- s:0296 e:000295 CFUNC  :to_s
c:0052 p:0019 s:0292 e:000289 METHOD /[redacted]/ruby/2.7.6/lib/ruby/gems/2.7.0/gems/bitset-1.2.0/lib/bitset.rb:19

I guess the NUM2INT and INT2NUM's are the cause here. My CRuby is limited, but switching to NUM2ULL and ULL2NUM or SIZET2NUM and NUM2SIZET (and changing int's to size_t's) could perhaps work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions