Skip to content

Potential fix for code scanning alert no. 11: Access of invalid pointer#104

Merged
gsleap merged 1 commit into
mainfrom
alert-autofix-11
Apr 1, 2026
Merged

Potential fix for code scanning alert no. 11: Access of invalid pointer#104
gsleap merged 1 commit into
mainfrom
alert-autofix-11

Conversation

@gsleap
Copy link
Copy Markdown
Member

@gsleap gsleap commented Mar 31, 2026

Potential fix for https://github.com/MWATelescope/mwalib/security/code-scanning/11

In general, to fix this kind of problem you must ensure that any pointer obtained via FFI is validated in the same scope where it is dereferenced, and that code paths where the pointer might still be null or invalid are excluded before dereference (e.g., by returning early, panicking, or otherwise aborting). Assertions in a different unsafe block are not enough for static analysis and are fragile if the FFI function misbehaves.

The best targeted fix here, without changing functionality, is:

  1. Keep the existing call to mwalib_voltage_metadata_get but treat non‑zero return values as fatal by panicking with the error message. This keeps tests strict and clarifies that subsequent code assumes success.
  2. Immediately after the call, check voltage_metadata_ptr for null again (inside the same function, in safe Rust) and panic if it is null. This reinforces the contract and avoids undefined behavior if the FFI function erroneously returns success with a null pointer.
  3. When computing buffer_len, avoid dereferencing voltage_metadata_ptr directly; instead, bind a temporary reference let voltage_metadata = &*voltage_metadata_ptr; after the null check and use that to access fields. That makes it explicit that we only dereference after validating non-null and keeps the unsafe dereference localized.
  4. Optionally, merge the two unsafe blocks around mwalib_voltage_metadata_get and the dereference, or at least add the added null check and reference binding in the same region where the dereference occurs.

We only need to modify the function that contains the shown snippet in src/voltage_context/ffi_test.rs. No new methods or imports are required; we can reuse CString and the standard library. The functional behavior remains the same on success paths; on failure or inconsistent FFI behavior, the test will now panic before dereferencing an invalid pointer.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
unsafe {
// At this point we have asserted that voltage_metadata_ptr is non-null,
// so it is safe to create a reference and use it below.
let voltage_metadata = &*voltage_metadata_ptr;

Check failure

Code scanning / CodeQL

Access of invalid pointer High

This operation dereferences a pointer that may be
invalid
.

Copilot Autofix

AI about 2 months ago

Copilot could not generate an autofix suggestion

Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.

@gsleap gsleap marked this pull request as ready for review April 1, 2026 06:39
@gsleap gsleap merged commit b7e1a6d into main Apr 1, 2026
44 of 45 checks passed
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.

2 participants