rust-lang / rust-lang/rust

CStr from fixed c_char array

Open
#146,836 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-FFI C-feature-request T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Hi,

Often times I see a fixed array of C chars returned via FFI boundary which cannot be easily converted into CStr because:

  1. CStr::from_ptr() uses strlen() which can walk outside of boundaries of a fixed array
  2. CStr::from_bytes_until_nul(&[u8]) takes u8 slice.

Each time I stumble upon [c_char; N] it's always a struggle.

As such I think CStr should have an initializer from &[c_char] slice. This would work identical to CStr::from_ptr() except it wouldn't need to use strlen. It could flip the slice into &[u8] and then pass it into from_bytes_until_nul.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing CStr::from_ptr and CStr::from_bytes_until_nul entry points described in the issue, along with how [c_char] slices cross the FFI boundary. Define the bounded conversion's API and safety behavior, then add coverage showing fixed c_char arrays are handled without scanning beyond the slice.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.