Re #1994 (misc): Fixes bugs in base64

* move access to input parameter variable after the assertion to check the variable.
 * prevent invalid memory access for empty input string
 * allow using an output buffer of just the right size

Thanks to Adrien Béraud for the patch



git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@5589 74dad513-b988-da41-8d7b-12977e46ad98
remotes/origin/2.7.x
Sauw Ming 7 years ago
parent 2bfc5161cd
commit 568a1967ce

@ -124,14 +124,16 @@ PJ_DEF(pj_status_t) pj_base64_encode(const pj_uint8_t *input, int in_len,
PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input,
pj_uint8_t *out, int *out_len)
{
const char *buf = input->ptr;
int len = (int)input->slen;
const char *buf;
int len;
int i, j, k;
int c[4];
PJ_ASSERT_RETURN(input && out && out_len, PJ_EINVAL);
while (buf[len-1] == '=' && len)
buf = input->ptr;
len = (int)input->slen;
while (len && buf[len-1] == '=')
--len;
PJ_ASSERT_RETURN(*out_len >= PJ_BASE64_TO_BASE256_LEN(len),
@ -161,7 +163,7 @@ PJ_DEF(pj_status_t) pj_base64_decode(const pj_str_t *input,
out[j++] = (pj_uint8_t)(((c[2] & 0x03)<<6) | (c[3] & 0x3F));
}
pj_assert(j < *out_len);
pj_assert(j <= *out_len);
*out_len = j;
return PJ_SUCCESS;

Loading…
Cancel
Save