[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


    insns.def: avoid integer overflow
    In these expressions `1` is of type `signed int` (cf: ISO/IEC
    9899:1990 section 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 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, 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:

More information about the ruby-cvs mailing list