Fix invalid negative index usage when mutating last byte#147
Fix invalid negative index usage when mutating last byte#1470x0v4 wants to merge 1 commit intomagicblock-labs:mainfrom
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro 📒 Files selected for processing (1)
WalkthroughThis 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 Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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. Comment |
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:
Applied this fix in both Commit a new state to the PDA test cases.
This:
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
Refactor