From 9e4cb15135ef66afc3ff3bc0446e82c2d2d71435 Mon Sep 17 00:00:00 2001 From: Jacob Barthelmeh Date: Fri, 22 May 2020 10:40:15 -0600 Subject: [PATCH] add free of buffer to GetStringAlloc --- src/internal.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/internal.c b/src/internal.c index 8739e32..8eef403 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1661,7 +1661,8 @@ static int GetString(char* s, word32* sSz, /* Gets the size of a string, allocates memory to hold it plus a NULL, then - * copies it into the allocated buffer, and terminates it with a NULL. */ + * copies it into the allocated buffer, and terminates it with a NULL. + * If s points to an already created buffer, than it is free'd first */ static int GetStringAlloc(WOLFSSH* ssh, char** s, byte* buf, word32 len, word32 *idx) { @@ -1674,12 +1675,16 @@ static int GetStringAlloc(WOLFSSH* ssh, char** s, if (result == WS_SUCCESS) { if (*idx >= len || strSz > len - *idx) return WS_BUFFER_E; + str = (char*)WMALLOC(strSz + 1, ssh->ctx->heap, DYNTYPE_STRING); if (str == NULL) return WS_MEMORY_E; WMEMCPY(str, buf + *idx, strSz); *idx += strSz; str[strSz] = '\0'; + + if (*s != NULL) + WFREE(*s, ssh->ctx->heap, DYNTYPE_STRING); *s = str; }