mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-21 00:38:40 +00:00
switch_b64_encode wrote each base64 character before checking the output bound, and computed that bound as `bytes >= (int)olen - 1`. Both are unsafe: - Writing before the check meant a buffer with no room for a data byte still received one: `olen == 1` wrote a character at index 0 and the NUL at index 1, and `olen == 0` (where `(int)olen - 1` is -1) wrote two bytes, both past the end. - Casting the unsigned `olen` to `int` truncated it above INT_MAX, producing a wrong bound for very large buffers. Reject `olen == 0`, make the output index `bytes` a `size_t`, and test `bytes + 1 >= olen` before each write, so the bound never casts or underflows and always leaves the last byte for the terminator. Apply the same `bytes + 1 < olen` guard to the trailing partial-group character and the `=` padding, so no write can pass the end. Remove the dead line-wrap counter `y`: it only gated a commented-out newline emit, so it counted and reset with no effect on the output. Add unit tests covering the output-bound edges for every write site.