Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow Int in Repeater.Interval.Internal.second #48

Open
optimusbits opened this issue May 26, 2020 · 1 comment
Open

Overflow Int in Repeater.Interval.Internal.second #48

optimusbits opened this issue May 26, 2020 · 1 comment

Comments

@optimusbits
Copy link

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
Copy link
Author

optimusbits commented May 26, 2020

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)
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant