Repository navigation
Description
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-606implementsstr_to_cstrby allocatings.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-609exposes the same behavior aswith_str_to_cstr.- Internal runtime/compiler wrappers use
str_to_cstrfor environment and filesystem paths, for examplert/rt_core.w:1543,rt/rt_core.w:2213, and many path helpers throughrt/rt_core.w:2315. Those are also vulnerable to C-observed truncation if a Withstrcontains NUL. rt/rt_core.w:230-236has acstr_lenreader for C strings, but there is no correspondingstr_contains_nulcheck before conversion.test/behavior/behav_string_escapes.w:44-45already 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.wcovers missing contract metadata, not interior-NUL handling once a conversion is modeled.
Implementation plan
- Add a small runtime scanner in
rt/rt_core.w, for examplefn str_has_interior_nul(s: str) -> bool, usingstr_data(s)/str_length(s)and byte iteration. - 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_importsafe bindings should lower through a checked adapter that can report through the binding's declared error model when one exists.
- Update
str_to_cstror replace it withchecked_str_to_cstrso 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. - 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. - Ensure explicitly raw operations remain raw: passing
str_data(s) as *const c_charor manually constructing a pointer inunsafeis not this safe adapter. - 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 containinginterior 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-exitand//! expect-stderr: interior NUL) rather than silent truncation. - Internal runtime regression test for a filesystem/env path containing
\0if 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.wmust 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
strto 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 :testpass
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
Reject interior-NUL str at safe C-string boundary (#426)
ehartford commentedon Jun 17, 2026
Implemented in 8ac8fbd.
Runtime (
rt/rt_core.w): addedstr_has_interior_nuland gatedstr_to_cstr(which backswith_str_to_cstr/the str→*const c_charcodegen adapter and all internal env/fs path wrappers) on it. A dynamic interior NUL now fails loudly viawith_panic_core(exit 1,interior NULon 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_charparam of an extern/c_import function, is now a compile error (sema_type_is_c_char_pointer+string_literal_has_interior_nuldecode-and-scan), caught before codegen.Acceptance criteria
with build/:fixpoint/ tests passTests:
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/CStrAPI surface (#427) can route through this same checked adapter.Add String/StrView/CStr/CString conversion APIs (#427)