From 83f5e1b3fd86c35cb1a06caae6253c596c50e93f Mon Sep 17 00:00:00 2001 From: deuce <> Date: Fri, 23 Feb 2018 02:03:22 +0000 Subject: [PATCH] Don't read past the end of the input string, even if you're going to ignore what you see there. --- src/encode/base64.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/encode/base64.c b/src/encode/base64.c index 65e4c088d3..8c2217ae77 100644 --- a/src/encode/base64.c +++ b/src/encode/base64.c @@ -131,22 +131,29 @@ int b64_encode(char *target, size_t tlen, const char *source, size_t slen) { FREE_AND_NULL(tmpbuf); return(-1); } - enc=buf|((*inp & 0xF0) >> 4); + if(inp==inend) + done=1; + if (!done) + enc=buf|((*inp & 0xF0) >> 4); if(add_char(outp++, enc, done, outend)) { FREE_AND_NULL(tmpbuf); return(-1); } - if(inp==inend) - done=1; - buf=(*(inp++)<<2)&0x3C; - enc=buf|((*inp & 0xC0)>>6); + if (!done) { + buf=(*(inp++)<<2)&0x3C; + if (inp == inend) + enc=buf; + else + enc=buf|((*inp & 0xC0)>>6); + } if(add_char(outp++, enc, done, outend)) { FREE_AND_NULL(tmpbuf); return(-1); } if(inp==inend) done=1; - enc=((int)*(inp++))&0x3F; + if (!done) + enc=((int)*(inp++))&0x3F; if(add_char(outp++, enc, done, outend)) { FREE_AND_NULL(tmpbuf); return(-1); -- GitLab