Re: [PATCH RFC v2 4/7] rust: implement a test balloon via the "varint" subsystem
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Sep 5, 2025, 21:46 UTC
- Message-ID
- <xmqqy0qs1sk5.fsf@gitster.g>
- In-Reply-To
- <20250905-b4-pks-rust-breaking-change-v2-4-6939cbf4a0b8@pks.im>
Patrick Steinhardt <ps@pks.im> writes:
Show 32 quoted lines
> diff --git a/src/varint.rs b/src/varint.rs
> new file mode 100644
> index 00000000000..3d41760a555
> --- /dev/null
> +++ b/src/varint.rs
> @@ -0,0 +1,92 @@
> +use std::os::raw::c_int;
> +use std::os::raw::c_uchar;
> +
> +#[no_mangle]
> +pub unsafe extern "C" fn decode_varint(bufp: *mut *const c_uchar) -> usize {
> + let mut buf = *bufp;
> + let mut c = *buf;
> + let mut val = usize::from(c & 127);
> +
> + buf = buf.add(1);
> +
> + while (c & 128) != 0 {
> + val += 1;
> + if val == 0 || val.leading_zeros() < 7 {
> + return 0; // overflow
> + }
> +
> + c = *buf;
> + buf = buf.add(1);
> +
> + val = (val << 7) + usize::from(c & 127);
> + }
> +
> + *bufp = buf;
> + val
> +}This (and the encoding side) looks quite faithful translation of the original in C. Interestingly, disassembly I saw looked a lot more optimized than the C variant compiled with clang-19 -O2. The difference probably is largely due to its omitting frame pointer.
The comparison to detect overflow compiled to direct comparison with 0x1ffffffffffffff (both in C and rustc/LLVM), which was amusing, too.