From b97b2376955669a81f853fb609ba18cd421611c5 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 15 Nov 2018 13:28:19 -0800 Subject: [PATCH] Static Analysis Fixes Ran the clang static analysis and infer and fixed most of the reported items. There were many that infer found that looked like false positives. --- examples/client/client.c | 4 ++-- src/internal.c | 10 ++++++---- src/ssh.c | 2 +- src/wolfsftp.c | 8 +++++--- tests/unit.c | 2 +- wolfsftp/client/sftpclient.c | 4 +++- 6 files changed, 18 insertions(+), 12 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index aa8788ea..640a5fce 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -114,7 +114,7 @@ static int wsUserAuth(byte authType, void* ctx) { const char* defaultPassword = (const char*)ctx; - word32 passwordSz; + word32 passwordSz = 0; int ret = WOLFSSH_USERAUTH_SUCCESS; (void)authType; @@ -133,8 +133,8 @@ static int wsUserAuth(byte authType, char* c = strpbrk((char*)userPassword, "\r\n");; if (c != NULL) *c = '\0'; - passwordSz = (word32)strlen((const char*)userPassword); } + passwordSz = (word32)strlen((const char*)userPassword); SetEcho(1); #ifdef USE_WINDOWS_API printf("\r\n"); diff --git a/src/internal.c b/src/internal.c index d8276a44..7128ce73 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1846,11 +1846,13 @@ static int DoKexInit(WOLFSSH* ssh, byte* buf, word32 len, word32* idx) * is save the actual values. */ - if (ssh->handshake == NULL) { - ssh->handshake = HandshakeInfoNew(ssh->ctx->heap); + if (ret == WS_SUCCESS) { if (ssh->handshake == NULL) { - WLOG(WS_LOG_DEBUG, "Couldn't allocate handshake info"); - ret = WS_MEMORY_E; + ssh->handshake = HandshakeInfoNew(ssh->ctx->heap); + if (ssh->handshake == NULL) { + WLOG(WS_LOG_DEBUG, "Couldn't allocate handshake info"); + ret = WS_MEMORY_E; + } } } diff --git a/src/ssh.c b/src/ssh.c index fbb56da9..7f5f85ed 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -865,7 +865,7 @@ int wolfSSH_SetChannelType(WOLFSSH* ssh, byte type, byte* name, word32 nameSz) int wolfSSH_SetUsername(WOLFSSH* ssh, const char* username) { char* value = NULL; - word32 valueSz; + word32 valueSz = 0; int ret = WS_SUCCESS; if (ssh == NULL || ssh->handshake == NULL || diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 749c32fc..8b2e94e4 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -136,10 +136,10 @@ static int SFTP_ServerRecvInit(WOLFSSH* ssh) { if (sz > 0) { byte* data = (byte*)WMALLOC(sz, NULL, DYNTYPE_BUFFER); if (data == NULL) return WS_MEMORY_E; - if ((len = wolfSSH_stream_read(ssh, data, sz)) != (int)sz) { - return len; - } + len = wolfSSH_stream_read(ssh, data, sz); WFREE(data, NULL, DYNTYPE_BUFFER); + if (len != (int)sz) + return len; } ssh->reqId++; @@ -903,6 +903,7 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, word32 maxSz) DYNTYPE_SFTP); if (dirList == NULL) { WFREE(dir, ssh->ctx->heap, DYNTYPE_BUFFER); + WCLOSEDIR(&ctx); return WS_MEMORY_E; } #ifdef WOLFSSL_NUCLEUS @@ -922,6 +923,7 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, word32 maxSz) ssh->ctx->heap, DYNTYPE_SFTP); if (cur == NULL) { WFREE(dir, ssh->ctx->heap, DYNTYPE_BUFFER); + WCLOSEDIR(&ctx); return WS_MEMORY_E; } #ifdef WOLFSSL_NUCLEUS diff --git a/tests/unit.c b/tests/unit.c index 3f29f8cd..9e6a37f9 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -49,7 +49,7 @@ static int Base16_Decode(const byte* in, word32 inLen, word32 outIdx = 0; if (inLen == 1 && *outLen && in) { - byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */ + byte b = in[inIdx] - 0x30; /* 0 starts at 0x30 */ /* sanity check */ if (b >= sizeof(hexDecode)/sizeof(hexDecode[0])) diff --git a/wolfsftp/client/sftpclient.c b/wolfsftp/client/sftpclient.c index ee02541c..8b8adf58 100644 --- a/wolfsftp/client/sftpclient.c +++ b/wolfsftp/client/sftpclient.c @@ -259,8 +259,8 @@ static int wsUserAuth(byte authType, char* c = strpbrk((char*)userPassword, "\r\n");; if (c != NULL) *c = '\0'; - passwordSz = (word32)strlen((const char*)userPassword); } + passwordSz = (word32)strlen((const char*)userPassword); SetEcho(1); #ifdef USE_WINDOWS_API printf("\r\n"); @@ -308,6 +308,8 @@ int doCmds() if (pt[0] != '/') { int maxSz = (int)WSTRLEN(workingDir) + sz + 2; f = XMALLOC(maxSz, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (f == NULL) + return WS_MEMORY_E; f[0] = '\0'; WSTRNCAT(f, workingDir, maxSz);