-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base64::encode crashes for small buffers #4921
Comments
A little more readable, but doing the same thing for the case in question a couple other lengths I tried:
Would you like to submit a pull request to fix this? |
Your code is better than mine! |
@LaborEtArs Are you using latest master ? |
Before the change, the code was:
which is the same as here on github |
Sorry, I'm using a IE10 right now; having problems with the github site... so can't insert links... |
Sorry, I had no knowledge of this duplicate length calculation code. |
The updated macros in cencode.h are OK; same results as earlephilhower's code for length = 0..1024 :-) |
Sorry, failed to create a PR; do I really have to create a branch of the repo just for such a small change??? |
Yes :) |
Fork the repo only once. |
Forking worked, Local cloning failed two times with: Will try again next week... |
Pull Request seems to be accepted (and merged) by now, but I can't find the change when looking into 'base64.cpp'... |
#4934 is now merged. |
String base64::encode(uint8_t * data, size_t length, bool doNewLines) will crash when used with a quite short input buffer; for example 4 bytes.
A 4 byte input to base64 will produce an 8 byte output (plus trailing '\0' = 9), as two filling bytes will by attached to the input before processing. The buffer calculated by base64::encode however is only 4 * 1.6f + 1 = 6.4 + 1 = 6 + 1 = 7 bytes long!
A better size algorithm would be:
size_t size = ((((length * 4) + 2) / 3) + (3 - (length % 3)) + 1); if (doNewLines) size += ((size + 71) / 72);
(((length * 4) + 2) / 3):
3 input bytes (24 bits) will be converted to 4 output bytes (4 * 6bits = 24bit); the +2 ensures the division to be the next ceiling integer
(3 - (length % 3)):
Input buffers that can't be divided by 3 will be filled up by the algorithm
1:
Trailing '\0'
((size + 71) / 72):
If 'doNewLines' is true, every 72 (encoded) chars a newline will be added (again the +71 ensures the division to be the next ceiling integer)
The text was updated successfully, but these errors were encountered: