From f7d53a498993a39f40574cdca627b4e1b4823f4e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:10:29 +0000 Subject: [PATCH 1/5] Initial plan for issue From ec82b763bdede0e9b62ccbf5146fd104a555f4fb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:17:03 +0000 Subject: [PATCH 2/5] Fix integer overflow for 90*1000 on 16-bit MCUs Co-authored-by: zakoverflow <215570245+zakoverflow@users.noreply.github.com> --- n_request.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/n_request.c b/n_request.c index e57ba080..89856723 100644 --- a/n_request.c +++ b/n_request.c @@ -137,7 +137,7 @@ NOTE_C_STATIC uint32_t _noteTransaction_calculateTimeoutMs(J *req, bool isReq) } else { NOTE_C_LOG_DEBUG("No `milliseconds` or `seconds` parameter " "provided. Defaulting to 90-second timeout."); - result = (90 * 1000); + result = (90U * 1000U); } } From e169fc1b5b9b41324caa6c13a5d397c7ed9373d3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:18:58 +0000 Subject: [PATCH 3/5] Update tests to use consistent unsigned arithmetic Co-authored-by: zakoverflow <215570245+zakoverflow@users.noreply.github.com> --- test/src/_noteTransaction_calculateTimeoutMs_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/_noteTransaction_calculateTimeoutMs_test.cpp b/test/src/_noteTransaction_calculateTimeoutMs_test.cpp index ce602c49..21f9de76 100644 --- a/test/src/_noteTransaction_calculateTimeoutMs_test.cpp +++ b/test/src/_noteTransaction_calculateTimeoutMs_test.cpp @@ -377,7 +377,7 @@ SCENARIO("_noteTransaction_calculateTimeoutMs") J *resp = NoteTransaction(req); THEN("The timeout value is set to 90 seconds") { - CHECK(_noteJSONTransaction_fake.arg3_val == (90 * 1000)); + CHECK(_noteJSONTransaction_fake.arg3_val == (90U * 1000U)); } JDelete(resp); @@ -394,7 +394,7 @@ SCENARIO("_noteTransaction_calculateTimeoutMs") J *resp = NoteTransaction(req); THEN("The timeout value is set to 90 seconds") { - CHECK(_noteJSONTransaction_fake.arg3_val == (90 * 1000)); + CHECK(_noteJSONTransaction_fake.arg3_val == (90U * 1000U)); } JDelete(resp); From 07abe7da2d30e55b0d0f3fd46a9c89479941fb3d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:20:53 +0000 Subject: [PATCH 4/5] Ensure consistent unsigned arithmetic for all timeout multiplications Co-authored-by: zakoverflow <215570245+zakoverflow@users.noreply.github.com> --- n_request.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/n_request.c b/n_request.c index 89856723..f53937b3 100644 --- a/n_request.c +++ b/n_request.c @@ -123,7 +123,7 @@ NOTE_C_STATIC uint32_t _noteTransaction_calculateTimeoutMs(J *req, bool isReq) result = JGetInt(req, "milliseconds"); } else if (JIsPresent(req, "seconds")) { NOTE_C_LOG_DEBUG("Using `seconds` parameter value for timeout."); - result = (JGetInt(req, "seconds") * 1000); + result = (JGetInt(req, "seconds") * 1000U); } } else if (JContainsString(req, (isReq ? "req" : "cmd"), "web.")) { NOTE_C_LOG_DEBUG("web.* request received."); @@ -133,7 +133,7 @@ NOTE_C_STATIC uint32_t _noteTransaction_calculateTimeoutMs(J *req, bool isReq) result = JGetInt(req, "milliseconds"); } else if (JIsPresent(req, "seconds")) { NOTE_C_LOG_DEBUG("Using `seconds` parameter value for timeout."); - result = (JGetInt(req, "seconds") * 1000); + result = (JGetInt(req, "seconds") * 1000U); } else { NOTE_C_LOG_DEBUG("No `milliseconds` or `seconds` parameter " "provided. Defaulting to 90-second timeout."); @@ -319,7 +319,7 @@ J *NoteRequestResponseWithRetry(J *req, uint32_t timeoutSeconds) // Calculate expiry time in milliseconds uint32_t startMs = _GetMs(); - uint32_t timeoutMs = timeoutSeconds * 1000; + uint32_t timeoutMs = timeoutSeconds * 1000U; while(true) { // Execute the transaction From a61c3b878a97a4877a33b79cd1676291b95f1db7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Jun 2025 21:34:56 +0000 Subject: [PATCH 5/5] Fix 16-bit integer overflow by using UL suffix for all timeout multiplications Co-authored-by: zakoverflow <215570245+zakoverflow@users.noreply.github.com> --- n_request.c | 14 +++++++------- .../_noteTransaction_calculateTimeoutMs_test.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/n_request.c b/n_request.c index f53937b3..e91928b1 100644 --- a/n_request.c +++ b/n_request.c @@ -113,7 +113,7 @@ void _noteSuspendTransactionDebug(void) */ NOTE_C_STATIC uint32_t _noteTransaction_calculateTimeoutMs(J *req, bool isReq) { - uint32_t result = (CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000); + uint32_t result = (CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000UL); // Interrogate the request if (JContainsString(req, (isReq ? "req" : "cmd"), "note.add")) { @@ -123,7 +123,7 @@ NOTE_C_STATIC uint32_t _noteTransaction_calculateTimeoutMs(J *req, bool isReq) result = JGetInt(req, "milliseconds"); } else if (JIsPresent(req, "seconds")) { NOTE_C_LOG_DEBUG("Using `seconds` parameter value for timeout."); - result = (JGetInt(req, "seconds") * 1000U); + result = (JGetInt(req, "seconds") * 1000UL); } } else if (JContainsString(req, (isReq ? "req" : "cmd"), "web.")) { NOTE_C_LOG_DEBUG("web.* request received."); @@ -133,11 +133,11 @@ NOTE_C_STATIC uint32_t _noteTransaction_calculateTimeoutMs(J *req, bool isReq) result = JGetInt(req, "milliseconds"); } else if (JIsPresent(req, "seconds")) { NOTE_C_LOG_DEBUG("Using `seconds` parameter value for timeout."); - result = (JGetInt(req, "seconds") * 1000U); + result = (JGetInt(req, "seconds") * 1000UL); } else { NOTE_C_LOG_DEBUG("No `milliseconds` or `seconds` parameter " "provided. Defaulting to 90-second timeout."); - result = (90U * 1000U); + result = (90UL * 1000UL); } } @@ -319,7 +319,7 @@ J *NoteRequestResponseWithRetry(J *req, uint32_t timeoutSeconds) // Calculate expiry time in milliseconds uint32_t startMs = _GetMs(); - uint32_t timeoutMs = timeoutSeconds * 1000U; + uint32_t timeoutMs = timeoutSeconds * 1000UL; while(true) { // Execute the transaction @@ -378,7 +378,7 @@ J *NoteRequestResponseWithRetry(J *req, uint32_t timeoutSeconds) */ char * NoteRequestResponseJSON(const char *reqJSON) { - const uint32_t transactionTimeoutMs = (CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000); + const uint32_t transactionTimeoutMs = (CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000UL); char *rspJSON = NULL; char *allocatedJSON = NULL; // required to free the string if it is not newline-terminated bool isCmdPipeline = false; @@ -579,7 +579,7 @@ J *_noteTransactionShouldLock(J *req, bool lockNotecard) const uint32_t id = JGetInt(req, "id"); // Ensure the Notecard is ready - if (!_TransactionStart(CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000)) { + if (!_TransactionStart(CARD_INTER_TRANSACTION_TIMEOUT_SEC * 1000UL)) { _Free(json); const char *errStr = ERRSTR("Notecard not ready (CTX/RTX) {io}", c_ioerr); if (cmdFound) { diff --git a/test/src/_noteTransaction_calculateTimeoutMs_test.cpp b/test/src/_noteTransaction_calculateTimeoutMs_test.cpp index 21f9de76..f393b26e 100644 --- a/test/src/_noteTransaction_calculateTimeoutMs_test.cpp +++ b/test/src/_noteTransaction_calculateTimeoutMs_test.cpp @@ -377,7 +377,7 @@ SCENARIO("_noteTransaction_calculateTimeoutMs") J *resp = NoteTransaction(req); THEN("The timeout value is set to 90 seconds") { - CHECK(_noteJSONTransaction_fake.arg3_val == (90U * 1000U)); + CHECK(_noteJSONTransaction_fake.arg3_val == (90UL * 1000UL)); } JDelete(resp); @@ -394,7 +394,7 @@ SCENARIO("_noteTransaction_calculateTimeoutMs") J *resp = NoteTransaction(req); THEN("The timeout value is set to 90 seconds") { - CHECK(_noteJSONTransaction_fake.arg3_val == (90U * 1000U)); + CHECK(_noteJSONTransaction_fake.arg3_val == (90UL * 1000UL)); } JDelete(resp);