[ruby-cvs:72695] shyouhei:r65595 (trunk): insns.def: avoid integer overflow

shyouhei at ruby-lang.org shyouhei at ruby-lang.org
Wed Nov 7 16:16:50 JST 2018


shyouhei	2018-11-07 16:16:50 +0900 (Wed, 07 Nov 2018)

  New Revision: 65595

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=65595

  Log:
    insns.def: avoid integer overflow
    
    In these expressions `1` is of type `signed int` (cf: ISO/IEC
    9899:1990 section 6.1.3.2). The variable (e.g. `num`) is of type
    `rb_num_t`, which is in fact `unsigned long`.  These two expressions
    then exercises the "usual arithmetic conversions" (cf: ISO/IEC
    9899:1990 section 6.2.1.5) and both eventually become `unsigned long`.
    The two unsigned expressions are then subtracted to generate another
    unsigned integer expression (cf: ISO/IEC 9899:1990 section 6.3.6).
    This is where integer overflows can occur.  OTOH the left hand side of
    the assignments are `rb_snum_t` which is `signed long`.  The
    assignments exercise the "implicit conversion" of "an unsigned integer
    is converted to its corresponding signed integer" case (cf: ISO/IEC
    9899:1990 section 6.2.1.2), which is "implementation-defined" (read:
    not portable).
    
    Casts are the proper way to avoid this problem.  Because all
    expressions are converted to some integer types before any binary
    operations are performed, the assignments now have fully defined
    behaviour.  These values can never exceed LONG_MAX so the casts must
    not lose any information.
    
    See also: https://travis-ci.org/ruby/ruby/jobs/451726874#L4357

  Modified files:
    trunk/insns.def


More information about the ruby-cvs mailing list