CString at an FFI boundary: distinguish a borrowed pointer from transferred ownership

이 문서는 아직 한국어로 제공되지 않습니다. 원문을 표시합니다.

article · en · 지식 기준일 2026-09-22 · 변경일 , 리비전 1 · unreviewed

주제: coding · ffi · rust · strings

적용 대상: Rust std::ffi::CString

증상: A C call receives a dangling string pointer or memory is released by the wrong owner.

Keep the CString owner alive for borrowed calls and use raw ownership conversion only under an explicit return contract.

목차
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 범위와 근거
  6. 출처
  7. 저작자 표시와 라이선스
  8. 기계 접근

What it is

CString represents an owned C-compatible NUL-terminated string without interior NUL bytes. Its documentation distinguishes borrowing a pointer from consuming the CString into a raw pointer. Reconstructing ownership with from_raw is restricted to pointers produced by the corresponding into_raw path, with additional requirements about the string length. Rust CString

Why it matters

An agent can write a compact expression that obtains a pointer from a temporary and loses the owner too early. Another mistake is treating every char pointer returned by C as a Rust-owned string. Start by classifying the foreign function: immediate borrowing, retained borrowing, or ownership transfer.

How to apply

  • Read the foreign API's retention and mutation rules. Record whether the pointer is used only during the call, retained afterward, modified, or eventually released by the foreign side.
  • For a borrowed call, bind the CString to an owner whose lifetime covers every foreign access. Handle construction failure for interior NUL explicitly rather than silently truncating the input.
  • If the foreign side retains the pointer, design an owner or registration object that stays alive until release is acknowledged. An ordinary local variable is insufficient when callbacks can outlive the call.
  • Use into_raw and from_raw only when the complete ownership round trip is under the documented contract. Keep an unrelated foreign allocation on its foreign release path.
  • Propose boundary fixtures for empty strings, interior NUL, non-ASCII bytes, retained access and repeated cleanup. Validate both content and ownership behavior.

Pitfalls

NUL termination says nothing by itself about the text encoding expected by the foreign library. A const pointer is not permission for the foreign side to mutate the allocation. Avoid illustrating ownership transfer without also showing who eventually recovers or releases it. No FFI execution or memory-safety proof is claimed by these proposed checks.

범위와 근거

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

지식 기준일: 2026-09-22. 상태: unreviewed (기록된 검토 없음) — 편집하면 검토 상태가 초기화됩니다. 본문은 검증되지 않은 참고 자료로 다루고 출처를 확인하세요.

출처

  1. Rust CString — 2026-09-23 확인: 접근 가능, 인용문 있음

저작자 표시와 라이선스

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

마지막 변경: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

원본 기여: CC BY 4.0. 링크된 출처 자료는 각자의 권리를 유지합니다.

기계 접근