diff --git a/.gitignore b/.gitignore index 627d3dc0d..58057497b 100644 --- a/.gitignore +++ b/.gitignore @@ -122,3 +122,7 @@ fpga_version_info.c # docs !doc/*.json + +# local codeql +_codeql* +/codeql \ No newline at end of file diff --git a/client/src/cmddata.c b/client/src/cmddata.c index cb0870af4..14144c77c 100644 --- a/client/src/cmddata.c +++ b/client/src/cmddata.c @@ -3681,8 +3681,9 @@ static int CmdTestSaveState8(const char *Cmd) { srand(time(NULL)); - size_t length = 64; - uint8_t *srcBuffer = (uint8_t *)calloc(length, sizeof(uint8_t)); + size_t length = (rand() % 256); + PrintAndLogEx(DEBUG, "Testing with length = %llu", length); + uint8_t *srcBuffer = (uint8_t*)calloc(length + 1, sizeof(uint8_t)); //Set up the source buffer with random data for (int i = 0; i < length; i++) { @@ -3690,24 +3691,23 @@ static int CmdTestSaveState8(const char *Cmd) { } buffer_savestate_t test8 = save_buffer8(srcBuffer, length); - PrintAndLogEx(DEBUG, "Save State created, length=%llu, type=%i", test8.bufferSize, test8.type); + PrintAndLogEx(DEBUG, "Save State created, length = %llu, padding = %i, type = %i", test8.bufferSize, test8.padding, test8.type); test8.clock = rand(); test8.offset = rand(); - PrintAndLogEx(DEBUG, "Save State clock=%u, offset=%u", test8.clock, test8.offset); + PrintAndLogEx(DEBUG, "Save State clock = %u, offset = %u", test8.clock, test8.offset); uint8_t *destBuffer = (uint8_t *)calloc(length, sizeof(uint8_t)); size_t returnedLength = restore_buffer8(test8, destBuffer); if (returnedLength != length) { - PrintAndLogEx(FAILED, "Return Length != Buffer Length! Expected '%llu', got '%llu", g_DemodBufferLen, returnedLength); - free(srcBuffer); - free(destBuffer); - return PM3_EFAILED; + PrintAndLogEx(DEBUG, _YELLOW_("Returned length != expected length!")); + PrintAndLogEx(WARNING, "Returned Length = %llu Buffer Length = %llu Expected = %llu", returnedLength, test8.bufferSize, length); + } else { + PrintAndLogEx(DEBUG, _GREEN_("Lengths match!") "\n"); } - PrintAndLogEx(DEBUG, _GREEN_("Lengths match!") "\n"); - - for (size_t i = 0; i < length; i++) { + + for (size_t i = 0; i < returnedLength; i++) { if (srcBuffer[i] != destBuffer[i]) { PrintAndLogEx(FAILED, "Buffers don't match at index %lu!, Expected %i, got %i", i, srcBuffer[i], destBuffer[i]); free(srcBuffer); diff --git a/client/src/graph.c b/client/src/graph.c index f0e1a67bd..50b31abf1 100644 --- a/client/src/graph.c +++ b/client/src/graph.c @@ -519,7 +519,8 @@ buffer_savestate_t save_buffer32(uint32_t *src, size_t length) { buffer_savestate_t bst = { .type = sizeof(uint32_t), .bufferSize = length, - .buffer = savedBuffer + .buffer = savedBuffer, + .padding = 0 }; return bst; @@ -535,7 +536,8 @@ buffer_savestate_t save_bufferS32(int32_t *src, size_t length) { buffer_savestate_t bst = { .type = (sizeof(int32_t) >> 8), .bufferSize = length, - .buffer = savedBuffer + .buffer = savedBuffer, + .padding = 0 }; return bst; @@ -547,8 +549,11 @@ buffer_savestate_t save_buffer8(uint8_t *src, size_t length) { // 1/4 of the size needed size_t buffSize = (length / 4); + PrintAndLogEx(DEBUG, "(save_buffer8) buffSize = %llu, length = %llu", buffSize, length); + if (length % 4) { buffSize++; + PrintAndLogEx(DEBUG, "(save_buffer8) new buffSize = %llu", buffSize); } // calloc the memory needed @@ -564,7 +569,8 @@ buffer_savestate_t save_buffer8(uint8_t *src, size_t length) { buffer_savestate_t bst = { .type = sizeof(uint8_t), .bufferSize = buffSize, - .buffer = savedBuffer + .buffer = savedBuffer, + .padding = ((buffSize * 4) - length) }; return bst; @@ -602,13 +608,18 @@ size_t restore_buffer8(buffer_savestate_t saveState, uint8_t *dest) { } size_t index = 0; + size_t length = ((saveState.bufferSize * 4) - saveState.padding); // Unpack the array for (size_t i = 0; i < saveState.bufferSize; i++) { dest[index++] = saveState.buffer[i]; + if(index == length) break; dest[index++] = (saveState.buffer[i] >> 8) & 0xFF; + if(index == length) break; dest[index++] = (saveState.buffer[i] >> 16) & 0xFF; + if(index == length) break; dest[index++] = (saveState.buffer[i] >> 24) & 0xFF; + if(index == length) break; } return index; diff --git a/client/src/graph.h b/client/src/graph.h index b2e02b137..6f0822f96 100644 --- a/client/src/graph.h +++ b/client/src/graph.h @@ -26,11 +26,12 @@ extern "C" { #endif typedef struct { - const uint8_t type; //Used for sanity checks - const uint32_t *buffer; - const size_t bufferSize; - uint32_t offset; - uint32_t clock; //Not used by all buffers + const uint8_t type; // Used for sanity checks + const uint32_t *buffer; // The storage buffer for this save state + const size_t bufferSize; // The size of the buffer + const uint8_t padding; // The amount of padding at the end of the buffer, if needed + uint32_t offset; // (optional) Any offset the buffer needs after restoring + uint32_t clock; // (optional) Clock data for the buffer } buffer_savestate_t; typedef struct {