Bug 1540357 - Fix implicit int truncation in dom/canvas's gfx/* includes. r=jrmuizel
authorJeff Gilbert <jgilbert@mozilla.com>
Tue, 02 Apr 2019 17:00:47 +0000
changeset 467622 f4debce94b0003c8445be2eeae913790f6f65df1
parent 467621 54a2f08bcf48725ae4de58481c61b07011601d90
child 467623 c941cc70ee3e8a3f64c6c799535f63d48793c5d6
push id35806
push userrgurzau@mozilla.com
push dateWed, 03 Apr 2019 04:07:39 +0000
treeherdermozilla-central@45808ab18609 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjrmuizel
bugs1540357
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1540357 - Fix implicit int truncation in dom/canvas's gfx/* includes. r=jrmuizel Depends on D25497 Differential Revision: https://phabricator.services.mozilla.com/D25498
gfx/src/FilterSupport.h
gfx/src/FontPropertyTypes.h
gfx/thebes/gfxFont.h
gfx/thebes/gfxFontUtils.h
--- a/gfx/src/FilterSupport.h
+++ b/gfx/src/FilterSupport.h
@@ -388,17 +388,18 @@ class ColorModel {
       : mColorSpace(ColorSpace::SRGB), mAlphaModel(AlphaModel::Premultiplied) {}
   bool operator==(const ColorModel& aOther) const {
     return mColorSpace == aOther.mColorSpace &&
            mAlphaModel == aOther.mAlphaModel;
   }
 
   // Used to index FilterCachedColorModels::mFilterForColorModel.
   uint8_t ToIndex() const {
-    return (uint8_t(mColorSpace) << 1) + uint8_t(mAlphaModel);
+    return static_cast<uint8_t>(static_cast<uint8_t>(mColorSpace) << 1) |
+           static_cast<uint8_t>(mAlphaModel);
   }
 
   ColorSpace mColorSpace;
   AlphaModel mAlphaModel;
 };
 
 /**
  * A data structure to carry attributes for a given primitive that's part of a
--- a/gfx/src/FontPropertyTypes.h
+++ b/gfx/src/FontPropertyTypes.h
@@ -92,17 +92,18 @@ class FontPropertyValue {
 
  protected:
   // Construct from a floating-point or integer value, checking that it is
   // within the allowed range and converting to fixed-point representation.
   explicit constexpr FontPropertyValue(float aValue)
       : mValue(std::round(aValue * kScale)) {
     MOZ_ASSERT(aValue >= kMin && aValue <= kMax);
   }
-  explicit constexpr FontPropertyValue(int aValue) : mValue(aValue * kScale) {
+  explicit constexpr FontPropertyValue(int aValue)
+      : mValue(static_cast<InternalType>(aValue * kScale)) {
     MOZ_ASSERT(aValue >= Min && aValue <= Max);
   }
 
   // Construct directly from a fixed-point value of type T, with no check;
   // note that there may be special "flag" values that are outside the normal
   // min/max range (e.g. for font-style:italic, distinct from oblique angle).
   explicit constexpr FontPropertyValue(InternalType aValue) : mValue(aValue) {}
 
--- a/gfx/thebes/gfxFont.h
+++ b/gfx/thebes/gfxFont.h
@@ -681,17 +681,17 @@ MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(gf
  * textruns in their entirety rather than using cached words, because there may
  * be layout features that depend on the inter-word spaces).
  */
 class gfxShapedText {
  public:
   typedef mozilla::unicode::Script Script;
 
   gfxShapedText(uint32_t aLength, mozilla::gfx::ShapedTextFlags aFlags,
-                int32_t aAppUnitsPerDevUnit)
+                uint16_t aAppUnitsPerDevUnit)
       : mLength(aLength),
         mFlags(aFlags),
         mAppUnitsPerDevUnit(aAppUnitsPerDevUnit) {}
 
   virtual ~gfxShapedText() {}
 
   /**
    * This class records the information associated with a character in the
@@ -1197,17 +1197,17 @@ class gfxShapedWord final : public gfxSh
   //
   // Returns null on allocation failure (does NOT use infallible alloc)
   // so caller must check for success.
   //
   // This does NOT perform shaping, so the returned word contains no
   // glyph data; the caller must call gfxFont::ShapeText() with appropriate
   // parameters to set up the glyphs.
   static gfxShapedWord* Create(const uint8_t* aText, uint32_t aLength,
-                               Script aRunScript, int32_t aAppUnitsPerDevUnit,
+                               Script aRunScript, uint16_t aAppUnitsPerDevUnit,
                                mozilla::gfx::ShapedTextFlags aFlags,
                                gfxFontShaper::RoundingFlags aRounding) {
     NS_ASSERTION(aLength <= gfxPlatform::GetPlatform()->WordCacheCharLimit(),
                  "excessive length for gfxShapedWord!");
 
     // Compute size needed including the mCharacterGlyphs array
     // and a copy of the original text
     uint32_t size = offsetof(gfxShapedWord, mCharGlyphsStorage) +
@@ -1218,17 +1218,17 @@ class gfxShapedWord final : public gfxSh
     }
 
     // Construct in the pre-allocated storage, using placement new
     return new (storage) gfxShapedWord(aText, aLength, aRunScript,
                                        aAppUnitsPerDevUnit, aFlags, aRounding);
   }
 
   static gfxShapedWord* Create(const char16_t* aText, uint32_t aLength,
-                               Script aRunScript, int32_t aAppUnitsPerDevUnit,
+                               Script aRunScript, uint16_t aAppUnitsPerDevUnit,
                                mozilla::gfx::ShapedTextFlags aFlags,
                                gfxFontShaper::RoundingFlags aRounding) {
     NS_ASSERTION(aLength <= gfxPlatform::GetPlatform()->WordCacheCharLimit(),
                  "excessive length for gfxShapedWord!");
 
     // In the 16-bit version of Create, if the TEXT_IS_8BIT flag is set,
     // then we convert the text to an 8-bit version and call the 8-bit
     // Create function instead.
@@ -1290,32 +1290,32 @@ class gfxShapedWord final : public gfxSh
   }
 
  private:
   // so that gfxTextRun can share our DetailedGlyphStore class
   friend class gfxTextRun;
 
   // Construct storage for a ShapedWord, ready to receive glyph data
   gfxShapedWord(const uint8_t* aText, uint32_t aLength, Script aRunScript,
-                int32_t aAppUnitsPerDevUnit,
+                uint16_t aAppUnitsPerDevUnit,
                 mozilla::gfx::ShapedTextFlags aFlags,
                 gfxFontShaper::RoundingFlags aRounding)
       : gfxShapedText(aLength,
                       aFlags | mozilla::gfx::ShapedTextFlags::TEXT_IS_8BIT,
                       aAppUnitsPerDevUnit),
         mScript(aRunScript),
         mRounding(aRounding),
         mAgeCounter(0) {
     memset(mCharGlyphsStorage, 0, aLength * sizeof(CompressedGlyph));
     uint8_t* text = reinterpret_cast<uint8_t*>(&mCharGlyphsStorage[aLength]);
     memcpy(text, aText, aLength * sizeof(uint8_t));
   }
 
   gfxShapedWord(const char16_t* aText, uint32_t aLength, Script aRunScript,
-                int32_t aAppUnitsPerDevUnit,
+                uint16_t aAppUnitsPerDevUnit,
                 mozilla::gfx::ShapedTextFlags aFlags,
                 gfxFontShaper::RoundingFlags aRounding)
       : gfxShapedText(aLength, aFlags, aAppUnitsPerDevUnit),
         mScript(aRunScript),
         mRounding(aRounding),
         mAgeCounter(0) {
     memset(mCharGlyphsStorage, 0, aLength * sizeof(CompressedGlyph));
     char16_t* text = reinterpret_cast<char16_t*>(&mCharGlyphsStorage[aLength]);
--- a/gfx/thebes/gfxFontUtils.h
+++ b/gfx/thebes/gfxFontUtils.h
@@ -5,19 +5,20 @@
 
 #ifndef GFX_FONT_UTILS_H
 #define GFX_FONT_UTILS_H
 
 #include "gfxFontVariations.h"
 #include "gfxPlatform.h"
 #include "nsComponentManagerUtils.h"
 #include "nsTArray.h"
-#include "mozilla/Likely.h"
+#include "mozilla/Casting.h"
 #include "mozilla/Encoding.h"
 #include "mozilla/EndianUtils.h"
+#include "mozilla/Likely.h"
 #include "mozilla/MemoryReporting.h"
 #include "mozilla/UniquePtr.h"
 
 #include "zlib.h"
 #include <algorithm>
 
 /* Bug 341128 - w32api defines min/max which causes problems with <bitset> */
 #ifdef __MINGW32__
