From 720c16bf0ae7557f03eac81dc34d22ac78429837 Mon Sep 17 00:00:00 2001 From: Alex Duan <417921451@qq.com> Date: Wed, 19 Oct 2022 19:53:10 +0800 Subject: [PATCH] fix(shell): coverity problem for some --- source/util/src/tconfig.c | 2 ++ tools/shell/src/shellAuto.c | 20 ++++++-------------- tools/shell/src/shellCommand.c | 8 ++++++++ tools/shell/src/shellTire.c | 23 +++++++++++------------ 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index edb2f0380e..0bf9e7cc33 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -944,6 +944,7 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { if (taosReadFile(pFile, buf, fileSize) <= 0) { taosCloseFile(&pFile); uError("load json file error: %s", filepath); + taosMemoryFreeClear(buf); return -1; } taosCloseFile(&pFile); @@ -953,6 +954,7 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { if (jsonParseError != NULL) { uError("load json file parse error: %s", jsonParseError); } + taosMemoryFreeClear(buf); return -1; } taosMemoryFreeClear(buf); diff --git a/tools/shell/src/shellAuto.c b/tools/shell/src/shellAuto.c index 46eaf6cff1..8d980ba653 100644 --- a/tools/shell/src/shellAuto.c +++ b/tools/shell/src/shellAuto.c @@ -562,23 +562,15 @@ void parseCommand(SWords* command, bool pattern) { // free SShellCmd void freeCommand(SWords* command) { - SWord* word = command->head; - if (word == NULL) { - return; - } - + SWord* item = command->head; // loop - while (word->next) { - SWord* tmp = word; - word = word->next; + while (item) { + SWord* tmp = item; + item = item->next; // if malloc need free if (tmp->free && tmp->word) taosMemoryFree(tmp->word); taosMemoryFree(tmp); } - - // if malloc need free - if (word->free && word->word) taosMemoryFree(word->word); - taosMemoryFree(word); } void GenerateVarType(int type, char** p, int count) { @@ -1204,11 +1196,11 @@ bool nextMatchCommand(TAOS* con, SShellCmd* cmd, SWords* firstMatch) { #endif // free + freeCommand(input); if (input->source) { taosMemoryFree(input->source); input->source = NULL; } - freeCommand(input); taosMemoryFree(input); return true; @@ -1377,7 +1369,7 @@ bool appendAfterSelect(TAOS* con, SShellCmd* cmd, char* sql, int32_t len) { bool ret = false; if (from == NULL) { bool fieldEnd = fieldsInputEnd(p); - // cheeck fields input end then insert from keyword + // check fields input end then insert from keyword if (fieldEnd && p[len - 1] == ' ') { shellInsertChar(cmd, "from", 4); taosMemoryFree(p); diff --git a/tools/shell/src/shellCommand.c b/tools/shell/src/shellCommand.c index c34b22b691..665c832b85 100644 --- a/tools/shell/src/shellCommand.c +++ b/tools/shell/src/shellCommand.c @@ -103,6 +103,8 @@ void shellInsertChar(SShellCmd *cmd, char *c, int32_t size) { cmd->cursorOffset += size; cmd->screenOffset += taosWcharWidth(wc); cmd->endOffset += taosWcharWidth(wc); + // set string end + cmd->command[cmd->commandSize] = 0; #ifdef WINDOWS #else shellShowOnScreen(cmd); @@ -123,6 +125,8 @@ void shellBackspaceChar(SShellCmd *cmd) { cmd->cursorOffset -= size; cmd->screenOffset -= width; cmd->endOffset -= width; + // set string end + cmd->command[cmd->commandSize] = 0; shellShowOnScreen(cmd); } } @@ -136,6 +140,8 @@ void shellClearLineBefore(SShellCmd *cmd) { cmd->cursorOffset = 0; cmd->screenOffset = 0; cmd->endOffset = cmd->commandSize; + // set string end + cmd->command[cmd->commandSize] = 0; shellShowOnScreen(cmd); } @@ -160,6 +166,8 @@ void shellDeleteChar(SShellCmd *cmd) { cmd->commandSize - cmd->cursorOffset - size); cmd->commandSize -= size; cmd->endOffset -= width; + // set string end + cmd->command[cmd->commandSize] = 0; shellShowOnScreen(cmd); } } diff --git a/tools/shell/src/shellTire.c b/tools/shell/src/shellTire.c index 346757b76f..0628570904 100644 --- a/tools/shell/src/shellTire.c +++ b/tools/shell/src/shellTire.c @@ -309,27 +309,24 @@ void matchPrefixFromTree(STire* tire, char* prefix, SMatch* match) { } SMatch* matchPrefix(STire* tire, char* prefix, SMatch* match) { - if (match == NULL) { - match = (SMatch*)taosMemoryMalloc(sizeof(SMatch)); - memset(match, 0, sizeof(SMatch)); + SMatch* rMatch = match; // define return match + if (rMatch == NULL) { + rMatch = (SMatch*)taosMemoryMalloc(sizeof(SMatch)); + memset(rMatch, 0, sizeof(SMatch)); } switch (tire->type) { case TIRE_TREE: - matchPrefixFromTree(tire, prefix, match); + matchPrefixFromTree(tire, prefix, rMatch); + break; case TIRE_LIST: - matchPrefixFromList(tire, prefix, match); + matchPrefixFromList(tire, prefix, rMatch); + break; default: break; } - // return if need - if (match->count == 0) { - freeMatch(match); - match = NULL; - } - - return match; + return rMatch; } // get all items from tires tree @@ -378,8 +375,10 @@ SMatch* enumAll(STire* tire) { switch (tire->type) { case TIRE_TREE: enumFromTree(tire, match); + break; case TIRE_LIST: enumFromList(tire, match); + break; default: break; } -- GitLab