Bug 1557655 - Fix size_t/uint64_t mismatch for StringToBigInt on 32-bit. r=jwalden, a=RyanVM
authorAndy Wingo <wingo@igalia.com>
Tue, 25 Jun 2019 17:36:07 +0000
changeset 537166 12c9d3b85c14cefe91e938b72c63e79f4f722e5d
parent 537165 49b397671d0f6ed01b98acfc89493b2c70a849de
child 537167 1df87d06a827fb2989f3467da5ce59e155b34d18
push id2082
push userffxbld-merge
push dateMon, 01 Jul 2019 08:34:18 +0000
treeherdermozilla-release@2fb19d0466d2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwalden, RyanVM
bugs1557655
milestone68.0
Bug 1557655 - Fix size_t/uint64_t mismatch for StringToBigInt on 32-bit. r=jwalden, a=RyanVM Differential Revision: https://phabricator.services.mozilla.com/D35022
js/src/vm/BigIntType.cpp
--- a/js/src/vm/BigIntType.cpp
+++ b/js/src/vm/BigIntType.cpp
@@ -1406,22 +1406,26 @@ BigInt* BigInt::destructivelyTrimHighZer
 //   N ≥ ⌈charcount * bitsPerChar / (DigitBits * bitsPerCharTableMultiplier)⌉
 //
 // Note that `N` is computed even more conservatively here because `bitsPerChar`
 // is rounded up.
 bool BigInt::calculateMaximumDigitsRequired(JSContext* cx, uint8_t radix,
                                             size_t charcount, size_t* result) {
   MOZ_ASSERT(2 <= radix && radix <= 36);
 
-  size_t bitsPerChar = maxBitsPerCharTable[radix];
+  uint8_t bitsPerChar = maxBitsPerCharTable[radix];
 
   MOZ_ASSERT(charcount > 0);
-  MOZ_ASSERT(charcount <= std::numeric_limits<size_t>::max() / bitsPerChar);
-  uint64_t n =
-      CeilDiv(charcount * bitsPerChar, DigitBits * bitsPerCharTableMultiplier);
+  MOZ_ASSERT(charcount <= std::numeric_limits<uint64_t>::max() / bitsPerChar);
+  static_assert(
+      MaxDigitLength < std::numeric_limits<size_t>::max(),
+      "can't safely cast calculateMaximumDigitsRequired result to size_t");
+
+  uint64_t n = CeilDiv(static_cast<uint64_t>(charcount) * bitsPerChar,
+                       DigitBits * bitsPerCharTableMultiplier);
   if (n > MaxDigitLength) {
     ReportOutOfMemory(cx);
     return false;
   }
 
   *result = n;
   return true;
 }