Fix UB in compiler-rt base64 implementation
authorserge-sans-paille <sguelton@redhat.com>
Tue, 3 Mar 2020 12:27:36 +0000 (13:27 +0100)
committerserge-sans-paille <sguelton@redhat.com>
Tue, 3 Mar 2020 12:28:32 +0000 (13:28 +0100)
As a follow-up to 1454c27b60447d969d0c1ecaf20b2186fe9d85ec

compiler-rt/lib/fuzzer/FuzzerUtil.cpp

index 87180d1..7eecb68 100644 (file)
@@ -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];