@@ -153,17 +154,17 @@ class gfxSparseBitSet {
   void set(uint32_t aIndex) {
     uint32_t i = aIndex / BLOCK_SIZE_BITS;
     while (i >= mBlockIndex.Length()) {
       mBlockIndex.AppendElement(NO_BLOCK);
     }
     if (mBlockIndex[i] == NO_BLOCK) {
       mBlocks.AppendElement();
       MOZ_ASSERT(mBlocks.Length() < 0xffff, "block index overflow!");
-      mBlockIndex[i] = mBlocks.Length() - 1;
+      mBlockIndex[i] = static_cast<uint16_t>(mBlocks.Length() - 1);
     }
     Block& block = mBlocks[mBlockIndex[i]];
     block.mBits[(aIndex >> 3) & (BLOCK_SIZE - 1)] |= 1 << (aIndex & 0x7);
   }
 
   void set(uint32_t aIndex, bool aValue) {
     if (aValue) {
       set(aIndex);
@@ -183,17 +184,17 @@ class gfxSparseBitSet {
     for (uint32_t i = startIndex; i <= endIndex; ++i) {
       const uint32_t blockFirstBit = i * BLOCK_SIZE_BITS;
       const uint32_t blockLastBit = blockFirstBit + BLOCK_SIZE_BITS - 1;
 
       if (mBlockIndex[i] == NO_BLOCK) {
         bool fullBlock = (aStart <= blockFirstBit && aEnd >= blockLastBit);
         mBlocks.AppendElement(Block(fullBlock ? 0xFF : 0));
         MOZ_ASSERT(mBlocks.Length() < 0xffff, "block index overflow!");
-        mBlockIndex[i] = mBlocks.Length() - 1;
+        mBlockIndex[i] = static_cast<uint16_t>(mBlocks.Length() - 1);
         if (fullBlock) {
           continue;
         }
       }
 
       Block& block = mBlocks[mBlockIndex[i]];
       const uint32_t start =
           aStart > blockFirstBit ? aStart - blockFirstBit : 0;
@@ -209,17 +210,17 @@ class gfxSparseBitSet {
   void clear(uint32_t aIndex) {
     uint32_t i = aIndex / BLOCK_SIZE_BITS;
     if (i >= mBlockIndex.Length()) {
       return;
     }
     if (mBlockIndex[i] == NO_BLOCK) {
       mBlocks.AppendElement();
       MOZ_ASSERT(mBlocks.Length() < 0xffff, "block index overflow!");
-      mBlockIndex[i] = mBlocks.Length() - 1;
+      mBlockIndex[i] = static_cast<uint16_t>(mBlocks.Length() - 1);
     }
     Block& block = mBlocks[mBlockIndex[i]];
     block.mBits[(aIndex >> 3) & (BLOCK_SIZE - 1)] &= ~(1 << (aIndex & 0x7));
   }
 
   void ClearRange(uint32_t aStart, uint32_t aEnd) {
     const uint32_t startIndex = aStart / BLOCK_SIZE_BITS;
     const uint32_t endIndex = aEnd / BLOCK_SIZE_BITS;
@@ -273,17 +274,17 @@ class gfxSparseBitSet {
       // if it is missing (implicitly empty), just skip
       if (aBitset.mBlockIndex[i] == NO_BLOCK) {
         continue;
       }
       // if the block is missing in this set, just copy the other
       if (mBlockIndex[i] == NO_BLOCK) {
         mBlocks.AppendElement(aBitset.mBlocks[aBitset.mBlockIndex[i]]);
         MOZ_ASSERT(mBlocks.Length() < 0xffff, "block index overflow!");
-        mBlockIndex[i] = mBlocks.Length() - 1;
+        mBlockIndex[i] = static_cast<uint16_t>(mBlocks.Length() - 1);
         continue;
       }
       // else set existing block to the union of both
       uint32_t* dst =
           reinterpret_cast<uint32_t*>(&mBlocks[mBlockIndex[i]].mBits);
       const uint32_t* src = reinterpret_cast<const uint32_t*>(
           &aBitset.mBlocks[aBitset.mBlockIndex[i]].mBits);
       for (uint32_t j = 0; j < BLOCK_SIZE / 4; ++j) {
@@ -360,17 +361,17 @@ class SharedBitSet {
   // contents of aBitset.
   static SharedBitSet* Create(void* aBuffer, size_t aBufSize,
                               const gfxSparseBitSet& aBitset) {
     MOZ_ASSERT(aBufSize >= RequiredSize(aBitset));
     return new (aBuffer) SharedBitSet(aBitset);
   }
 
   bool test(uint32_t aIndex) const {
-    uint16_t i = aIndex / BLOCK_SIZE_BITS;
+    const auto i = static_cast<uint16_t>(aIndex / BLOCK_SIZE_BITS);
     if (i >= mBlockIndexCount) {
       return false;
     }
     const uint16_t* const blockIndex =
         reinterpret_cast<const uint16_t*>(this + 1);
     if (blockIndex[i] == NO_BLOCK) {
       return false;
     }
@@ -406,17 +407,17 @@ class SharedBitSet {
     }
     return true;
   }
 
  private:
   SharedBitSet() = delete;
 
   explicit SharedBitSet(const gfxSparseBitSet& aBitset)
-      : mBlockIndexCount(aBitset.mBlockIndex.Length()), mBlockCount(0) {
+      : mBlockIndexCount(mozilla::AssertedCast<uint16_t>(aBitset.mBlockIndex.Length())), mBlockCount(0) {
     uint16_t* blockIndex = reinterpret_cast<uint16_t*>(this + 1);
     Block* blocks = reinterpret_cast<Block*>(blockIndex + mBlockIndexCount);
     for (uint16_t i = 0; i < mBlockIndexCount; i++) {
       if (aBitset.mBlockIndex[i] != NO_BLOCK) {
         const Block& srcBlock = aBitset.mBlocks[aBitset.mBlockIndex[i]];
         std::memcpy(&blocks[mBlockCount], &srcBlock, sizeof(Block));
         blockIndex[i] = mBlockCount;
         mBlockCount++;
@@ -852,23 +853,23 @@ class gfxFontUtils {
     mozilla::AutoSwap_PRUint16 length;      // String length (in bytes).
     mozilla::AutoSwap_PRUint16 offset;  // String offset from start of storage
                                         // (in bytes).
   };
 
   // for reading big-endian font data on either big or little-endian platforms
 
   static inline uint16_t ReadShortAt(const uint8_t* aBuf, uint32_t aIndex) {
-    return (aBuf[aIndex] << 8) | aBuf[aIndex + 1];
+    return static_cast<uint16_t>(aBuf[aIndex] << 8) | aBuf[aIndex + 1];
   }
 
   static inline uint16_t ReadShortAt16(const uint16_t* aBuf, uint32_t aIndex) {
     const uint8_t* buf = reinterpret_cast<const uint8_t*>(aBuf);
     uint32_t index = aIndex << 1;
-    return (buf[index] << 8) | buf[index + 1];
+    return static_cast<uint16_t>(buf[index] << 8) | buf[index + 1];
   }
 
   static inline uint32_t ReadUint24At(const uint8_t* aBuf, uint32_t aIndex) {
     return ((aBuf[aIndex] << 16) | (aBuf[aIndex + 1] << 8) |
             (aBuf[aIndex + 2]));
   }
 
   static inline uint32_t ReadLongAt(const uint8_t* aBuf, uint32_t aIndex) {