Skip to contentSkip to conversation

base64: Null terminate encoding

PATCH In review James Prestwood 2 participants 3 messages Nov 17, 2021 – Nov 19, 2021

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

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
index c241869..e430470 100644
@@ -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
index 74dae8f..8564cdd 100644
@@ -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
index 2b09c2b..9804b91 100644
@@ -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
author
#1 lore raw

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
index 2c2a57d..6e47c2b 100644
@@ -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
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 8 lines · James Prestwood

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.

Quoted 7 lines

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(-)

Both applied, thanks.

Regards,
-Denis