Source code:
#include <cstdio>
static const char encodeBase64Table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int main() {
// create decoding table
unsigned long invalid = 64;
unsigned long decodeBase64Table[256] = {};
for (unsigned long i = 0; i < 256; i++)
decodeBase64Table[i] = invalid;
for (unsigned long i = 0; i < 64; i++) {
decodeBase64Table[(unsigned char)encodeBase64Table[i]] = i;
}
printf("decodeBase64Table[65] = %lu\n", decodeBase64Table[65]);
return 0;
}
Apple Clang 11.0.3 (expected output, even with -O3):
/t/s/1673994196 ❯❯❯ /usr/bin/clang++ --version
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
/t/s/1673994196 ❯❯❯ /usr/bin/clang++ -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -O3 a.cpp
/t/s/1673994196 ❯❯❯ ./a.out
decodeBase64Table[65] = 0
Apple Clang 14.0.0 (unexpected output, only with -O3):
/t/s/1673994196 ❯❯❯ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/t/s/1673994196 ❯❯❯ /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk -O3 a.cpp
/t/s/1673994196 ❯❯❯ ./a.out
decodeBase64Table[65] = 64
This problem is reproducible on x86_64
and arm64