File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed
Sources/FoundationEssentials/Decimal
Tests/FoundationEssentialsTests Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -949,7 +949,7 @@ extension Decimal {
949
949
950
950
// D1: Normalize
951
951
// Calculate d such that `d*highest_dight_of_divisor >= b/2 (0x8000)
952
- let d = ( 1 << 16 ) / UInt32( divisor [ divisor. count - 1 ] + 1 )
952
+ let d : UInt32 = ( 1 << 16 ) / ( UInt32 ( divisor [ divisor. count - 1 ] ) + 1 )
953
953
// This is to make the whole algorithm work and
954
954
// (dividend * d) / (divisor * d) == dividend / divisor
955
955
var normalizedDividend = try self . _integerMultiplyByShort (
Original file line number Diff line number Diff line change @@ -613,6 +613,32 @@ final class DecimalTests : XCTestCase {
613
613
XCTAssertTrue ( Decimal . _compare ( lhs: expected, rhs: result) == . orderedSame)
614
614
}
615
615
616
+ func testCrashingDivision( ) throws {
617
+ // This test makes sure the following division
618
+ // does not crash
619
+ let first : Decimal = Decimal ( 1147858867 )
620
+ let second : Decimal = Decimal ( 4294967295 )
621
+ let result = first / second
622
+ let expected : Decimal = Decimal (
623
+ _exponent: - 38 ,
624
+ _length: 8 ,
625
+ _isNegative: 0 ,
626
+ _isCompact: 1 ,
627
+ _reserved: 0 ,
628
+ _mantissa: (
629
+ 58076 ,
630
+ 13229 ,
631
+ 12316 ,
632
+ 25502 ,
633
+ 15252 ,
634
+ 32996 ,
635
+ 11611 ,
636
+ 5147
637
+ )
638
+ )
639
+ XCTAssertEqual ( result, expected)
640
+ }
641
+
616
642
func testPower( ) throws {
617
643
var a = Decimal ( 1234 )
618
644
var result = try a. _power ( exponent: 0 , roundingMode: . plain)
@@ -1272,5 +1298,4 @@ final class DecimalTests : XCTestCase {
1272
1298
XCTAssertEqual ( length, 3 )
1273
1299
}
1274
1300
#endif
1275
-
1276
1301
}
You can’t perform that action at this time.
0 commit comments