From c2c87e4ad11387c8ffa7ea511ef24c649199416d Mon Sep 17 00:00:00 2001 From: 0x0v4 <20mitchellthomas02@gmail.com> Date: Thu, 26 Feb 2026 09:01:51 +0100 Subject: [PATCH] Fix invalid negative index usage when mutating last byte The tests used new_data[-1] to mutate the last byte of account data. In JavaScript/TypeScript, negative indices are not valid array indices. new_data[-1] evaluates to undefined, resulting in NaN, and creates a "-1" object property instead of modifying the last byte. As a result, the account data was not actually modified, and the commit tests were not validating real state changes. Replaced negative index usage with explicit last-index access: ```ts const lastIndex = new_data.length - 1; new_data[lastIndex] = (new_data[lastIndex] + 1) % 256; ``` Applied this fix in both Commit a new state to the PDA test cases. This: - Ensures the last byte of the buffer is actually mutated - Makes state changes explicit - Keeps changes minimal - Preserves existing test structure and logic This restores correct state mutation semantics and ensures the commit tests validate real data changes. Additionally, changed let to const for account and new_data since they are not reassigned. --- tests/integration/tests/test-delegation.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/integration/tests/test-delegation.ts b/tests/integration/tests/test-delegation.ts index cf7a7e19..210e1116 100644 --- a/tests/integration/tests/test-delegation.ts +++ b/tests/integration/tests/test-delegation.ts @@ -208,9 +208,11 @@ describe("TestDelegation", () => { }); it("Commit a new state to the PDA", async () => { - let account = await provider.connection.getAccountInfo(pda); - let new_data = account.data; - new_data[-1] = (new_data[-1] + 1) % 256; + const account = await provider.connection.getAccountInfo(pda); + const new_data = account.data; + + const lastIndex = new_data.length - 1; + new_data[lastIndex] = (new_data[lastIndex] + 1) % 256; const args: CommitAccountInstructionArgs = { slot: new anchor.BN(1), @@ -316,9 +318,11 @@ describe("TestDelegation", () => { }); it("Commit a new state to the PDA", async () => { - let account = await provider.connection.getAccountInfo(pda); - let new_data = account.data; - new_data[-1] = (new_data[-1] + 1) % 256; + const account = await provider.connection.getAccountInfo(pda); + const new_data = account.data; + + const lastIndex = new_data.length - 1; + new_data[lastIndex] = (new_data[lastIndex] + 1) % 256; const args: CommitAccountInstructionArgs = { slot: new anchor.BN(2),