diff --git a/client/src/fileutils.c b/client/src/fileutils.c index 4b397013e1..dbb8c8b275 100644 --- a/client/src/fileutils.c +++ b/client/src/fileutils.c @@ -1157,7 +1157,8 @@ int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, siz uint8_t block[MFBLOCK_SIZE] = {0}; // ensure zero-filled when partial block of data read JsonLoadBufAsHex(root, blocks, block, MFBLOCK_SIZE, &len); if (!len) { - PrintAndLogEx(WARNING, "WARNING: json %s block %d has zero-length data ... file parsing stopped", ctype, i); + PrintAndLogEx(WARNING, "WARNING: json %s block %d has zero-length data", ctype, i); + PrintAndLogEx(INFO, "file parsing stopped"); break; } else if (len != MFBLOCK_SIZE) { PrintAndLogEx(WARNING, "WARNING: json %s block %d only has %d bytes, expected %d (will fill with zero data)", ctype, i, len, MFBLOCK_SIZE); @@ -1182,21 +1183,27 @@ int loadFileJSONex(const char *preferredName, void *data, size_t maxdatalen, siz *datalen = MFU_DUMP_PREFIX_LENGTH; size_t sptr = 0; - for (int i = 0; i < maxdatalen; i++) { - + // load blocks (i) from 0..N, but check sptr against total data length, not `i` + for (int i = 0; sptr < maxdatalen; i++) { if (sptr + MFBLOCK_SIZE > maxdatalen) { + PrintAndLogEx(ERR, "loadFileJSONex: maxdatalen=%4d (%04x) block (i)=%4d (%04x) sptr=%4d (%04x) -- exceeded maxdatalen", maxdatalen, maxdatalen, i, i, sptr, sptr); retval = PM3_EMALLOC; goto out; } snprintf(blocks, sizeof(blocks), "$.blocks.%d", i); - uint8_t block[MFBLOCK_SIZE]; + uint8_t block[MFBLOCK_SIZE] = {0}; // ensure zero-filled when partial block of data read JsonLoadBufAsHex(root, blocks, block, MFBLOCK_SIZE, &len); - if (!len) + if (!len) { + PrintAndLogEx(WARNING, "WARNING: json %s block %d has zero-length data", ctype, i); + PrintAndLogEx(INFO, "file parsing stopped"); break; + } else if (len != MFBLOCK_SIZE) { + PrintAndLogEx(WARNING, "WARNING: json %s block %d only has %d bytes, expected %d (will fill with zero data)", ctype, i, len, MFBLOCK_SIZE); + } memcpy(&udata.bytes[sptr], block, MFBLOCK_SIZE); - sptr += len; + sptr += MFBLOCK_SIZE; // always increment pointer by the full block size, even if only partial data read from dump file } *datalen = sptr;