Skip to content

Conversation

@nyurik
Copy link
Contributor

@nyurik nyurik commented Dec 10, 2024

This PR renames the former kind enum from FromBytesWithNulErrorKind to FromBytesWithNulError, and removes the original struct.

See rust-lang/libs-team#493

Possible Changes - TBD

  • should the new enum FromBytesWithNulError derive Copy?
  • should there be any new/changed attributes?
  • add some more tests

Problem

One of CStr constructors, CStr::from_bytes_with_nul(bytes: &[u8]) handles 3 cases:

  1. bytes has one NULL as the last value - creates CStr
  2. bytes has no NULL - error
  3. bytes has a NULL in some other position - error

The 3rd case is error that may require lossy conversion, but the 2nd case can easily be handled by the user code. Unfortunately, this function returns an opaque FromBytesWithNulError error in both 2nd and 3rd case, so the user cannot detect just the 2nd case - having to re-implement the entire function and bring in the memchr dependency.

Motivating examples or use cases

In this code, my FFI code needs to copy user's &[u8] into a C-allocated memory blob in a NUL-terminated CStr format. My code must first validate if &[u8] has a trailing NUL (case 1), no NUL (adds one on the fly - case 2), or NUL in the middle (3rd case - error). I had to re-implement from_bytes_with_nul and add memchrdependency just to handle the 2nd case.

r? @Amanieu

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 10, 2024
@tgross35 tgross35 added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 13, 2024
@nyurik
Copy link
Contributor Author

nyurik commented Dec 25, 2024

@tgross35 I thought this proposal has already passed - see rust-lang/libs-team#493

@tgross35
Copy link
Contributor

The meeting was a preliminary thumbs up, but the actual FCP process still needs to happen for anything that changes stable API.

@nyurik
Copy link
Contributor Author

nyurik commented Dec 26, 2024

@tgross35 thx, any links on anything I need to do? Or is this rust-team internal?

@tgross35
Copy link
Contributor

Nothing internal and nothing for you to do, a lot of rust-lang just slows down around this time of year. Somebody from the libs-api team will need to propose FCP, Amanieu can do that and is already assigned.

@Amanieu
Copy link
Member

Amanieu commented Dec 26, 2024

This is insta-stable since this is turned into a enum which can be matched on, even if (I think) we put the variants under a feature flag.

@rfcbot merge

@rfcbot
Copy link

rfcbot commented Dec 26, 2024

Team member @Amanieu has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 26, 2024
@rust-log-analyzer

This comment has been minimized.

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Jan 5, 2025
@rfcbot
Copy link

rfcbot commented Jan 5, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Jan 5, 2025
@theemathas
Copy link
Contributor

I believe that this can potentially break existing code, so this needs at the very least a crater run. See rust-lang/rfcs#3753

@tgross35
Copy link
Contributor

tgross35 commented Jan 5, 2025

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 5, 2025
Convert `struct FromBytesWithNulError` into enum

This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct.

See rust-lang/libs-team#493

## Possible Changes - TBD
* [x] should the new `enum FromBytesWithNulError` derive `Copy`?
* [ ] should there be any new/changed attributes?
* [x] add some more tests

## Problem

One of `CStr` constructors, `CStr::from_bytes_with_nul(bytes: &[u8])` handles 3 cases:
1. `bytes` has one NULL as the last value - creates CStr
2. `bytes` has no NULL - error
3. `bytes` has a NULL in some other position - error

The 3rd case is error that may require lossy conversion, but the 2nd case can easily be handled by the user code. Unfortunately, this function returns an opaque `FromBytesWithNulError` error in both 2nd and 3rd case, so the user cannot detect just the 2nd case - having to re-implement the entire function and bring in the `memchr` dependency.

## Motivating examples or use cases

