Refactor 5 FFI test sites to NonNull / as_ref().expect() for clearer non-null contracts#318
Merged
Conversation
…non-null contracts
…quired check always reports
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.
Summary
Cleanup follow-up to #317. Refactors 5 test-code FFI sites where
assert!(!ptr.is_null())+ raw&*ptrderefs were flagged by CodeQL'srust/access-invalid-pointer. Both reviewers (Codex + Pickle) recommend the refactors on code-quality grounds, with the caveat that CodeQL's null-barrier model isif ptr.is_null() {...}-shaped and may not auto-close the alerts even after this PR.The 2
discovery.rsvtable derefs are intentionally not touched -- both reviewers explicitly rejected any rewrite as a code smell (theif !X.is_null() && !Y.is_null() { *X }form is canonical Rust raw-pointer null-guard). Those 2 + any remaining test alerts are for UI dismissal as accepted false positives.What changed
crates/pecos-qis-ffi/src/ffi.rs::test_heap_alloc_and_free:assert!(!ptr.is_null())+ raw*mut u8->NonNull::new(...).expect(...). TheNonNulltype encodes the non-null invariant for a malloc-backed allocation whereas_ref/as_mutwould be the wrong abstraction (uninitialised memory). Codex's specific recommendation.crates/pecos-qis-ffi/src/lib.rs(4 sites:test_pending_operations_storage,test_wait_for_result_ready_with_dynamic_mode, two derefs intest_wait_for_result_ready_exports_only_new_operations):assert!(!ptr.is_null())+unsafe { &*ptr }->unsafe { ptr.as_ref() }.expect(...). Non-null encoded inOption<&T>; safeexpect()panics on null with a descriptive message. Pickle + Codex aligned.Net diff: -5 lines (refactor is genuinely more concise). No behaviour change in the tests' assertion semantics.
Why this isn't "code-to-satisfy-the-linter"
Per CLAUDE.md, refactoring solely to placate a static analyzer is in the same family as suppressing warnings to silence them. The reason these refactors pass that test:
NonNull<T>andOption<&T>are the standard types for "definitely non-null" and "maybe non-null with safe ownership" respectively. Using them instead of raw*mut T+assert!+&*ptris what the Rust FFI ecosystem does.Optionvs the canonicalif !is_null()guard). Cleanup ends where it would start subtracting value.Side observation (not in scope here)
Codex flagged: should plugin discovery treat a plugin that returns exactly one of
handle/vtable(but not both) as a protocol violation (log/reject) rather than silently treating it as "not provided"? A half-populated plugin could leak a foreign handle if the destroy-vtable is absent. Not blocking this PR -- backlog for the plugin-protocol design.Test plan
just lintcleancargo test -p pecos-qis-ffi --lib(89 pass, 0 fail)as_ref()/NonNullthrough stdlib internals). Whatever survives gets UI-dismissed alongside the 2discovery.rssites.Reviews
~/Repos/pecos-docs/reviews/codex-codeql-ffi-fp-resolution.md~/Repos/pecos-docs/reviews/pickle-codeql-ffi-fp-resolution.md