base64: Null terminate encoding
James Prestwood
PATCH 1/2 base64: Null terminate encoding
l_base64_encode was returning a char* without any NULL terminator. This appeared to be written to satisfy the only use in ELL (pem.c) but made for a confusing public API. Anyone using this API would expect a char* return to be NULL terminated.
Now l_base64_encode will NULL terminate which also removes the need for the length out parameter.
pem.c was updated to use strlen rather than rely on the out parameter.
--- ell/base64.c | 8 ++++---- ell/base64.h | 3 +-- ell/pem.c | 3 ++- 3 files changed, 7 insertions(+), 7 deletions(-)
ell/base64.c +4 -4
@@ -107,8 +107,7 @@ LIB_EXPORT uint8_t *l_base64_decode(const char *in, size_t in_len,return out_buf; } -LIB_EXPORT char *l_base64_encode(const uint8_t *in, size_t in_len, -int columns,size_t *n_written) +LIB_EXPORT char *l_base64_encode(const uint8_t *in, size_t in_len, int columns) {const uint8_t *in_end = in + in_len;char *out_buf, *out;
@@ -127,8 +126,7 @@ LIB_EXPORT char *l_base64_encode(const uint8_t *in, size_t in_len,if (columns && out_len) out_len += (out_len -4 ) / columns; - out_buf =l_malloc (out_len); - *n_written = out_len; + out_buf =l_malloc (out_len +1 ); out = out_buf;
@@ -169,5 +167,7 @@ LIB_EXPORT char *l_base64_encode(const uint8_t *in, size_t in_len,for (; pad <4 ; pad++) *out++ ='=' ; + *out ='\0' ; +return out_buf; }
ell/base64.h +1 -2
@@ -27,8 +27,7 @@ extern "C" {uint8_t *l_base64_decode (const char *in,size_t in_len,size_t *n_written); -char *l_base64_encode (const uint8_t *in,size_t in_len,int columns, -size_t *n_written); +char *l_base64_encode (const uint8_t *in,size_t in_len,int columns); #ifdef __cplusplus }
ell/pem.c +2 -1
@@ -379,7 +379,8 @@ static bool pem_write_one_cert(struct l_cert *cert, void *user_data) iov[0 ].iov_base ="-----BEGIN CERTIFICATE-----\n" ; iov[0 ].iov_len =strlen (iov[0 ].iov_base); - iov[1 ].iov_base =l_base64_encode (der, der_len,64 , &iov[1 ].iov_len); + iov[1 ].iov_base =l_base64_encode (der, der_len,64 ); + iov[1 ].iov_len =strlen (iov[1 ].iov_base); iov[2 ].iov_base ="\n-----END CERTIFICATE-----\n" ; iov[2 ].iov_len =strlen (iov[2 ].iov_base); r =L_TFR (writev (*fd, iov,3 ));
Signature · 1 line
2.31.1
James Prestwood
PATCH 2/2 unit: update base64 test with API change
--- unit/test-base64.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
unit/test-base64.c +3 -4
@@ -115,13 +115,12 @@ static void test_base64_encode(const void *data) {const struct base64_encode_test *test = data;char *encoded; -size_t encoded_size; encoded =l_base64_encode ((uint8_t *)test->input,strlen (test->input), - test->columns, &encoded_size); + test->columns);assert (encoded); -assert (encoded_size ==strlen (test->output)); -assert (!memcmp (encoded, test->output, encoded_size)); +assert (strlen (encoded) ==strlen (test->output)); +assert (!memcmp (encoded, test->output,strlen (encoded)));l_free (encoded); }
Signature · 1 line
2.31.1
Denis Kenzior
Hi James,
Quoted 8 lines · James Prestwood
l_base64_encodewas returning a char* without any NULL terminator. This appeared to be written to satisfy the only use in ELL (pem.c) but made for a confusing public API. Anyone using this API would expect a char* return to be NULL terminated.Now
l_base64_encodewill NULL terminate which also removes the need for the length out parameter.
Quoted 7 lines
pem.cwas updated to use strlen rather than rely on the out parameter.--- ell/base64.c | 8 ++++---- ell/base64.h | 3 +-- ell/pem.c | 3 ++- 3 files changed, 7 insertions(+), 7 deletions(-)
Both applied, thanks.
Regards,
-Denis