فهرست منبع

Backport upstream fix from https://github.com/attaswift/BigInt/commit/d2d29c1225d96d66c331712cce4eb4510322a243

Marcin Krzyzanowski 2 سال پیش
والد
کامیت
bce847559b
1فایلهای تغییر یافته به همراه10 افزوده شده و 6 حذف شده
  1. 10 6
      Sources/CryptoSwift/CS_BigInt/String Conversion.swift

+ 10 - 6
Sources/CryptoSwift/CS_BigInt/String Conversion.swift

@@ -19,11 +19,14 @@ extension CS.BigUInt {
         var overflow = false
         var count = 0
         while !overflow {
-            let (p, o) = power.multipliedReportingOverflow(by: Word(radix))
-            overflow = o
-            if !o || p == 0 {
+            let (high,low) = power.multipliedFullWidth(by: Word(radix))
+            if high > 0 {
+              overflow = true
+            }
+
+            if !overflow || (high == 1 && low == 0) {
                 count += 1
-                power = p
+                power = low
             }
         }
         return (count, power)
@@ -37,7 +40,8 @@ extension CS.BigUInt {
     /// - Parameter `radix`: The base of the number system to use, or 10 if unspecified.
     /// - Returns: The integer represented by `text`, or nil if `text` contains a character that does not represent a numeral in `radix`.
     public init?<S: StringProtocol>(_ text: S, radix: Int = 10) {
-        precondition(radix > 1)
+        precondition(radix > 1 && radix < 36)
+        guard !text.isEmpty else { return nil }
         let (charsPerWord, power) = CS.BigUInt.charsPerWord(forRadix: radix)
 
         var words: [Word] = []
@@ -97,7 +101,7 @@ extension CS.BigInt {
         }
         guard let m = magnitude else { return nil }
         self.magnitude = m
-        self.sign = sign
+        self.sign = m.isZero ? .plus : sign
     }
 }