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

This commit is contained in:
iceman1001 2022-01-10 22:29:57 +01:00
commit 221a06321b
3 changed files with 19 additions and 13 deletions

View file

@ -3830,7 +3830,7 @@ static int CmdHFiClassEncode(const char *Cmd) {
wiegand_message_t packed; wiegand_message_t packed;
memset(&packed, 0, sizeof(wiegand_message_t)); memset(&packed, 0, sizeof(wiegand_message_t));
int format_idx = HIDFindCardFormat((char *)format); int format_idx = HIDFindCardFormat(format);
if (format_idx == -1) { if (format_idx == -1) {
PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format);
return PM3_EINVARG; return PM3_EINVARG;

View file

@ -279,7 +279,7 @@ static int CmdHIDSim(const char *Cmd) {
memset(&packed, 0, sizeof(wiegand_message_t)); memset(&packed, 0, sizeof(wiegand_message_t));
// format validation // format validation
int format_idx = HIDFindCardFormat((char *)format); int format_idx = HIDFindCardFormat(format);
if (format_idx == -1 && raw_len == 0) { if (format_idx == -1 && raw_len == 0) {
PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format);
return PM3_EINVARG; return PM3_EINVARG;
@ -383,7 +383,7 @@ static int CmdHIDClone(const char *Cmd) {
memset(&packed, 0, sizeof(wiegand_message_t)); memset(&packed, 0, sizeof(wiegand_message_t));
// format validation // format validation
int format_idx = HIDFindCardFormat((char *)format); int format_idx = HIDFindCardFormat(format);
if (format_idx == -1 && raw_len == 0) { if (format_idx == -1 && raw_len == 0) {
PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format);
return PM3_EINVARG; return PM3_EINVARG;
@ -509,7 +509,7 @@ static int CmdHIDBrute(const char *Cmd) {
formatLen = sizeof(format); formatLen = sizeof(format);
CLIGetStrWithReturn(ctx, 2, format, &formatLen); CLIGetStrWithReturn(ctx, 2, format, &formatLen);
format_idx = HIDFindCardFormat((char *) format); format_idx = HIDFindCardFormat(format);
if (format_idx == -1) { if (format_idx == -1) {
PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format); PrintAndLogEx(WARNING, "Unknown format: " _YELLOW_("%s"), format);
CLIParserFree(ctx); CLIParserFree(ctx);

View file

@ -1391,7 +1391,7 @@ void HIDListFormats(void) {
++i; ++i;
} }
PrintAndLogEx(INFO, "------------------------------------------------------------"); PrintAndLogEx(INFO, "------------------------------------------------------------");
PrintAndLogEx(INFO, "Available card formats: " _YELLOW_("%" PRIu64), ARRAYLEN(FormatTable)); PrintAndLogEx(INFO, "Available card formats: " _YELLOW_("%" PRIu64), ARRAYLEN(FormatTable) - 1);
PrintAndLogEx(NORMAL, ""); PrintAndLogEx(NORMAL, "");
return; return;
} }
@ -1402,27 +1402,33 @@ cardformat_t HIDGetCardFormat(int idx) {
int HIDFindCardFormat(const char *format) { int HIDFindCardFormat(const char *format) {
if (FormatTable[0].Name == NULL) char* s = str_dup(format);
return -1; str_lower(s);
int i = 0; 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; ++i;
} }
if (FormatTable[i].Name) free(s);
return i;
return -1; return -1;
} }
bool HIDPack(int format_idx, wiegand_card_t *card, wiegand_message_t *packed, bool preamble) { bool HIDPack(int format_idx, wiegand_card_t *card, wiegand_message_t *packed, bool preamble) {
memset(packed, 0, sizeof(wiegand_message_t)); 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 false;
return FormatTable[format_idx].Pack(card, packed, preamble); return FormatTable[format_idx].Pack(card, packed, preamble);