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(-)
diff --git a/ell/base64.c b/ell/base64.c
index c241869..e430470 100644
--- a/ell/base64.c
+++ b/ell/base64.c
@@ -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;
}
diff --git a/ell/base64.h b/ell/base64.h
index 74dae8f..8564cdd 100644
--- a/ell/base64.h
+++ b/ell/base64.h
@@ -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
}
diff --git a/ell/pem.c b/ell/pem.c
index 2b09c2b..9804b91 100644
--- a/ell/pem.c
+++ b/ell/pem.c
@@ -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));
--
2.31.1
Show replies by date
---
unit/test-base64.c | 7 +++----
1 file changed, 3 insertions(+), 4 deletions(-)
diff --git a/unit/test-base64.c b/unit/test-base64.c
index 2c2a57d..6e47c2b 100644
--- a/unit/test-base64.c
+++ b/unit/test-base64.c
@@ -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);
}
--
2.31.1
Hi James,
On 11/17/21 3:49 PM, James Prestwood wrote:
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(-)
Both applied, thanks.
Regards,
-Denis