Skip to content

Add sanity check in for loops that end-start <= max_iterations#110

Merged
someone235 merged 2 commits intokaspanet:covpp-reset2from
someone235:for-sanity
Apr 28, 2026
Merged

Add sanity check in for loops that end-start <= max_iterations#110
someone235 merged 2 commits intokaspanet:covpp-reset2from
someone235:for-sanity

Conversation

@someone235
Copy link
Copy Markdown
Contributor

For loops now require the declared range size to fit within the compile-time max_iterations bound.

Previously, a runtime-bounded loop could silently execute only the first max_iterations iterations even when end - start was larger. This change makes that mismatch explicit: the
contract rejects the call at runtime for dynamic bounds, and the compiler rejects it immediately when the bounds are compile-time constants.

Language Behavior Changes

  • A for loop is now valid only when:
end - start <= max_iterations
  • Compile-time constant ranges that exceed max_iterations are rejected during compilation:
contract Loops() {
    entrypoint function main() {
        for (i, 0, 4, 3) {
            require(i >= 0);
        }
    }
}
  • Runtime ranges that exceed max_iterations now fail contract execution:
contract RuntimeLoop() {
    entrypoint function main(int start, int end) {
        for (i, start, end, 3) {
            require(i >= start);
        }
    }
}

Calling main(2, 6) fails because 6 - 2 > 3.

  • Empty or reversed ranges remain valid and execute zero iterations:
for (i, 4, 2, 3) {
    require(false);
}

@someone235 someone235 merged commit 2bfa583 into kaspanet:covpp-reset2 Apr 28, 2026
4 checks passed
@someone235 someone235 deleted the for-sanity branch April 28, 2026 08:54
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