Skip to content

Commit 6358a08

Browse files
pks-tgitster
authored andcommitted
rust/varint: add safety comments
The `decode_varint()` and `encode_varint()` functions in our Rust crate are reimplementations of the respective C functions. As such, we are naturally forced to use the same interface in both Rust and C, which makes use of raw pointers. The consequence is that the code needs to be marked as unsafe in Rust. It is common practice in Rust to provide safety documentation for every block that is marked as unsafe. This common practice is also enforced by Clippy, Rust's static analyser. We don't have Clippy wired up yet, and we could of course just disable this check. But we're about to wire it up, and it is reasonable to always enforce documentation for unsafe blocks. Add such safety comments to already squelch those warnings now. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 052e679 commit 6358a08

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/varint.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/// # Safety
2+
///
3+
/// Callers must provide a NUL-terminated array to ensure safety.
14
#[no_mangle]
25
pub unsafe extern "C" fn decode_varint(bufp: *mut *const u8) -> u64 {
36
let mut buf = *bufp;
@@ -22,6 +25,11 @@ pub unsafe extern "C" fn decode_varint(bufp: *mut *const u8) -> u64 {
2225
val
2326
}
2427

28+
/// # Safety
29+
///
30+
/// The provided buffer must be large enough to store the encoded varint. Callers may either provide
31+
/// a `[u8; 16]` here, which is guaranteed to satisfy all encodable numbers. Or they can call this
32+
/// function with a `NULL` pointer first to figure out array size.
2533
#[no_mangle]
2634
pub unsafe extern "C" fn encode_varint(value: u64, buf: *mut u8) -> u8 {
2735
let mut varint: [u8; 16] = [0; 16];

0 commit comments

Comments
 (0)