Просмотр исходного кода

Clarify comment on base64_encode accumulator signedness

yhirose 1 месяц назад
Родитель
Сommit
0ae93881b4
1 измененных файлов с 4 добавлено и 2 удалено
  1. 4 2
      httplib.h

+ 4 - 2
httplib.h

@@ -4665,8 +4665,10 @@ inline std::string base64_encode(const std::string &in) {
   std::string out;
   out.reserve(in.size());
 
-  // Unsigned: once four bytes are folded in the top bit is set, so the next
-  // `val << 8` would left-shift a negative int (undefined behaviour).
+  // Unsigned: the accumulator is never masked, so with a signed int the
+  // `val << 8` below overflows once enough bytes are folded in (undefined
+  // behaviour before C++20). Only the low bits are ever emitted, so the
+  // wrap-around of an unsigned accumulator does not affect the output.
   uint32_t val = 0;
   auto valb = -6;