The size of encoded_data array and the maximum output length parameter
to Base64Encode function were incorrect leading to buffer overflow for
certain cases. The algorithm requires at least 5 bytes of space to even
convert a string of length 1.
Use BASE64_BUFFER_SIZE macro to correctly calculate this output length.
Set size of encoded_data array to the calculated output length.
{
const StringType *str = s;
- unsigned long len = out_size;
- uint8_t encoded_data[str->len * 2];
+ unsigned long len = BASE64_BUFFER_SIZE(str->len);
+ uint8_t encoded_data[len];
if (Base64Encode((unsigned char *)str->ptr, str->len,
encoded_data, &len) != SC_BASE64_OK)
return 0;