From c146fcf581f03deab6faa9f9fc3a871d4c0bd82a Mon Sep 17 00:00:00 2001 From: Kareem Date: Tue, 20 Sep 2022 14:44:01 -0700 Subject: [PATCH] Update Base16_Encode so the ending null terminator is optional. --- wolfcrypt/src/coding.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/coding.c b/wolfcrypt/src/coding.c index bc9262ccb..fedd6c48b 100644 --- a/wolfcrypt/src/coding.c +++ b/wolfcrypt/src/coding.c @@ -563,7 +563,7 @@ int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen) if (in == NULL || out == NULL || outLen == NULL) return BAD_FUNC_ARG; - if (*outLen < (2 * inLen + 1)) + if (*outLen < (2 * inLen)) return BAD_FUNC_ARG; for (i = 0; i < inLen; i++) { @@ -584,8 +584,9 @@ int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen) out[outIdx++] = lb; } - /* force 0 at this end */ - out[outIdx++] = 0; + /* If the output buffer has a room for an extra byte, add a null terminator */ + if (*outLen > outIdx) + out[outIdx++]= '\0'; *outLen = outIdx; return 0;