Fixing base64 decoding error when input string is bad
The following code would crash the previous version when calling MemFree:
// 53 * A
const char maliciousBase64Input[] = "AAAAAAAAAAAAAAAAAAAAAAAA"
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
int decodedSize = 0;
unsigned char *decodedData = DecodeDataBase64(
maliciousBase64Input, &decodedSize);
if (decodedData) {
MemFree(decodedData);
}
The reason is a lack of array bound checks in the decoding loop, which
corrupted here the heap (though this is platform dependent).
Adding the bound checks here prevents the memory corruption.
Tested with encoding random data of sizes 0-1023 and comparing it
with the decoded result.