Skip to content

Fix invalid negative index usage when mutating last byte#147

Open
0x0v4 wants to merge 1 commit intomagicblock-labs:mainfrom
0x0v4:0x0v4-patch-1
Open

Fix invalid negative index usage when mutating last byte#147
0x0v4 wants to merge 1 commit intomagicblock-labs:mainfrom
0x0v4:0x0v4-patch-1

Conversation

@0x0v4
Copy link

@0x0v4 0x0v4 commented Feb 26, 2026

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:

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.

Summary by CodeRabbit

  • Tests

    • Improved test reliability and robustness with safer array handling.
  • Refactor

    • Enhanced code structure and variable handling for better maintainability.

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.
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 95ca88d and c2c87e4.

📒 Files selected for processing (1)
  • tests/integration/tests/test-delegation.ts

Walkthrough

This change fixes invalid array indexing in a test file by replacing negative index access with a properly calculated last index variable. Additionally, variable declarations were updated from let to const for immutability, and blank lines were added for improved readability across three similar code blocks.

Changes

Cohort / File(s) Summary
Array Indexing Fix & Code Cleanup
tests/integration/tests/test-delegation.ts
Replaced invalid array indexing using [-1] with safe lastIndex calculation; converted let declarations to const for account and new_data variables; added blank lines for readability in three "Commit a new state to the PDA" blocks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main change: fixing invalid negative index usage when mutating the last byte, which is the core issue addressed in the pull request.
Description check ✅ Passed The description provides a clear problem statement, explains the root cause, details the solution with code examples, and justifies the changes. It does not follow the template structure (Status table, Before/After screenshots, Deploy Notes) but contains substantive, complete information about the changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant