danielpclark / rutie

“The Tie Between Ruby and Rust.”
MIT License
940 stars 62 forks source link

Integer::new(std::i64::MIN) panics #113

Closed turboladen closed 4 years ago

turboladen commented 4 years ago

I feel like I must be overlooking something, but I ran across this while writing some tests. I tried whittling it down to find the breaking point; some of these panic (signal: 11, SIGSEGV: invalid memory reference); some don't:

// Panic
let i = std::i64::MIN;  // -9223372036854775808
let _ = Integer::new(i);

// Panic
let i = std::i64::MIN + std::u32::MAX as i64; // -9223372032559808513
let _ = Integer::new(i);

// Panic
let i = std::i64::MAX;  // 9223372036854775807
let _ = Integer::new(i);

// Panic
let i = std::i64::MAX - 1;
let _ = Integer::new(i);

// Ok
let i = (std::i32::MAX as i64) + 1; // 2147483648
let _ = Integer::new(i);

// Panic
let i = (std::i32::MIN as i64).pow(2); // 4611686018427387904
let _ = Integer::new(i);

// Ok! Looks like this is a threshold on the positive end.
let i = (std::i32::MIN as i64).pow(2) -  1;
let _ = Integer::new(i);

// Ok!
let i = (std::i32::MIN as i64).pow(2) * -1; // -4611686018427387904
let _ = Integer::new(i);

// Panic; looks like the -1 here is where it fails, at least on the negative end
let i = (std::i32::MIN as i64).pow(2) * -1 - 1;  // -4611686018427387905
let _ = Integer::new(i);

Ruby version: ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18] Rust version: rustc 1.39.0 (4560ea788 2019-11-04)

If this is a Ruby limitation (only handling values in between 32- and 64-bit int types), I'm not sure what a reasonable solution would be, but it at least seems worth documenting.

danielpclark commented 4 years ago

I've tested each of these on Linux and they all work without issue. I've added tests to the main code base to see how they'll do across operating systems.

danielpclark commented 4 years ago

The tests pass CI for Mac. I is likely a system architecture and customized build of Ruby issue.

turboladen commented 4 years ago

FWIW, it was a standard install of Ruby, using chruby, nothing fancy.

danielpclark commented 4 years ago

@turboladen I've noticed with rbenv on Mac it tends to have a statically linked Ruby built rather than a dynamically linked one. That causes its own issues. I've not heard any other reports with chruby, and I haven't tried it yet.

That may be something I should add to the test suite… using different Ruby version managers.

turboladen commented 4 years ago

Oh ya, good point—I usually forget that (chruby behaves the same) and have to reinstall Ruby with the —enable-shared (I think that’s the one at least) flag. ...which I suppose negates my “nothing fancy” comment, since it could be picking up anything weird from my env.