Bug #16269
closedEvery 4 bytes the Integer.sqrt(n) method closes abruptly
Description
If the variable size_byte
(starting from 8) is a multiple of 4 the program fails.
But as an example let's start with 9 (which works well).
size_byte = 9
a = (('F' * (size_byte * 2))).to_i(16)
c = a
a *= a
b = Integer.sqrt(a)
puts "c: #{c}\nb: #{b}\nc == b:#{c == b}"
# This prints:
c: 4722366482869645213695
b: 4722366482869645213695
c == b:true
Which is correct, but if we change the value of the variable size_byte
to 8
then Ruby raises an error.
The same happens with the following numbers 8, 12, 16, 20, 24, 28, 32, 36, etc... The printing error is very long so I leave it at the end.
For the moment, I've solved it this way:
size_byte = 8
a = (('F' * (size_byte * 2))).to_i(16)
c = a
a *= a
b = (Math.sqrt(a).round) - 1
puts "c: #{c}\nb: #{b}\n c == b:#{c == b}"
The problem with this alternative is that I don't know if I run the risk of getting "Infinity" back, I don't want it, I need the integer value.
This bug alone creates many "invisible" problems.
#-------------------------------------------------------------
Bug:
#-------------------------------------------------------------
Assertion Failed: bignum.c:410:bary_small_lshift:0 <= shift && shift < BITSPERDIG
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0003 p:---- s:0015 e:000014 CFUNC :sqrt
c:0002 p:0048 s:0010 E:000730 EVAL jdoodle.rb:7 [FINISH]
c:0001 p:0000 s:0003 E:001900 (none) [FINISH]
-- Ruby level backtrace information ----------------------------------------
jdoodle.rb:7:in `<main>'
jdoodle.rb:7:in `sqrt'
-- C level backtrace information -------------------------------------------
/usr/lib/libruby.so.2.6(0x7fe74cc59c66) [0x7fe74cc59c66]
/usr/lib/libruby.so.2.6(0x7fe74cc5a40c) [0x7fe74cc5a40c]
/usr/lib/libruby.so.2.6(0x7fe74ca8091c) [0x7fe74ca8091c]
/usr/lib/libruby.so.2.6(0x7fe74ca957a4) [0x7fe74ca957a4]
/usr/lib/libruby.so.2.6(0x7fe74ca98bea) [0x7fe74ca98bea]
/usr/lib/libruby.so.2.6(0x7fe74caa35c8) [0x7fe74caa35c8]
/usr/lib/libruby.so.2.6(0x7fe74cc3ac01) [0x7fe74cc3ac01]
/usr/lib/libruby.so.2.6(0x7fe74cc4b02f) [0x7fe74cc4b02f]
/usr/lib/libruby.so.2.6(0x7fe74cc4c0c4) [0x7fe74cc4c0c4]
/usr/lib/libruby.so.2.6(0x7fe74cc43fdb) [0x7fe74cc43fdb]
/usr/lib/libruby.so.2.6(rb_vm_exec+0x1e5) [0x7fe74cc49f45]
/usr/lib/libruby.so.2.6(0x7fe74cafa537) [0x7fe74cafa537]
/usr/lib/libruby.so.2.6(ruby_exec_node+0x1e) [0x7fe74cafc68e]
/usr/lib/libruby.so.2.6(ruby_run_node+0x30) [0x7fe74caff0f0]
ruby(0x5623aebb4081) [0x5623aebb4081]
/usr/lib/libc.so.6(__libc_start_main+0xf3) [0x7fe74c8b5153]
ruby(_start+0x2e) [0x5623aebb40ce]
-- Other runtime information -----------------------------------------------
* Loaded script: jdoodle.rb
* Loaded features:
0 enumerator.so
1 thread.rb
2 rational.so
3 complex.so
4 /usr/lib/ruby/2.6.0/x86_64-linux/enc/encdb.so
5 /usr/lib/ruby/2.6.0/x86_64-linux/enc/trans/transdb.so
6 /usr/lib/ruby/2.6.0/x86_64-linux/rbconfig.rb
7 /usr/lib/ruby/2.6.0/rubygems/compatibility.rb
8 /usr/lib/ruby/2.6.0/rubygems/defaults.rb
9 /usr/lib/ruby/2.6.0/rubygems/deprecate.rb
10 /usr/lib/ruby/2.6.0/rubygems/errors.rb
11 /usr/lib/ruby/2.6.0/rubygems/path_support.rb
12 /usr/lib/ruby/2.6.0/rubygems/version.rb
13 /usr/lib/ruby/2.6.0/rubygems/requirement.rb
14 /usr/lib/ruby/2.6.0/rubygems/platform.rb
15 /usr/lib/ruby/2.6.0/rubygems/basic_specification.rb
16 /usr/lib/ruby/2.6.0/rubygems/stub_specification.rb
17 /usr/lib/ruby/2.6.0/delegate.rb
18 /usr/lib/ruby/2.6.0/uri/rfc2396_parser.rb
19 /usr/lib/ruby/2.6.0/uri/rfc3986_parser.rb
20 /usr/lib/ruby/2.6.0/uri/common.rb
21 /usr/lib/ruby/2.6.0/uri/generic.rb
22 /usr/lib/ruby/2.6.0/uri/file.rb
23 /usr/lib/ruby/2.6.0/uri/ftp.rb
24 /usr/lib/ruby/2.6.0/uri/http.rb
25 /usr/lib/ruby/2.6.0/uri/https.rb
26 /usr/lib/ruby/2.6.0/uri/ldap.rb
27 /usr/lib/ruby/2.6.0/uri/ldaps.rb
28 /usr/lib/ruby/2.6.0/uri/mailto.rb
29 /usr/lib/ruby/2.6.0/uri.rb
30 /usr/lib/ruby/2.6.0/rubygems/specification_policy.rb
31 /usr/lib/ruby/2.6.0/rubygems/util/list.rb
32 /usr/lib/ruby/2.6.0/x86_64-linux/stringio.so
33 /usr/lib/ruby/2.6.0/rubygems/specification.rb
34 /usr/lib/ruby/2.6.0/rubygems/exceptions.rb
35 /usr/lib/ruby/2.6.0/rubygems/util.rb
36 /usr/lib/ruby/2.6.0/rubygems/bundler_version_finder.rb
37 /usr/lib/ruby/2.6.0/rubygems/dependency.rb
38 /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_gem.rb
39 /usr/lib/ruby/2.6.0/monitor.rb
40 /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb
41 /usr/lib/ruby/2.6.0/rubygems/core_ext/kernel_warn.rb
42 /usr/lib/ruby/2.6.0/rubygems.rb
* Process memory map:
5623aebb3000-5623aebb4000 r--p 00000000 00:3e 59 /usr/bin/ruby
5623aebb4000-5623aebb5000 r-xp 00001000 00:3e 59 /usr/bin/ruby
5623aebb5000-5623aebb6000 r--p 00002000 00:3e 59 /usr/bin/ruby
5623aebb6000-5623aebb7000 r--p 00002000 00:3e 59 /usr/bin/ruby
5623aebb7000-5623aebb8000 rw-p 00003000 00:3e 59 /usr/bin/ruby
5623af6f3000-5623af714000 rw-p 00000000 00:00 0 [heap]
5623af714000-5623afaef000 rw-p 00000000 00:00 0 [heap]
7fe749d04000-7fe74a004000 r--s 00000000 00:3e 61 /usr/lib/libruby.so.2.6.5
7fe74a004000-7fe74a007000 r--p 00000000 00:3e 157 /usr/lib/libgcc_s.so.1
7fe74a007000-7fe74a018000 r-xp 00003000 00:3e 157 /usr/lib/libgcc_s.so.1
7fe74a018000-7fe74a01c000 r--p 00014000 00:3e 157 /usr/lib/libgcc_s.so.1
7fe74a01c000-7fe74a01d000 r--p 00017000 00:3e 157 /usr/lib/libgcc_s.so.1
7fe74a01d000-7fe74a01e000 rw-p 00018000 00:3e 157 /usr/lib/libgcc_s.so.1
7fe74a03d000-7fe74c147000 rw-p 00000000 00:00 0
7fe74c147000-7fe74c42d000 r--p 00000000 00:3e 47 /usr/lib/locale/locale-archive
7fe74c42d000-7fe74c42f000 rw-p 00000000 00:00 0
7fe74c42f000-7fe74c43e000 r--p 00000000 00:3e 69 /usr/lib/libm-2.30.so
7fe74c43e000-7fe74c4da000 r-xp 0000f000 00:3e 69 /usr/lib/libm-2.30.so
7fe74c4da000-7fe74c573000 r--p 000ab000 00:3e 69 /usr/lib/libm-2.30.so
7fe74c573000-7fe74c574000 r--p 00143000 00:3e 69 /usr/lib/libm-2.30.so
7fe74c574000-7fe74c575000 rw-p 00144000 00:3e 69 /usr/lib/libm-2.30.so
7fe74c575000-7fe74c576000 r--p 00000000 00:3e 67 /usr/lib/libcrypt-2.30.so
7fe74c576000-7fe74c57c000 r-xp 00001000 00:3e 67 /usr/lib/libcrypt-2.30.so
7fe74c57c000-7fe74c57e000 r--p 00007000 00:3e 67 /usr/lib/libcrypt-2.30.so
7fe74c57e000-7fe74c57f000 ---p 00009000 00:3e 67 /usr/lib/libcrypt-2.30.so
7fe74c57f000-7fe74c580000 r--p 00009000 00:3e 67 /usr/lib/libcrypt-2.30.so
7fe74c580000-7fe74c581000 rw-p 0000a000 00:3e 67 /usr/lib/libcrypt-2.30.so
7fe74c581000-7fe74c5af000 rw-p 00000000 00:00 0
7fe74c5af000-7fe74c5b0000 r--p 00000000 00:3e 52 /usr/lib/libdl-2.30.so
7fe74c5b0000-7fe74c5b1000 r-xp 00001000 00:3e 52 /usr/lib/libdl-2.30.so
7fe74c5b1000-7fe74c5b2000 r--p 00002000 00:3e 52 /usr/lib/libdl-2.30.so
7fe74c5b2000-7fe74c5b3000 r--p 00002000 00:3e 52 /usr/lib/libdl-2.30.so
7fe74c5b3000-7fe74c5b4000 rw-p 00003000 00:3e 52 /usr/lib/libdl-2.30.so
7fe74c5b4000-7fe74c5b6000 rw-p 00000000 00:00 0
7fe74c5b6000-7fe74c5c6000 r--p 00000000 00:3e 65
JDoodle - output Limit reached.
Updated by mame (Yusuke Endoh) almost 5 years ago
- Status changed from Open to Closed
Applied in changeset git|f364564e66d1db1de8e80d669287386595c8bc46.
bignum.c (estimate_initial_sqrt): prevent integer overflow
Integer.sqrt(0xffff_ffff_ffff_ffff ** 2)
caused assertion failure
because of integer overflow. [ruby-core:95453] [Bug #16269]
Updated by mame (Yusuke Endoh) almost 5 years ago
- Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN to 2.5: REQUIRED, 2.6: REQUIRED
A great report. I've fixed the issue. Thank you!
Updated by usa (Usaku NAKAMURA) over 3 years ago
- Backport changed from 2.5: REQUIRED, 2.6: REQUIRED to 2.5: REQUIRED, 2.6: DONE
ruby_2_6 r67894 merged revision(s) f364564e66d1db1de8e80d669287386595c8bc46.