In [this code](https://github.com/gquintard/varnish-rs/blob/f86d7a87683b08d2e634d63e77d9dc1d24ed4a13/varnish-sys/src/vcl/ws.rs#L158), my FFI code needs to copy user's `&[u8]` into a C-allocated memory blob in a NUL-terminated `CStr` format.  My code must first validate if `&[u8]` has a trailing NUL (case 1), no NUL (adds one on the fly - case 2), or NUL in the middle (3rd case - error). I had to re-implement `from_bytes_with_nul` and add `memchr`dependency just to handle the 2nd case.

r? `@Amanieu`
@bors
Copy link
Collaborator

bors commented Jan 5, 2025

⌛ Trying commit bf4ecec with merge aacf02f...

@bors
Copy link
Collaborator

bors commented Jan 5, 2025

☀️ Try build successful - checks-actions
Build commit: aacf02f (aacf02ff7319d9b9af3fe5e9e1094c2f000a4689)

@tgross35
Copy link
Contributor

tgross35 commented Jan 5, 2025

I was about to start a crater run but checked for use in the wild first, from a quick search I don't see any cases of the potentially breaking pattern mentioned in the ACP https://github.com/search?q=lang%3Arust+%2FFromBytesWithNulError+%3F%5C%7B+%3F%5C.%5C.%2F&type=code. So the beta crater runs are probably sufficient to cover anything here, unless there are other patterns that also break.

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@dtolnay
Copy link
Member

dtolnay commented Jan 15, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 15, 2025

📌 Commit 86b86fa has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2025
@dtolnay dtolnay assigned dtolnay and unassigned Amanieu Jan 15, 2025
@nyurik
Copy link
Contributor Author

nyurik commented Jan 15, 2025

@rustbot label: +relnotes

@rustbot rustbot added the relnotes Marks issues that should be documented in the release notes of the next release. label Jan 15, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 15, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang#132397 (Make missing_abi lint warn-by-default.)
 - rust-lang#133807 (ci: Enable opt-dist for dist-aarch64-linux builds)
 - rust-lang#134143 (Convert `struct FromBytesWithNulError` into enum)
 - rust-lang#134338 (Use a C-safe return type for `__rust_[ui]128_*` overflowing intrinsics)
 - rust-lang#134678 (Update `ReadDir::next` in `std::sys::pal::unix::fs` to use `&raw const (*p).field` instead of `p.byte_offset().cast()`)
 - rust-lang#135424 (Detect unstable lint docs that dont enable their feature)
 - rust-lang#135520 (Make sure we actually use the right trivial lifetime substs when eagerly monomorphizing drop for ADTs)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 229c91b into rust-lang:master Jan 15, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 15, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 15, 2025
Rollup merge of rust-lang#134143 - nyurik:err-nul, r=dtolnay

Convert `struct FromBytesWithNulError` into enum

This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct.

See rust-lang/libs-team#493

## Possible Changes - TBD
* [x] should the new `enum FromBytesWithNulError` derive `Copy`?
* [ ] should there be any new/changed attributes?
* [x] add some more tests

## Problem

One of `CStr` constructors, `CStr::from_bytes_with_nul(bytes: &[u8])` handles 3 cases:
1. `bytes` has one NULL as the last value - creates CStr
2. `bytes` has no NULL - error
3. `bytes` has a NULL in some other position - error

The 3rd case is error that may require lossy conversion, but the 2nd case can easily be handled by the user code. Unfortunately, this function returns an opaque `FromBytesWithNulError` error in both 2nd and 3rd case, so the user cannot detect just the 2nd case - having to re-implement the entire function and bring in the `memchr` dependency.

## Motivating examples or use cases

In [this code](https://github.com/gquintard/varnish-rs/blob/f86d7a87683b08d2e634d63e77d9dc1d24ed4a13/varnish-sys/src/vcl/ws.rs#L158), my FFI code needs to copy user's `&[u8]` into a C-allocated memory blob in a NUL-terminated `CStr` format.  My code must first validate if `&[u8]` has a trailing NUL (case 1), no NUL (adds one on the fly - case 2), or NUL in the middle (3rd case - error). I had to re-implement `from_bytes_with_nul` and add `memchr`dependency just to handle the 2nd case.

r? `@Amanieu`
@nyurik nyurik deleted the err-nul branch January 15, 2025 18:02
tautschnig added a commit to tautschnig/verify-rust-std that referenced this pull request Jan 17, 2025