Bug 1362449 - part 11 - fix decoding subroutines to cope with wide output types; r=erahm
authorNathan Froyd <froydnj@mozilla.com>
Wed, 06 Sep 2017 16:58:36 -0400
changeset 428881 f2669ac73b9078fc82cbff32eb97c2f2440ce248
parent 428880 c92f1975d9367e10a9fbb77f08ee0e3f6761127e
child 428882 981709ff412efd00b236ca2f0c76bfda6de786be
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerserahm
bugs1362449
milestone57.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 1362449 - part 11 - fix decoding subroutines to cope with wide output types; r=erahm The result of decoding needs to be an explicit 8-bit type before being widened to the output type to avoid undesirable effects like sign extension.
xpcom/io/Base64.cpp
--- a/xpcom/io/Base64.cpp
+++ b/xpcom/io/Base64.cpp
@@ -417,47 +417,47 @@ Decode4to3(const T* aSrc, U* aDest, Deco
 {
   uint8_t w, x, y, z;
   if (!aToVal(aSrc[0], &w) ||
       !aToVal(aSrc[1], &x) ||
       !aToVal(aSrc[2], &y) ||
       !aToVal(aSrc[3], &z)) {
     return false;
   }
-  aDest[0] = U(w << 2 | x >> 4);
-  aDest[1] = U(x << 4 | y >> 2);
-  aDest[2] = U(y << 6 | z);
+  aDest[0] = U(uint8_t(w << 2 | x >> 4));
+  aDest[1] = U(uint8_t(x << 4 | y >> 2));
+  aDest[2] = U(uint8_t(y << 6 | z));
   return true;
 }
 
 template<typename T, typename U, typename Decoder>
 static bool
 Decode3to2(const T* aSrc, U* aDest, Decoder aToVal)
 {
   uint8_t w, x, y;
   if (!aToVal(aSrc[0], &w) ||
       !aToVal(aSrc[1], &x) ||
       !aToVal(aSrc[2], &y)) {
     return false;
   }
-  aDest[0] = U(w << 2 | x >> 4);
-  aDest[1] = U(x << 4 | y >> 2);
+  aDest[0] = U(uint8_t(w << 2 | x >> 4));
+  aDest[1] = U(uint8_t(x << 4 | y >> 2));
   return true;
 }
 
 template<typename T, typename U, typename Decoder>
 static bool
 Decode2to1(const T* aSrc, U* aDest, Decoder aToVal)
 {
   uint8_t w, x;
   if (!aToVal(aSrc[0], &w) ||
       !aToVal(aSrc[1], &x)) {
     return false;
   }
-  aDest[0] = U(w << 2 | x >> 4);
+  aDest[0] = U(uint8_t(w << 2 | x >> 4));
   return true;
 }
 
 template<typename SrcT, typename DestT>
 static nsresult
 Base64DecodeHelper(const SrcT* aBase64, uint32_t aBase64Len, DestT* aBinary,
                    uint32_t* aBinaryLen)
 {