Skip to content

Commit

Permalink
make base64 output buffer be caller-allocated and fix memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
csl04r committed Dec 3, 2018
1 parent df90821 commit 864234d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions corkscrew.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,24 @@ char linefeed[] = "\r\n\r\n"; /* it is better and tested with oops & squid */
const static char base64[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

#ifdef ANSI_FUNC
int base64_encode (char *out, char *in, size_t *outlen, size_t *encoded_len)
int base64_encode (char **outp, char *in, size_t *outlen, size_t *encoded_len)
#else
int base64_encode (out, in, outlen, encoded_len)
char *out;
int base64_encode (outp, in, outlen, encoded_len)
char **outp;
char *in;
size_t *outlen;
size_t *encoded_len;
#endif
{
char *src, *end;
char *src, *end, *out;
int ret;
size_t outlen_min;

unsigned int tmp;

int i,inlen;

out = *outp;
if (!in || !out || !outlen || !*outlen) {
return -1; // bad input
}
Expand All @@ -74,7 +75,7 @@ size_t *encoded_len;
outlen_min = 4 * ((inlen + 2) / 3) + 1;
if (*outlen < outlen_min) {
*outlen = outlen_min;
out = realloc(out, outlen_min);
*outp = realloc(*outp, outlen_min);
}

ret = 0;
Expand Down Expand Up @@ -125,7 +126,7 @@ size_t *encoded_len;
break;
}

*encoded_len = strlen(out);
*encoded_len = strlen(*outp);
return ret;
}

Expand Down Expand Up @@ -242,7 +243,7 @@ char *argv[];
strncat(uri, " HTTP/1.0", sizeof(uri) - strlen(uri) - 1);
if (up != NULL) {
size_t encoded_len = 0;
int encode_result = base64_encode(up, b64_buf, &b64_buf_len, &encoded_len);
int encode_result = base64_encode(&b64_buf, up, &b64_buf_len, &encoded_len);
if (0 == encode_result && encoded_len > 0) {
strncat(uri, "\nProxy-Authorization: Basic ", sizeof(uri) - strlen(uri) - 1);
strncat(uri, b64_buf, encoded_len);
Expand Down

0 comments on commit 864234d

Please sign in to comment.