counter(native): avoid panic, validate owner, checked_add, fail unknown instructions by yukikm · Pull Request #531 · solana-developers/program-examples

Pull request overview

This PR hardens the Solana-native counter example program to fail safely on malformed instructions and unsafe account inputs, and adds regression tests intended to prevent reintroducing these footguns.

Changes:

  • Avoids panics on-chain by replacing assert! with explicit error returns for readonly accounts.
  • Validates instruction discriminant (including empty data) and rejects unknown instructions.
  • Adds ownership validation and overflow-safe increment (checked_add) in the counter update path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
basics/counter/native/program/src/lib.rs Adds instruction-data validation, unknown-instruction failures, owner + writable checks, and overflow-safe increment.
basics/counter/native/program/tests/test.rs Strengthens the happy-path test and adds new failure-mode tests (unknown instruction + readonly counter).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.