From ac6ffb96029e1a1e00df04c2334a3167b89af705 Mon Sep 17 00:00:00 2001 From: prozessorkern Date: Wed, 2 Dec 2020 15:47:23 +0100 Subject: [PATCH] fixed findings from static analysis --- makefile | 17 ++++++-------- src/shellmatta.c | 19 +++++++++------- src/shellmatta_autocomplete.c | 8 +++---- src/shellmatta_opt.h | 12 ---------- src/shellmatta_utils.c | 42 +++++++++++++++++------------------ 5 files changed, 43 insertions(+), 55 deletions(-) diff --git a/makefile b/makefile index e47043a..51b67bd 100644 --- a/makefile +++ b/makefile @@ -92,24 +92,21 @@ cppcheck: unittest: $(UNITTEST_TARGET) - @mkdir -p output/test/unittest/report @echo running test: - @# remove coverage from former run +# remove coverage from former run @-find . -name "*.gcda" -type f -delete -$(UNITTEST_TARGET) - @#gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src +# gcov -o output/test/unittest $(UNITTEST_CPPOBJ) -r src - @# remove report from former run +# remove report from former run -rm -rf $(OBJ_DIR)test/unittest/report/* - gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api - @#-rm *.gcov - + gcovr --html-details --output $(OBJ_DIR)test/unittest/report/report.html output/test/unittest -f src -f api -d + integrationtest: $(INTEGRATIONTEST_TARGET) - @mkdir -p output/test/integrationtest/report @echo running test: -$(INTEGRATIONTEST_TARGET) - #gcov -o output/test $(TEST_CPPOBJ) -r - gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api - #-rm *.gcov - + gcovr --html-details --output $(OBJ_DIR)test/integrationtest/report/report.html output/src -f src -f api -d + example: $(EXAMPLE_TARGET) @echo building example diff --git a/src/shellmatta.c b/src/shellmatta.c index c20a220..4fc8aaa 100644 --- a/src/shellmatta.c +++ b/src/shellmatta.c @@ -427,6 +427,10 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, utils_terminateInput(inst); } } + else + { + /* nothing to do here - continue parsing the command */ + } /** -# process byte wise */ for (; (inst->byteCounter < size) && (NULL == inst->busyCmd); inst->byteCounter++) @@ -597,15 +601,15 @@ shellmatta_retCode_t shellmatta_processData(shellmatta_handle_t handle, while (NULL != cmd) { /** -# compare command and alias string and length */ - if ( ((0 == strncmp( inst->buffer, - cmd->cmd, - cmdLen)) - && (cmdLen == strlen(cmd->cmd))) + if ( ((cmdLen == strlen(cmd->cmd)) + && (0 == strncmp( inst->buffer, + cmd->cmd, + cmdLen))) || ((NULL != cmd->cmdAlias) - && ((0 == strncmp( inst->buffer, + && (cmdLen == strlen(cmd->cmdAlias)) + && (0 == strncmp( inst->buffer, cmd->cmdAlias, - cmdLen)) - && (cmdLen == strlen(cmd->cmdAlias))))) + cmdLen)))) { utils_writeEcho(inst, "\r\n", 2u); shellmatta_opt_init(inst, cmdLen + 1u); @@ -819,4 +823,3 @@ shellmatta_retCode_t shellmatta_printf( shellmatta_handle_t handle, #endif /** @} */ - diff --git a/src/shellmatta_autocomplete.c b/src/shellmatta_autocomplete.c index e7d26c3..560e525 100644 --- a/src/shellmatta_autocomplete.c +++ b/src/shellmatta_autocomplete.c @@ -52,7 +52,7 @@ void autocomplete_run(shellmatta_instance_t *inst) { /** -# check if command matches the input */ if( (strlen(cmd->cmd) >= inst->cursor) - && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor))) + && (0 == strncmp(cmd->cmd, inst->buffer, inst->cursor))) { /** -# add newline on first find */ if(0u == printedLen) @@ -67,7 +67,7 @@ void autocomplete_run(shellmatta_instance_t *inst) /** -# check if command alias matches the input */ if( (NULL != cmd->cmdAlias) && (strlen(cmd->cmdAlias) >= inst->cursor) - && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor))) + && (0 == strncmp(cmd->cmdAlias, inst->buffer, inst->cursor))) { /** -# add newline on first find */ if(0u == printedLen) @@ -100,7 +100,7 @@ void autocomplete_run(shellmatta_instance_t *inst) { /** -# check if command matches the input */ if( (strlen(cmd->cmd) >= inst->cursor) - && (0u == memcmp(cmd->cmd, inst->buffer, inst->cursor))) + && (0 == strncmp(cmd->cmd, inst->buffer, inst->cursor))) { /** -# store first match */ if(NULL == tempCmd) @@ -126,7 +126,7 @@ void autocomplete_run(shellmatta_instance_t *inst) /** -# check if command Alias matches the input */ if( (NULL != cmd->cmdAlias) && (strlen(cmd->cmdAlias) >= inst->cursor) - && (0u == memcmp(cmd->cmdAlias, inst->buffer, inst->cursor))) + && (0 == strncmp(cmd->cmdAlias, inst->buffer, inst->cursor))) { /** -# store first match */ if(NULL == tempCmd) diff --git a/src/shellmatta_opt.h b/src/shellmatta_opt.h index 2f6a14d..6509720 100644 --- a/src/shellmatta_opt.h +++ b/src/shellmatta_opt.h @@ -22,18 +22,6 @@ #include "shellmatta.h" #include -shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle, - const char *optionString, - char *option, - char **argument, - uint32_t *argLen); - -shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle, - const shellmatta_opt_long_t *longOptions, - char *option, - char **argument, - uint32_t *argLen); - void shellmatta_opt_init( shellmatta_instance_t *inst, uint32_t argStart); diff --git a/src/shellmatta_utils.c b/src/shellmatta_utils.c index e67fda6..28bde64 100644 --- a/src/shellmatta_utils.c +++ b/src/shellmatta_utils.c @@ -51,7 +51,7 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base) char tempBuffer[34u]; uint32_t i; uint32_t bufferIdx = 0u; - char digitValue; + int8_t digitValue; /** -# check the base for plausibility */ if((2 <= base) && (16 >= base)) @@ -68,8 +68,8 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base) i = 0u; do { - digitValue = (char) (value % base); - tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : (('A' - 10) + digitValue); + digitValue = (int8_t) (value % base); + tempBuffer[i] = (digitValue < 10) ? ('0' + digitValue) : ('A' + (digitValue - 10)); value /= base; i ++; }while(value > 0); @@ -91,7 +91,7 @@ uint32_t utils_shellItoa(int32_t value, char *buffer, uint32_t base) */ void utils_saveCursorPos(shellmatta_instance_t *inst) { - utils_writeEcho(inst, "\x1b[s", 3u); + utils_writeEcho(inst, "\x1b" "[s", 3u); } /** @@ -100,7 +100,7 @@ void utils_saveCursorPos(shellmatta_instance_t *inst) */ void utils_restoreCursorPos(shellmatta_instance_t *inst) { - utils_writeEcho(inst, "\x1b[u", 3u); + utils_writeEcho(inst, "\x1b" "[u", 3u); } /** @@ -110,7 +110,7 @@ void utils_restoreCursorPos(shellmatta_instance_t *inst) */ void utils_eraseLine(shellmatta_instance_t *inst) { - utils_writeEcho(inst, "\x1b[K", 3u); + utils_writeEcho(inst, "\x1b" "[K", 3u); } /** @@ -275,22 +275,22 @@ void utils_clearInput(shellmatta_instance_t *inst) * @return #SHELLMATTA_OK * #SHELLMATTA_ERROR (buffer overflow) */ -static shellmatta_retCode_t helpCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length) +static shellmatta_retCode_t helpCmdFct(const shellmatta_handle_t handle, const char *arguments, uint32_t length) { - shellmatta_retCode_t ret = SHELLMATTA_OK; - shellmatta_instance_t *inst = (shellmatta_instance_t*) handle; - shellmatta_cmd_t *cmd = inst->cmdList; - size_t maxCmdLen = 0u; - size_t maxCmdAliasLen = 0u; - size_t maxCmdHelpLen = 0u; - size_t cmdLen = 0u; - size_t cmdAliasLen = 0u; - size_t cmdHelpLen = 0u; - uint32_t tabCnt = 0u; - static const char tabBuffer[] = { ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' ', - ' ', ' ', ' ', ' '}; + shellmatta_retCode_t ret = SHELLMATTA_OK; + const shellmatta_instance_t *inst = (const shellmatta_instance_t*) handle; + shellmatta_cmd_t *cmd = inst->cmdList; + size_t maxCmdLen = 0u; + size_t maxCmdAliasLen = 0u; + size_t maxCmdHelpLen = 0u; + size_t cmdLen = 0u; + size_t cmdAliasLen = 0u; + size_t cmdHelpLen = 0u; + uint32_t tabCnt = 0u; + static const char tabBuffer[] = { ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' ', + ' ', ' ', ' ', ' '}; /** -# loop through all commands to determine the lengths of each cmd */ while(NULL != cmd)