ReneNyffenegger / cpp-base64

base64 encoding and decoding with c++
Other
891 stars 311 forks source link

Accepts base64 encoded data without padding #25

Closed Bouska closed 3 years ago

Bouska commented 3 years ago

Current code does not accept non padded data while the RFC2045 accepts non padded data. If non padded data is passed, the code will either throw std::runtime_error: Input is not valid base64-encoded data. when decoding the NULL terminator of the string or fail with a out of bound check.

With proposed code, we check for either end of the string (for non padded data) or a padding character. I didn't check the performance, but I'm expecting a performance drop because of this additional check.

Fixes #24

ReneNyffenegger commented 3 years ago

I believe this should be (pos + 2 < length_of_string) instead of (pos + 2 < length_of_string - 1) and (pos + 3 < length_of_string) instead of (pos + 3 < length_of_string - 1)

ReneNyffenegger commented 3 years ago

Used pos + 2 < length_of_string to check for unpadded data.

Bouska commented 3 years ago

You are right, < length_of_string allows to check for the last non-NULL character. Somehow my tests with < length_of_string - 1 worked 🤔