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

Fix a division-by-zero crash in the VPN code #823

Merged

Conversation

diegoreymendez
Copy link
Contributor

@diegoreymendez diegoreymendez commented May 15, 2024

Task/Issue URL: https://app.asana.com/0/1203137811378537/1207299387361586/f

iOS PR: duckduckgo/iOS#2862
macOS PR: duckduckgo/macos-browser#2785
What kind of version bump will this require?: Patch

Description

Fixes a crash caused by a division by zero.

Testing

See platform-specific PRs for testing instructions.


Internal references:

Software Engineering Expectations
Technical Design Template

@diegoreymendez diegoreymendez changed the base branch from main to release/144.0.7-3 May 15, 2024 16:31
Comment on lines +118 to +119
rx = Double(newer.rxBytes - older.rxBytes) / deltaSeconds
tx = Double(newer.txBytes - older.txBytes) / deltaSeconds
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't want to divide by zero.

@diegoreymendez diegoreymendez changed the title Fixes a crasher Fix a division-by-zero crash in the VPN code May 15, 2024
@diegoreymendez diegoreymendez marked this pull request as ready for review May 15, 2024 16:41
@diegoreymendez diegoreymendez requested a review from mallexxx May 15, 2024 16:42
Copy link
Collaborator

@mallexxx mallexxx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably that should work, unless you do a horse kick test 😀

        let d = Date()
        let (rx, tx) = NetworkProtectionConnectionBandwidthAnalyzer.bytesPerSecond(newer: .init(rxBytes: 9_999_999_999_999, txBytes: 9_999_999_999_999, date: d.addingTimeInterval(0.0000001)),
                                                                                   older: .init(rxBytes: 0, txBytes: 0, date: d))

        let rxThreshold = 100 * 1024 // 100k
        let txThreshold = 100 * 1024 // 100k

        let uint_rx = UInt64(rx) // this will overflow
        let uint_tx = UInt64(tx)

But probably this condition is not something that has really happened, because lesser numbers work. Probably the crash was really about the division by zero leading to a NaN to UInt64 conversion.
Anyways, consider adding couple of small sanity unit tests

@diegoreymendez
Copy link
Contributor Author

I made the method static and added some tests to validate all is good and prevent regressions in the future. I also changed the line that was suffering in readability, I agree. Thanks for the suggestions.

The only suggestion I'm skipping is the larger inline code suggestion in your comment because IMO it favors a lower line count in detriment of readability and maintainability.

I want to make sure the reader can skim through the code and understand quickly, and I think this line isn't good to that end:

let (rx, tx) = NetworkProtectionConnectionBandwidthAnalyzer.bytesPerSecond(newer: .init(rxBytes: 9_999_999_999_999, txBytes: 9_999_999_999_999, date: d.addingTimeInterval(0.0000001)),
                                                                                   older: .init(rxBytes: 0, txBytes: 0, date: d))

@diegoreymendez diegoreymendez merged commit ada5f68 into release/144.0.7-3 May 17, 2024
7 checks passed
@diegoreymendez diegoreymendez deleted the diego/fix-crash-division-by-zero-in-vpn branch May 17, 2024 09:27
@diegoreymendez diegoreymendez self-assigned this May 17, 2024
diegoreymendez added a commit to duckduckgo/iOS that referenced this pull request May 17, 2024
diegoreymendez added a commit to duckduckgo/macos-browser that referenced this pull request May 17, 2024
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

Successfully merging this pull request may close these issues.

2 participants