Repeat icon indicating copy to clipboard operation
Repeat copied to clipboard

Overflow Int in Repeater.Interval.Internal.second

Open optimusbits opened this issue 4 years ago • 1 comments

On iPhone 5 with 32 bits processor it is possible to make int overflow. The underlay seconds:

/// Repeat interval
	public enum Interval {
              case seconds(_: Double)

		internal var value: DispatchTimeInterval {
			switch self {
			case .seconds(let value):			return .milliseconds(Int( Double(value) * Double(1000)))

which can exceeding the scope of int

optimusbits avatar May 26 '20 10:05 optimusbits

My proposal of solving this issue:

extension Repeater.Interval {
    
    /// safeSeconds fix issue with int range overflow in Repeater.Interval.Interval.Seconds.
    /// It should be used instead of Repeater.Interval.Interval.Seconds
    static func safeSeconds(_ seconds: Double) -> Self {
        /// Repeater uses milliseconds as basic unit. It converts  seconds to milliseconds
        let secondsInMiliseconds = Double(seconds) * Double(1000)
        
        /// check if seconds expressed in miliseconds overflows int range
        if secondsInMiliseconds.isInIntRange {
            return .seconds(seconds)
        } else {
            let overflowValue = seconds >= 0 ? Int.max : Int.min
            return .milliseconds(overflowValue)
        }
    }
}

optimusbits avatar May 26 '20 10:05 optimusbits