Skip to content

Bug: str→C-string conversion ignores interior NUL (§16.3c) #426

Closed

Description

@ehartford

Summary

Safe str to C-string adaptation must never silently truncate at an interior NUL. Today the runtime helper copies bytes and appends a terminator, so "abc\0def" reaches C as "abc" with no compile-time or runtime complaint. This is a safety and interop correctness bug: modeled C strings are supposed to automate ceremony, not hide poison-NUL behavior.

Spec

§16.3c says str may be auto-coerced to an input *const c_char only when the binding proves a read-only NUL-terminated input string contract, no retention after the call, and "the value has no interior NUL, or the conversion handles one loudly" (docs/with-specification.md:8370-8376). It further requires that "Interior NUL is never silently truncated": a proven interior NUL is a compile error; a dynamic interior NUL is checked at runtime and reported through the binding error model (docs/with-specification.md:8383-8386). §16.3c also forbids any safe conversion that silently truncates at an interior NUL (docs/with-specification.md:8432-8436).

Current state

  • rt/rt_core.w:601-606 implements str_to_cstr by allocating s.len() + 1, rt_memcpying all bytes, and writing a terminator. It does not scan for interior zero bytes before returning the C pointer.
  • rt/rt_core.w:608-609 exposes the same behavior as with_str_to_cstr.
  • Internal runtime/compiler wrappers use str_to_cstr for environment and filesystem paths, for example rt/rt_core.w:1543, rt/rt_core.w:2213, and many path helpers through rt/rt_core.w:2315. Those are also vulnerable to C-observed truncation if a With str contains NUL.
  • rt/rt_core.w:230-236 has a cstr_len reader for C strings, but there is no corresponding str_contains_nul check before conversion.
  • test/behavior/behav_string_escapes.w:44-45 already proves With strings may contain \0, so this is a reachable user value, not an impossible state.
  • test/compile_errors/err_c_import_str_to_c_char_requires_contract.w covers missing contract metadata, not interior-NUL handling once a conversion is modeled.

Implementation plan

  1. Add a small runtime scanner in rt/rt_core.w, for example fn str_has_interior_nul(s: str) -> bool, using str_data(s)/str_length(s) and byte iteration.
  2. Split conversion entry points by error model instead of one silent helper:
    • compiler/runtime internal syscall wrappers should fail loudly with a panic or diagnostic-grade stderr message before calling C when an interior NUL is found;
    • future modeled c_import safe bindings should lower through a checked adapter that can report through the binding's declared error model when one exists.
  3. Update str_to_cstr or replace it with checked_str_to_cstr so every safe call-scoped C-string conversion goes through the scan. Do not leave the unchecked helper available for safe paths with an easy name.
  4. Add compile-time literal detection in sema/lowering for values provably known to contain NUL. A string literal such as "abc\0def" passed to a modeled input C-string parameter must produce a compile error before codegen. If full constant propagation is too large, implement the literal case first and leave dynamic values to runtime checks.
  5. Ensure explicitly raw operations remain raw: passing str_data(s) as *const c_char or manually constructing a pointer in unsafe is not this safe adapter.
  6. Runtime files are seed-sensitive under AGENTS.md. Land runtime-only changes in their own logical change and run the full bootstrap/fixpoint/test gate before any Link.w or seed work.

Test plan

  • Compile-error test for a modeled C-string boundary with a proven NUL literal, e.g. test/compile_errors/err_c_import_str_to_cstr_interior_nul_literal.w, expecting a diagnostic containing interior NUL.
  • Runtime behavior test for a dynamic string containing NUL passed through a modeled safe C-string adapter. The test should assert a loud failure (//! expect-exit and //! expect-stderr: interior NUL) rather than silent truncation.
  • Internal runtime regression test for a filesystem/env path containing \0 if the harness can exercise it without depending on platform-specific errno text.
  • Existing C-string literal tests such as test/spec/spec_ss15_3_c_string_literals.w must continue to pass; c"..." is caller-managed C-string storage and should reject or encode NUL according to the CStr literal rules separately.

Acceptance criteria

  • No safe str to input C-string conversion silently truncates at an interior NUL
  • Proven literal interior NUL at a modeled C-string boundary is a compile error
  • Dynamic interior NUL at a modeled C-string boundary fails loudly at runtime or through the binding error model
  • Raw pointer interop remains explicit and unsafe
  • with build && with build :fixpoint && with build :test pass

Dependencies

Overlaps #427 for the public CString/CStr API surface. Related to #449 and #419 only insofar as all modeled FFI/runtime contracts need explicit effect/lifetime metadata.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 16.3.4.13, 16.3.4.14, 16.3.4.15, 16.3.4.38, 16.3.4.9.

Activity

  1. ehartford commented on Jun 17, 2026

    @ehartford
    ContributorAuthor

    Implemented in 8ac8fbd.

    Runtime (rt/rt_core.w): added str_has_interior_nul and gated str_to_cstr (which backs with_str_to_cstr/the str→*const c_char codegen adapter and all internal env/fs path wrappers) on it. A dynamic interior NUL now fails loudly via with_panic_core (exit 1, interior NUL on stderr) instead of producing a truncated C string. Raw interop (str_data(s) as *const c_char) is unaffected and stays explicit/unsafe.

    Sema (src/SemaCheck.w): a string literal provably containing a NUL, passed to a *const c_char param of an extern/c_import function, is now a compile error (sema_type_is_c_char_pointer + string_literal_has_interior_nul decode-and-scan), caught before codegen.

    Acceptance criteria

    • No safe str→input C-string conversion silently truncates at an interior NUL
    • Proven literal interior NUL at a modeled C-string boundary is a compile error
    • Dynamic interior NUL fails loudly at runtime
    • Raw pointer interop remains explicit/unsafe (unchanged)
    • with build / :fixpoint / tests pass

    Tests: err_c_import_str_to_cstr_interior_nul_literal (literal → compile error), behav_str_to_cstr_interior_nul_panics (dynamic → loud panic). Suites: native-compile-error-tests 598, behavior-tests 688, native-spec-tests 170, fixpoint holds.

    Note: the public CString/CStr API surface (#427) can route through this same checked adapter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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