• J
    Add `core::num::wrapping` and fix overflow errors. · 1246d406
    James Miller 提交于
    Many of the core rust libraries have places that rely on integer
    wrapping behaviour. These places have been altered to use the wrapping_*
    methods:
    
     * core::hash::sip - A number of macros
     * core::str - The `maximal_suffix` method in `TwoWaySearcher`
     * rustc::util::nodemap - Implementation of FnvHash
     * rustc_back::sha2 - A number of macros and other places
     * rand::isaac - Isaac64Rng, changed to use the Wrapping helper type
    
    Some places had "benign" underflow. This is when underflow or overflow
    occurs, but the unspecified value is not used due to other conditions.
    
     * collections::bit::Bitv - underflow when `self.nbits` is zero.
     * collections::hash::{map,table} - Underflow when searching an empty
       table. Did cause undefined behaviour in this case due to an
       out-of-bounds ptr::offset based on the underflowed index. However the
       resulting pointers would never be read from.
     * syntax::ext::deriving::encodable - Underflow when calculating the
       index of the last field in a variant with no fields.
    
    These cases were altered to avoid the underflow, often by moving the
    underflowing operation to a place where underflow could not happen.
    
    There was one case that relied on the fact that unsigned arithmetic and
    two's complement arithmetic are identical with wrapping semantics. This
    was changed to use the wrapping_* methods.
    
    Finally, the calculation of variant discriminants could overflow if the
    preceeding discriminant was `U64_MAX`. The logic in `rustc::middle::ty`
    for this was altered to avoid the overflow completely, while the
    remaining places were changed to use wrapping methods. This is because
    `rustc::middle::ty::enum_variants` now throws an error when the
    calculated discriminant value overflows a `u64`.
    
    This behaviour can be triggered by the following code:
    
    ```
    enum Foo {
      A = U64_MAX,
      B
    }
    ```
    
    This commit also implements the remaining integer operators for
    Wrapped<T>.
    1246d406
decoder.rs 55.5 KB