JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-6612

some problems with JRuby seeming to not detect Java Long arithmetic overflows

    Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.6.6
    • Fix Version/s: JRuby 1.7.0.pre1
    • Component/s: Core Classes/Modules
    • Labels:
      None
    • Environment:
      Laptop running Microsoft Windows Vista and a netbook running Microsoft Windows 7
    • Number of attachments :
      1

      Description

      There seem to be some bugs where the Java code underlying JRuby seems to not detect Long arithmetic overflows.

      For example, on my PCs running this code in JRuby:

      nn = -(2**63)-1
      3.times {|i| i += nn; puts "# i: #{i}; i*-1: #{i*-1}; -1*i: #{-1*i}" }
      

      results in this output:

      # i: -9223372036854775809; i*-1: 9223372036854775809; -1*i: 9223372036854775809
      # i: -9223372036854775808; i*-1: 9223372036854775808; -1*i: -9223372036854775808
      # i: -9223372036854775807; i*-1: 9223372036854775807; -1*i: 9223372036854775807
      

      I seem to have some problems with Fixnum multiplication (as above),
      and with Fixnum/Bignum Range#each and Range#each_with_index.

      There are more examples and information in the attached JRuby file.

      My apologies if I am making a mistake, or if these issues have been raised before: I searched jira.codehaus.org using "JRuby overflow", but didn't find anything that seemed to cover the examples in the attached file.

      I wasn't sure what priority to give this: if I'm correct that these are bugs with Fixnum (etc) then they would seem to be "major", but if no-one else has noticed these before then in practice there hasn't so far been a problem.

      In the attached file I've put a possible patch for one occurrence of the Fixnum multiply overflow problem, but there may be other places where patches are needed, and there is also the Fixnum/Bignum range problem: I'm afraid I haven't looked at all at the JRuby Java code for that.

      I would be happy to give more details and/or answer any questions.

        Activity

        Hide
        Colin Bartlett added a comment -

        Update: I've just downloaded
        http://jruby.org.s3.amazonaws.com/downloads/1.6.7/jruby-bin-1.6.7.zip
        and the issues in my original post about JRuby 1.6.6 seem to also be in 1.6.7.

        I've also done a better search, and found these, which may be relevant:
        http://jira.codehaus.org/browse/JRUBY-3566

        Show
        Colin Bartlett added a comment - Update: I've just downloaded http://jruby.org.s3.amazonaws.com/downloads/1.6.7/jruby-bin-1.6.7.zip and the issues in my original post about JRuby 1.6.6 seem to also be in 1.6.7. I've also done a better search, and found these, which may be relevant: http://jira.codehaus.org/browse/JRUBY-3566 bug in multiplication fixed in JRuby 1.3RC1 http://jira.codehaus.org/browse/JRUBY-6307 Powering operation of Integer sometimes gets a wrong calculation when 1.9 mode fixed in JRuby 1.6.6, JRuby 1.7 http://jira.codehaus.org/browse/JRUBY-6518 Array#combination returns unexpected value when argument number too big fixed in JRuby 1.6.8
        Hide
        Hiro Asari added a comment -

        Incorrect integer arithmetic is not good. Confirmed on master.

        Show
        Hiro Asari added a comment - Incorrect integer arithmetic is not good. Confirmed on master.
        Hide
        Hiro Asari added a comment -

        Fixed in 03f26a8.

        Show
        Hiro Asari added a comment - Fixed in 03f26a8.
        Hide
        Charles Oliver Nutter added a comment -

        Additional fixes:

        commit ee963e52200617634eb11d9ffbf984b956f7fb21
        Author: Charles Oliver Nutter <headius@headius.com>
        Date:   Thu Nov 8 13:44:42 2012 -0600
        
            Fix JRUBY-6612, JRUBY-6777, JRUBY-6778, JRUBY-6779, JRUBY-6790
            
            [JRUBY-6612] some problems with JRuby seeming to not detect Java Long arithmetic overflows
            [JRUBY-6777] RubyFixnum.java - two methods fail to detect some long integer overflows
            [JRUBY-6778] Possible long integer overflow bug in Integer#succ in RubyInteger.java
            [JRUBY-6779] Strange behaviour of some Integer Ranges with Range#each - maybe an integer overflow problem?
            [JRUBY-6790] Possible long integer overflow in fixnumStep in RubyNumeric.java
            
            Patches by Colin Bartlett. Thank you!
        
        :100644 100644 93f83c8... 109b856... M	src/org/jruby/RubyInteger.java
        :100644 100644 b631a40... 7ecf098... M	src/org/jruby/RubyNumeric.java
        :100644 100644 3ed0b55... d15c2ed... M	src/org/jruby/RubyRange.java
        
        Show
        Charles Oliver Nutter added a comment - Additional fixes: commit ee963e52200617634eb11d9ffbf984b956f7fb21 Author: Charles Oliver Nutter <headius@headius.com> Date: Thu Nov 8 13:44:42 2012 -0600 Fix JRUBY-6612, JRUBY-6777, JRUBY-6778, JRUBY-6779, JRUBY-6790 [JRUBY-6612] some problems with JRuby seeming to not detect Java Long arithmetic overflows [JRUBY-6777] RubyFixnum.java - two methods fail to detect some long integer overflows [JRUBY-6778] Possible long integer overflow bug in Integer#succ in RubyInteger.java [JRUBY-6779] Strange behaviour of some Integer Ranges with Range#each - maybe an integer overflow problem? [JRUBY-6790] Possible long integer overflow in fixnumStep in RubyNumeric.java Patches by Colin Bartlett. Thank you! :100644 100644 93f83c8... 109b856... M src/org/jruby/RubyInteger.java :100644 100644 b631a40... 7ecf098... M src/org/jruby/RubyNumeric.java :100644 100644 3ed0b55... d15c2ed... M src/org/jruby/RubyRange.java

          People

          • Assignee:
            Hiro Asari
            Reporter:
            Colin Bartlett
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: