Commit ae8b7a0f authored by Nikias Bassen's avatar Nikias Bassen

base64: Prevent use of strlen() in base64decode when input buffer size is known

parent 5e8fb617
...@@ -105,22 +105,23 @@ static int base64decode_block(unsigned char *target, const char *data, size_t da ...@@ -105,22 +105,23 @@ static int base64decode_block(unsigned char *target, const char *data, size_t da
unsigned char *base64decode(const char *buf, size_t *size) unsigned char *base64decode(const char *buf, size_t *size)
{ {
if (!buf) return NULL; if (!buf || !size) return NULL;
size_t len = strlen(buf); size_t len = (*size > 0) ? *size : strlen(buf);
if (len <= 0) return NULL; if (len <= 0) return NULL;
unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3); unsigned char *outbuf = (unsigned char*)malloc((len/4)*3+3);
const char *ptr = buf; const char *ptr = buf;
int p = 0; int p = 0;
size_t l = 0;
do { do {
ptr += strspn(ptr, "\r\n\t "); ptr += strspn(ptr, "\r\n\t ");
if (*ptr == '\0') { if (*ptr == '\0' || ptr >= buf+len) {
break; break;
} }
len = strcspn(ptr, "\r\n\t "); l = strcspn(ptr, "\r\n\t ");
if (len > 3) { if (l > 3 && ptr+l <= buf+len) {
p+=base64decode_block(outbuf+p, ptr, len); p+=base64decode_block(outbuf+p, ptr, l);
ptr += len; ptr += l;
} else { } else {
break; break;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment