Does anyone actually length-check CStr::from_ptr?

I was doing some FFI wrapping and saw this note in the CStr::from_ptr documentation (CStr in std::ffi - Rust):

The nul terminator must be within isize::MAX from ptr

Because malloc and objects in C generally use size_t which is equivalent to usize::MAX, it would seem like we need to check this constraint right? For example on a 32-bit system we might have a 3GB string which is more than CStr can handle.

Looking through various projects and the standard library I can't find anyone checking this. The SAFETY mentions seem to be things like this:

        // SAFETY: Just checked that the pointer is not NULL, and arguments
        // are otherwise guaranteed to be valid C strings.
        let cstr = unsafe { CStr::from_ptr(ptr) };
        vec.push(OsStringExt::from_vec(cstr.to_bytes().to_vec()));

Right now it does do a sanity check at debug that should catch this issue:

                // Chance at catching some UB at runtime with debug builds.
                debug_assert!(!bytes.is_empty() && bytes[bytes.len() - 1] == 0);

Though the documentation also implies this will be removed eventually?

Note: This operation is intended to be a 0-cost cast but it is currently implemented with an up-front calculation of the length of the string. This is not guaranteed to always be the case.

I do wonder what I'm supposed to do here outside add my own check that a C string is under isize::MAX. But there's a lot of code out there that doesn't do this...

On 64-bit systems, you don't have to worry about it because your process will never actually be allocated 263 bytes of RAM. Even counting swap, I don't think a computer with that much storage available to it has ever yet been built, and you tend to run into other limits well below that point, anyway (e.g. most systems' page table structures can't represent more than 248–252 bytes of mapped memory).

On 32-bit systems, you still don't have to worry about it, because the rule you're quoting reflects a limitation on the C side: Like Rust programs, C programs are required never to create any single allocation that is larger than isize::MAX (in C terms, PTRDIFF_MAX). This is because every C "object" can be viewed as an array of its bytes, so you can form a char * pointer to both its beginning and its end, and if the difference between the two is larger than the most positive value representable by ptrdiff_t, taking that difference provokes undefined behavior. (On a quick skim of N1570, it seems like the UB only happens if you actually do the subtraction, but I have heard people who know more about the C standard than me say this means you can't create an object that large in the first place.)

An allocation can be at most isize::MAX bytes in size. This results from the fact that the signed offset between any two pointers must fit in an isize.