Merge pull request #1268 from pacu/dbz-mitigation

This commit is contained in:
Kris Nuttycombe 2023-09-13 16:43:02 -06:00 committed by GitHub
commit 0abe7f8cdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -12,8 +12,11 @@ struct ScanProgress: Equatable {
let denominator: UInt64
func progress() throws -> Float {
// division by 0 is not a concern here because `ZcashRustBackend.getScanProgress() -> ScanProgress?`
// handles the 0 and returns nil rather than returning nil progress or 0 value here
guard denominator != 0 else {
// this shouldn't happen but if it does, we need to get notified by clients and work on a fix
throw ZcashError.rustScanProgressOutOfRange("\(numerator)/\(denominator)")
}
let value = Float(numerator) / Float(denominator)
// this shouldn't happen but if it does, we need to get notified by clients and work on a fix

View File

@ -157,4 +157,14 @@ class ZcashRustBackendTests: XCTestCase {
XCTAssertEqual(metadata?.networkType, .mainnet)
XCTAssertEqual(metadata?.addressType, .sapling)
}
func testScanProgressThrowsOnWrongValues() {
// Assert that throws on Zero denominator
XCTAssertThrowsError(try ScanProgress(numerator: 0, denominator: 0).progress())
// Assert that throws on numerator > denominator
XCTAssertThrowsError(try ScanProgress(numerator: 23, denominator: 2).progress())
XCTAssertNoThrow(try ScanProgress(numerator: 3, denominator: 4).progress())
}
}