Skip to content

Implement handling of C String #2549

Open

Description

@celinval

Requested feature: Add support to the new C string literal.
Use case: Users interacting with C code will likely take advantage of C string in the future. This will be specially helpful when we add proper support to CFFI.
Link to relevant documentation (Rust reference, Nomicon, RFC): https://rust-lang.github.io/rfcs/3348-c-str-literal.html

Test case:

#![feature(rustc_private)]
#![feature(c_str_literals)]

extern crate libc;
use libc::c_char;

#[kani::proof]
fn check_c_str() {
    assert_eq!(unsafe {*empty_c_str()}, 0);
    let (s, len) = non_empty_c_str();
    assert_ne!(unsafe {*s.offset(0)}, 0);
    assert_eq!(unsafe {*s.offset(len as isize)}, 0);
}

fn empty_c_str() -> *const c_char {
    c"".as_ptr() as *const c_char
}

/// Return a C string and its length (without the null character).
fn non_empty_c_str() -> (*const c_char, usize) {
    (c"hi".as_ptr() as *const c_char, 2)
}

Activity

  1. added this to the C-FFI Milestone milestone on Jun 21, 2023
  2. celinval commented on Nov 5, 2024

    @celinval
    ContributorAuthor

    Note that C string literal has been stabilized in Rust 1.77 for edition 2021 and above. So the test no longer needs any feature gate:

    // RUSTFLAGS=--edition=2021
    use std::ffi::c_char;
    
    #[kani::proof]
    fn check_c_str() {
        assert_eq!(unsafe {*empty_c_str()}, 0);
        let (s, len) = non_empty_c_str();
        assert_ne!(unsafe {*s.offset(0)}, 0);
        assert_eq!(unsafe {*s.offset(len as isize)}, 0);
    }
    
    fn empty_c_str() -> *const c_char {
        c"".as_ptr() as *const c_char
    }
    
    /// Return a C string and its length (without the null character).
    fn non_empty_c_str() -> (*const c_char, usize) {
        (c"hi".as_ptr() as *const c_char, 2)
    }
  3. feliperodri commented on Nov 6, 2026

    @feliperodri
    Member

    More evidence for this issue. Kani hit it while verifying these 2 crate(s):

    Minimal reproducer (Kani 0.68.0):

    #[cfg(kani)]
    #[kani::proof]
    fn cstr_lit() {
        let s: &core::ffi::CStr = c"hi";
        assert!(s.to_bytes() == b"hi");
    }
    $ cargo kani -Z unstable-options --harness cstr_lit --harness-timeout 5m
    - C string literal (1)
     - Description: "C string literal is not currently supported by Kani. Please post your example at https://github.com/model-checking/kani/issues/2549"
    Failed Checks: C string literal is not currently supported by Kani. Please post your example at https://github.com/model-checking/kani/issues/2549
    VERIFICATION:- FAILED
    

    Context: we ran AI agents that use the Kani agent plugin (#5059) to verify 101 of the most downloaded crates on crates.io with Kani 0.68.0, CBMC 6.11.0 and nightly-2026-10-22. The minimal reproducer above was re-run by hand with the same versions. This text was written with AI assistance.

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

    Z-Kani CompilerIssues that require some changes to the compiler[C] Feature / EnhancementA new feature request or enhancement to an existing feature.[E] Unsupported ConstructAdd support to an unsupported construct

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions