From f5bc5147b972b7fc27b38c2ecb290d8cf1a2dd74 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Thu, 31 Oct 2019 16:32:35 +0100 Subject: [PATCH] base64: Fix size types of bin_to_base64() Fixes T188 Signed-off-by: Andreas Schneider Reviewed-by: Jakub Jelen --- include/libssh/priv.h | 2 +- src/base64.c | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/libssh/priv.h b/include/libssh/priv.h index 90ef0dcf..68c67d3e 100644 --- a/include/libssh/priv.h +++ b/include/libssh/priv.h @@ -287,7 +287,7 @@ socket_t ssh_connect_host_nonblocking(ssh_session session, const char *host, /* in base64.c */ ssh_buffer base64_to_bin(const char *source); -unsigned char *bin_to_base64(const unsigned char *source, int len); +uint8_t *bin_to_base64(const uint8_t *source, size_t len); /* gzip.c */ int compress_buffer(ssh_session session,ssh_buffer buf); diff --git a/src/base64.c b/src/base64.c index f019ef76..c771b831 100644 --- a/src/base64.c +++ b/src/base64.c @@ -270,11 +270,11 @@ static void _bin_to_base64(uint8_t *dest, * * @returns the converted string */ -unsigned char *bin_to_base64(const unsigned char *source, int len) +uint8_t *bin_to_base64(const uint8_t *source, size_t len) { - unsigned char *base64; - unsigned char *ptr; - int flen = len + (3 - (len % 3)); /* round to upper 3 multiple */ + uint8_t *base64 = NULL; + uint8_t *ptr = NULL; + size_t flen = len + (3 - (len % 3)); /* round to upper 3 multiple */ flen = (4 * flen) / 3 + 1; base64 = malloc(flen); @@ -286,6 +286,9 @@ unsigned char *bin_to_base64(const unsigned char *source, int len) while(len > 0){ _bin_to_base64(ptr, source, len > 3 ? 3 : len); ptr += 4; + if (len < 3) { + break; + } source += 3; len -= 3; }