From 221a06321b8b3009e38e6409073e93d0f338f859 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Mon, 10 Jan 2022 22:29:57 +0100 Subject: [PATCH] cppcheck fix - array out of bounds. last item is null reduce 1 from arr len, zero based index reduce -1 to get index....\n also made the compare caseinsensitive --- client/src/cmdhficlass.c | 2 +- client/src/cmdlfhid.c | 6 +++--- client/src/wiegand_formats.c | 24 +++++++++++++++--------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/client/src/cmdhficlass.c b/client/src/cmdhficlass.c index 15e04a270..e61ea6df1 100644 --- a/client/src/cmdhficlass.c +++ b/client/src/cmdhficlass.c @@ -3830,7 +3830,7 @@ static int CmdHFiClassEncode(const char *Cmd) { wiegand_message_t packed; memset(&packed, 0, sizeof(wiegand_message_t)); - int format_idx = HIDFindCardFormat((char *)format); + int format_idx = HIDFindCardFormat(format); if (format_idx == -1) { PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); return PM3_EINVARG; diff --git a/client/src/cmdlfhid.c b/client/src/cmdlfhid.c index dbb8c3d12..e673f1829 100644 --- a/client/src/cmdlfhid.c +++ b/client/src/cmdlfhid.c @@ -279,7 +279,7 @@ static int CmdHIDSim(const char *Cmd) { memset(&packed, 0, sizeof(wiegand_message_t)); // format validation - int format_idx = HIDFindCardFormat((char *)format); + int format_idx = HIDFindCardFormat(format); if (format_idx == -1 && raw_len == 0) { PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); return PM3_EINVARG; @@ -383,7 +383,7 @@ static int CmdHIDClone(const char *Cmd) { memset(&packed, 0, sizeof(wiegand_message_t)); // format validation - int format_idx = HIDFindCardFormat((char *)format); + int format_idx = HIDFindCardFormat(format); if (format_idx == -1 && raw_len == 0) { PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); return PM3_EINVARG; @@ -509,7 +509,7 @@ static int CmdHIDBrute(const char *Cmd) { formatLen = sizeof(format); CLIGetStrWithReturn(ctx, 2, format, &formatLen); - format_idx = HIDFindCardFormat((char *) format); + format_idx = HIDFindCardFormat(format); if (format_idx == -1) { PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); CLIParserFree(ctx); diff --git a/client/src/wiegand_formats.c b/client/src/wiegand_formats.c index c7cf1d21b..ae29f72f3 100644 --- a/client/src/wiegand_formats.c +++ b/client/src/wiegand_formats.c @@ -1391,7 +1391,7 @@ void HIDListFormats(void) { ++i; } PrintAndLogEx(INFO, "------------------------------------------------------------"); - PrintAndLogEx(INFO, "Available card formats: " _YELLOW_("%" PRIu64), ARRAYLEN(FormatTable)); + PrintAndLogEx(INFO, "Available card formats: " _YELLOW_("%" PRIu64), ARRAYLEN(FormatTable) - 1); PrintAndLogEx(NORMAL, ""); return; } @@ -1402,27 +1402,33 @@ cardformat_t HIDGetCardFormat(int idx) { int HIDFindCardFormat(const char *format) { - if (FormatTable[0].Name == NULL) - return -1; + char* s = str_dup(format); + str_lower(s); int i = 0; + while (FormatTable[i].Name) { -// str_lower + char* a = str_dup(FormatTable[i].Name); + str_lower(a); - while (FormatTable[i].Name && strcmp(FormatTable[i].Name, format)) { + if (strcmp(a, s) == 0) { + free(a); + free(s); + return i; + } + + free(a); ++i; } - if (FormatTable[i].Name) - return i; - + free(s); return -1; } bool HIDPack(int format_idx, wiegand_card_t *card, wiegand_message_t *packed, bool preamble) { memset(packed, 0, sizeof(wiegand_message_t)); - if ((format_idx < 0) || (format_idx >= ARRAYLEN(FormatTable) - 1)) + if ((format_idx < 0) || (format_idx > ARRAYLEN(FormatTable) - 2)) return false; return FormatTable[format_idx].Pack(card, packed, preamble);