[compiler-rt] 292ab49 - Fix UB in compiler-rt base64 implementation

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 04:29:10 PST 2020


Author: serge-sans-paille
Date: 2020-03-03T13:28:32+01:00
New Revision: 292ab49d43a2b73295c8d5851d84de4608acc6ba

URL: https://github.com/llvm/llvm-project/commit/292ab49d43a2b73295c8d5851d84de4608acc6ba
DIFF: https://github.com/llvm/llvm-project/commit/292ab49d43a2b73295c8d5851d84de4608acc6ba.diff

LOG: Fix UB in compiler-rt base64 implementation

As a follow-up to 1454c27b60447d969d0c1ecaf20b2186fe9d85ec

Added: 
    

Modified: 
    compiler-rt/lib/fuzzer/FuzzerUtil.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/fuzzer/FuzzerUtil.cpp b/compiler-rt/lib/fuzzer/FuzzerUtil.cpp
index 87180d1ea85d..7eecb68d0729 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtil.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtil.cpp
@@ -161,20 +161,21 @@ std::string Base64(const Unit &U) {
 
   size_t i = 0, j = 0;
   for (size_t n = U.size() / 3 * 3; i < n; i += 3, j += 4) {
-    uint32_t x = (U[i] << 16) | (U[i + 1] << 8) | U[i + 2];
+    uint32_t x = ((unsigned char)U[i] << 16) | ((unsigned char)U[i + 1] << 8) |
+                 (unsigned char)U[i + 2];
     Buffer[j + 0] = Table[(x >> 18) & 63];
     Buffer[j + 1] = Table[(x >> 12) & 63];
     Buffer[j + 2] = Table[(x >> 6) & 63];
     Buffer[j + 3] = Table[x & 63];
   }
   if (i + 1 == U.size()) {
-    uint32_t x = (U[i] << 16);
+    uint32_t x = ((unsigned char)U[i] << 16);
     Buffer[j + 0] = Table[(x >> 18) & 63];
     Buffer[j + 1] = Table[(x >> 12) & 63];
     Buffer[j + 2] = '=';
     Buffer[j + 3] = '=';
   } else if (i + 2 == U.size()) {
-    uint32_t x = (U[i] << 16) | (U[i + 1] << 8);
+    uint32_t x = ((unsigned char)U[i] << 16) | ((unsigned char)U[i + 1] << 8);
     Buffer[j + 0] = Table[(x >> 18) & 63];
     Buffer[j + 1] = Table[(x >> 12) & 63];
     Buffer[j + 2] = Table[(x >> 6) & 63];


        


More information about the llvm-commits mailing list