Skip to content

libsimlin: use-after-free in integration-test error reporting (message read after simlin_error_free) #898

Closed

Description

@bpowers

Problem

Roughly 30 sites across the libsimlin integration tests use this error-reporting boilerplate:

let msg_ptr = simlin_error_get_message(err);
let msg = if msg_ptr.is_null() { "" } else { CStr::from_ptr(msg_ptr).to_str().unwrap() };
simlin_error_free(err);
panic!("... failed with error {:?}: {}", code, msg);

msg is a &str borrowing the error's C string, which simlin_error_free(err) frees before the panic! formats it. This is undefined behavior, and in practice a failing test prints garbage instead of the diagnostic (observed: failed with error BadOverride: <garbage bytes>), which actively misleads whoever is debugging the failure.

Confirmed site counts for the exact borrow-free-panic shape (variants with unwrap_or(\"\") add a few more):

  • src/libsimlin/tests/integration/project.rs: 13
  • src/libsimlin/tests/integration/simulation.rs: 5+ (e.g. the get_value helper around lines 604-611)
  • src/libsimlin/tests/integration/analysis.rs: 4
  • src/libsimlin/tests/integration/serialization.rs: 3
  • src/libsimlin/tests/integration/model.rs: 1
  • src/libsimlin/tests/integration/patch.rs: 1

Additional occurrences of the same idiom exist in errors.rs and wasm.rs with slightly different surrounding shapes.

Correct pattern (already exists)

src/libsimlin/tests/integration/simulation.rs now has correct helpers -- expect_no_error (line 688) and expect_error_code (line 704) -- that copy the message into an owned String BEFORE calling simlin_error_free. But even simulation.rs still has older inline sites that predate the helpers and keep the buggy borrow.

Suggested fix

Hoist the safe helpers into src/libsimlin/tests/integration/common/mod.rs (owned-String copy before free), then migrate all inline sites across errors.rs, analysis.rs, patch.rs, model.rs, wasm.rs, project.rs, serialization.rs, and the remaining simulation.rs sites to use them. This both removes the UB and deduplicates ~30 copies of the boilerplate.

Context

Identified while fixing the stale test_set_value_phases test on the conveyor-engine branch (PR #869 follow-up work). Related: #897 tracks the silent fixture-skip guards discovered in the same pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions