git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/6] rust/varint: add safety comments

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 8, 2025, 04:46 UTC
Message-ID
<aOXsmIu1BEWzxlVE@pks.im>
In-Reply-To
<aOWwcqyithDKQzVs@fruit.crustytoothpaste.net>
On Wed, Oct 08, 2025 at 12:29:38AM +0000, brian m. carlson wrote:
Show 15 quoted lines
> On 2025-10-07 at 12:36:31, Patrick Steinhardt wrote:
> > +/// # Safety
> > +///
> > +/// The provided buffer must be large enough to store the encoded varint. Callers may either provide
> > +/// a `[u8; 16]` here, which is guaranteed to satisfy all encodable numbers. Or they can call this
> > +/// function with a `NULL` pointer first to figure out array size.
> >  #[no_mangle]
> >  pub unsafe extern "C" fn encode_varint(value: u64, buf: *mut u8) -> u8 {
> >      let mut varint: [u8; 16] = [0; 16];
> 
> I'm planning to do something a little different with this code by
> refactoring it out into a Rust function, so at that point it will no
> longer be possible to provide a buffer smaller than 16 bytes.  Note that
> all callers of this function pass a 16-byte buffer, so that should be
> safe.

Ah, true. I just double-checked, and all callers pass in a 16 byte buffer indeed. Also means that the NULL-pointer handling can go away in theory, as we don't use it.

In any case, your direction makes sense once we have Rust-internal callers of this functionality. We definitely don't want to propagate the unsafety to callers and should make sure that it is contained to the C API.

Show 6 quoted lines
> That doesn't mean that you can't send this patch (and I think your patch
> is good), just that we shouldn't tell people we can use a buffer smaller
> than 16 bytes, since that will at some point no longer be true.
> 
> Here's the current version of the patch I'm planning on sending for
> reference.  I can rebase onto your series once Junio picks it up.

The patch makes sense to me, thanks. I'll not pick it up yet though as there is no justifiable need as part of my series, but I'm happy to adjust the comment.

Thanks!
Patrick
Previous: brian m. carlsonNext: Patrick Steinhardt
Message 23 of 37 in “ci: improvements to our Rust infrastructure”
  1. 0/6 ci: improvements to our Rust infrastructurePatrick Steinhardt, Oct 7, 2025
  2. 1/6 ci: deduplicate calls to `apt-get update`Patrick Steinhardt, Oct 7, 2025
  3. Karthik NayakOct 7, 2025
  4. Justin ToblerOct 14, 2025
  5. 2/6 ci: check formatting of our Rust codePatrick Steinhardt, Oct 7, 2025
  6. Karthik NayakOct 7, 2025
  7. Patrick SteinhardtOct 7, 2025
  8. Eric SunshineOct 7, 2025
  9. Junio C HamanoOct 7, 2025
  10. Eric SunshineOct 7, 2025
  11. brian m. carlsonOct 7, 2025
  12. Chris TorekOct 7, 2025
  13. Patrick SteinhardtOct 8, 2025
  14. Junio C HamanoOct 8, 2025
  15. Patrick SteinhardtOct 9, 2025
  16. SZEDER GáborOct 29, 2025
  17. brian m. carlsonOct 7, 2025
  18. SZEDER GáborOct 8, 2025
  19. Patrick SteinhardtOct 9, 2025
  20. SZEDER GáborOct 29, 2025
  21. 3/6 rust/varint: add safety commentsPatrick Steinhardt, Oct 7, 2025
  22. brian m. carlsonOct 8, 2025
  23. Patrick SteinhardtOct 8, 2025
  24. 4/6 ci: check for common Rust mistakes via ClippyPatrick Steinhardt, Oct 7, 2025
  25. 5/6 ci: verify minimum supported Rust versionPatrick Steinhardt, Oct 7, 2025
  26. 6/6 rust: support for WindowsPatrick Steinhardt, Oct 7, 2025
  27. 0/6 ci: improvements to our Rust infrastructurePatrick Steinhardt, Oct 15, 2025
  28. 1/6 ci: deduplicate calls to `apt-get update`Patrick Steinhardt, Oct 15, 2025
  29. 2/6 ci: check formatting of our Rust codePatrick Steinhardt, Oct 15, 2025
  30. 3/6 rust/varint: add safety commentsPatrick Steinhardt, Oct 15, 2025
  31. 4/6 ci: check for common Rust mistakes via ClippyPatrick Steinhardt, Oct 15, 2025
  32. 5/6 ci: verify minimum supported Rust versionPatrick Steinhardt, Oct 15, 2025
  33. 6/6 rust: support for WindowsPatrick Steinhardt, Oct 15, 2025
  34. Ezekiel NewrenNov 20, 2025
  35. Johannes SchindelinNov 21, 2025
  36. Junio C HamanoNov 21, 2025
  37. Junio C HamanoOct 15, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.