Bounds-check the BBList API: bad indices fail cleanly instead of via UB#73
Merged
Conversation
The BBList runtime (_bbVector* in basic.cpp, exposed as ListAt/ListInsert/ ListRemove/ListReplace/ListFirst/ListLast) did no index or emptiness validation on caller-supplied indices. From ordinary Blitz code this was reachable UB / memory corruption: - ListInsert/Remove/Replace computed begin()+idx with no range check -> invalid iterator -> UB. - ListFirst/ListLast called front()/back() on a possibly-empty vector -> UB. - ListAt called vec.at(idx), whose std::out_of_range escaped to the top-level bbruntime_run catch and terminated the WHOLE program with an STL string. Add listIndexOk()/listNotEmpty() guards mirroring the established bank/stream debug-vs-errorLog idiom (bbbank.cpp debugBank): in a debug build a bad index raises a clean Blitz RuntimeError; otherwise it is logged and the call no-ops / returns a safe default. ListInsert allows the append position (idx == size) and rejects idx > size. Happy-path behaviour is unchanged. BBList is a modern BlitzForge API (no legacy-Blitz3D compat risk) and is used heavily by downstream rcce2. Advances INTENT value #6 (trust the runtime). tests/ListTest.bb: add bounds/emptiness blocks — out-of-range ListAt returns Null, Remove/Replace are no-ops with size unchanged, First/Last on an empty list return Null, and ListInsert at idx==size appends while idx>size no-ops. The blocks complete (rather than aborting the run), which is the regression guard: pre-fix the out-of-range ListAt crashed the whole process. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Root cause, the bank/stream guard idiom mirrored, why -t exercises the soft-fail path, and deferred follow-ups (seTranslator break fix; compiler diagnostic snippet). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced May 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Non-technical summary
BBList(the modern dynamic-list type, used heavily by rcce2) trusted whatever index you handed it. Pass an out-of-range or negative index — or callListFirst/ListLaston an empty list — and the runtime hit undefined behaviour / heap corruption, or in the case ofListAt, killed the entire program with an opaque C++ error. This PR adds bounds checks so bad indices fail cleanly: a clear runtime error in debug builds, a safe no-op otherwise — never memory corruption. Advances INTENT value #6 ("Trust the runtime").Technical summary
In
src/blitzrc/bbruntime/basic.cpp, the_bbVector*functions did no validation:ListInsert/ListRemove/ListReplace→begin()+idxwith no range check (invalid iterator → UB).ListFirst/ListLast→front()/back()on a possibly-empty vector (UB).ListAt→vec.at(idx), whosestd::out_of_rangeescaped to the top-levelbbruntime_runcatch and terminated the whole program with an STL string.Added two inline guards —
listIndexOk()andlistNotEmpty()— mirroring the established bank/stream idiom (bbbank.cppdebugBank):debug ? RTEX(clean message) : errorLog + safe default. Applied to_bbVectorAt(now[]after the check),_bbVectorBack/_bbVectorFirst,_bbVectorRemove/_bbVectorReplace, and_bbVectorInsert(which correctly allows the append positionidx == sizeand rejectsidx > size). Happy-path behaviour is byte-identical. BBList is a BlitzForge-specific API, so there's no legacy-Blitz3D compatibility risk.Acceptance criteria + results
ListAtreturnsNulland the program keeps running (pre-fix it crashed the whole run) — newtestListAtOutOfRange.ListFirst/ListLaston an empty list returnNull, no UB —testListFirstLastEmpty.ListRemove/ListReplaceare no-ops, size + contents unchanged —testListRemoveReplaceOutOfRange.ListInsertacceptsidx == ListSize(append) and rejectsidx > ListSize/ negative —testListInsertBoundary.ListTest.bbhappy-path blocks pass unmodified; fulltest.batgreen; local build 0 errors; corpus sweep unaffected.Note: the new tests run in
-tmode wheredebugis false, so they exercise the soft-fail (no-op + safe default) path; the blocks completing is the regression guard, since the pre-fixListAtout-of-range aborted the process. In a-ddebug build the same inputs raise a clean BlitzRuntimeError.Trade-offs, deferred follow-ups
_bbVectorCleararg-count/shadow quirk (basic.cpp:882: rtSym declares one arg, C fn takes two; innerforshadows the unused param) — harmless, pre-existing; flagged, not touched.seTranslatormissing-breakfix (bbruntime_dll.cpp— mislabels every native crash; verification path now known) and human-readable compiler diagnostics (source line + caret, sanitize EOF byte).🤖 Generated with Claude Code