All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] base64: Null terminate encoding
@ 2021-11-17 21:49 James Prestwood
  0 siblings, 0 replies; 2+ messages in thread
From: James Prestwood @ 2021-11-17 21:49 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 2533 bytes --]

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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/2] base64: Null terminate encoding
@ 2021-11-19 16:13 Denis Kenzior
  0 siblings, 0 replies; 2+ messages in thread
From: Denis Kenzior @ 2021-11-19 16:13 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 693 bytes --]

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-11-19 16:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-17 21:49 [PATCH 1/2] base64: Null terminate encoding James Prestwood
2021-11-19 16:13 Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.