Skip to content

Commit c34d308

Browse files
authored
#![deny(clippy::undocumented_unsafe_blocks)]: Add and fix existing safety comments (#1305)
* Part of #1270. This adds `#![deny(clippy::undocumented_unsafe_blocks)]` to the `librav1d` crate. Since we don't yet run `clippy` in CI, this doesn't do anything yet, but that's good, since we can turn that on once we fix all of the errors. This PR only turns on the lint and fixes the existing safety comments into ones that the lint recognizes.
2 parents 347694f + 3ceb813 commit c34d308

13 files changed

+77
-58
lines changed

include/dav1d/data.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ impl From<Dav1dData> for Rav1dData {
3030
m,
3131
} = value;
3232
Self {
33-
// Safety: `r#ref` is a [`RawCArc`] originally from [`CArc`].
34-
data: r#ref.map(|r#ref| unsafe { CArc::from_raw(r#ref) }),
33+
data: r#ref.map(|r#ref| {
34+
// SAFETY: `r#ref` is a [`RawCArc`] originally from [`CArc`].
35+
unsafe { CArc::from_raw(r#ref) }
36+
}),
3537
m: m.into(),
3638
}
3739
}

include/dav1d/picture.rs

+26-14
Original file line numberDiff line numberDiff line change
@@ -449,24 +449,36 @@ impl From<Dav1dPicture> for Rav1dPicture {
449449
} = value;
450450
Self {
451451
// We don't `.update_rav1d()` [`Rav1dSequenceHeader`] because it's meant to be read-only.
452-
// Safety: `raw` came from [`RawArc::from_arc`].
453-
seq_hdr: seq_hdr_ref.map(|raw| unsafe { raw.into_arc() }),
452+
seq_hdr: seq_hdr_ref.map(|raw| {
453+
// SAFETY: `raw` came from [`RawArc::from_arc`].
454+
unsafe { raw.into_arc() }
455+
}),
454456
// We don't `.update_rav1d()` [`Rav1dFrameHeader`] because it's meant to be read-only.
455-
// Safety: `raw` came from [`RawArc::from_arc`].
456-
frame_hdr: frame_hdr_ref.map(|raw| unsafe { raw.into_arc() }),
457-
// Safety: `raw` came from [`RawArc::from_arc`].
458-
data: data_ref.map(|raw| unsafe { raw.into_arc() }),
457+
frame_hdr: frame_hdr_ref.map(|raw| {
458+
// SAFETY: `raw` came from [`RawArc::from_arc`].
459+
unsafe { raw.into_arc() }
460+
}),
461+
data: data_ref.map(|raw| {
462+
// SAFETY: `raw` came from [`RawArc::from_arc`].
463+
unsafe { raw.into_arc() }
464+
}),
459465
stride,
460466
p: p.into(),
461467
m: m.into(),
462-
// Safety: `raw` came from [`RawArc::from_arc`].
463-
content_light: content_light_ref.map(|raw| unsafe { raw.into_arc() }),
464-
// Safety: `raw` came from [`RawArc::from_arc`].
465-
mastering_display: mastering_display_ref.map(|raw| unsafe { raw.into_arc() }),
468+
content_light: content_light_ref.map(|raw| {
469+
// SAFETY: `raw` came from [`RawArc::from_arc`].
470+
unsafe { raw.into_arc() }
471+
}),
472+
mastering_display: mastering_display_ref.map(|raw| {
473+
// Safety: `raw` came from [`RawArc::from_arc`].
474+
unsafe { raw.into_arc() }
475+
}),
466476
// We don't `.update_rav1d` [`Rav1dITUTT35`] because never read it.
467-
// Safety: `raw` came from [`RawArc::from_arc`].
468477
itut_t35: itut_t35_ref
469-
.map(|raw| unsafe { raw.into_arc() })
478+
.map(|raw| {
479+
// SAFETY: `raw` came from [`RawArc::from_arc`].
480+
unsafe { raw.into_arc() }
481+
})
470482
.unwrap_or_default(),
471483
}
472484
}
@@ -740,7 +752,7 @@ impl Rav1dPicAllocator {
740752
..Default::default()
741753
};
742754
let mut pic_c = pic.to::<Dav1dPicture>();
743-
// Safety: `pic_c` is a valid `Dav1dPicture` with `data`, `stride`, `allocator_data` unset.
755+
// SAFETY: `pic_c` is a valid `Dav1dPicture` with `data`, `stride`, `allocator_data` unset.
744756
let result = unsafe { (self.alloc_picture_callback)(&mut pic_c, self.cookie) };
745757
result.try_to::<Rav1dResult>().unwrap()?;
746758
// `data`, `stride`, and `allocator_data` are the only fields set by the allocator.
@@ -777,7 +789,7 @@ impl Rav1dPicAllocator {
777789
allocator_data,
778790
..Default::default()
779791
};
780-
// Safety: `pic_c` contains the same `data` and `allocator_data`
792+
// SAFETY: `pic_c` contains the same `data` and `allocator_data`
781793
// that `Self::alloc_picture_data` set, which now get deallocated here.
782794
unsafe {
783795
(self.release_picture_callback)(&mut pic_c, self.cookie);

lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
)]
77
#![deny(unsafe_op_in_unsafe_fn)]
88
#![allow(clippy::all)]
9+
#![deny(clippy::undocumented_unsafe_blocks)]
910

1011
#[cfg(not(any(feature = "bitdepth_8", feature = "bitdepth_16")))]
1112
compile_error!("No bitdepths enabled. Enable one or more of the following features: `bitdepth_8`, `bitdepth_16`");

src/align.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ impl<T: Copy, C: AlignedByteChunk> AlignedVec<T, C> {
169169

170170
/// Extract a slice containing the entire vector.
171171
pub fn as_slice(&self) -> &[T] {
172-
// Safety: The first `len` elements have been
172+
// SAFETY: The first `len` elements have been
173173
// initialized to `T`s in `Self::resize_with`.
174174
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
175175
}
176176

177177
/// Extract a mutable slice of the entire vector.
178178
pub fn as_mut_slice(&mut self) -> &mut [T] {
179-
// Safety: The first `len` elements have been
179+
// SAFETY: The first `len` elements have been
180180
// initialized to `T`s in `Self::resize_with`.
181181
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
182182
}

src/c_arc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::sync::Arc;
1111

1212
pub fn arc_into_raw<T: ?Sized>(arc: Arc<T>) -> NonNull<T> {
1313
let raw = Arc::into_raw(arc).cast_mut();
14-
// Safety: [`Arc::into_raw`] never returns null.
14+
// SAFETY: [`Arc::into_raw`] never returns null.
1515
unsafe { NonNull::new_unchecked(raw) }
1616
}
1717

@@ -106,7 +106,7 @@ impl<T: ?Sized> AsRef<T> for CArc<T> {
106106
}
107107
}
108108

109-
// Safety: [`Self::stable_ref`] is a ptr
109+
// SAFETY: [`Self::stable_ref`] is a ptr
110110
// derived from [`Self::owner`]'s through [`CBox::as_ref`]
111111
// and is thus safe to dereference.
112112
// The [`CBox`] is [`Pin`]ned and
@@ -239,7 +239,7 @@ impl<T: ?Sized> CArc<T> {
239239
///
240240
/// The [`RawCArc`] must be originally from [`Self::into_raw`].
241241
pub unsafe fn from_raw(raw: RawCArc<T>) -> Self {
242-
// Safety: The [`RawCArc`] contains the output of [`Arc::into_raw`],
242+
// SAFETY: The [`RawCArc`] contains the output of [`Arc::into_raw`],
243243
// so we can call [`Arc::from_raw`] on it.
244244
let owner = unsafe { raw.0.into_arc() };
245245
owner.into()

src/c_box.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ unsafe impl<T: Sync + ?Sized> Sync for Unique<T> {}
7070
pub enum CBox<T: ?Sized> {
7171
Rust(Box<T>),
7272
C {
73-
/// # Safety:
73+
/// # SAFETY:
7474
///
7575
/// * Never moved.
7676
/// * Valid to dereference.
@@ -103,12 +103,12 @@ impl<T: ?Sized> Drop for CBox<T> {
103103
Self::Rust(_) => {} // Drop normally.
104104
Self::C { data, free, .. } => {
105105
let ptr = data.pointer.as_ptr();
106-
// Safety: See below.
106+
// SAFETY: See below.
107107
// The [`FnFree`] won't run Rust's `fn drop`,
108108
// so we have to do this ourselves first.
109109
unsafe { drop_in_place(ptr) };
110110
let ptr = ptr.cast();
111-
// Safety: See safety docs on [`Self::data`] and [`Self::from_c`].
111+
// SAFETY: See safety docs on [`Self::data`] and [`Self::from_c`].
112112
unsafe { free.free(ptr) }
113113
}
114114
}
@@ -137,7 +137,7 @@ impl<T: ?Sized> CBox<T> {
137137
}
138138

139139
pub fn into_pin(self) -> Pin<Self> {
140-
// Safety:
140+
// SAFETY:
141141
// If `self` is `Self::Rust`, `Box` can be pinned.
142142
// If `self` is `Self::C`, `data` is never moved until [`Self::drop`].
143143
unsafe { Pin::new_unchecked(self) }

src/disjoint_mut.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,7 @@ impl<V: Copy, C: AlignedByteChunk> DisjointMut<AlignedVec<V, C>> {
10021002
}
10031003
}
10041004

1005+
#[allow(clippy::undocumented_unsafe_blocks)]
10051006
#[test]
10061007
fn test_overlapping_immut() {
10071008
let mut v: DisjointMut<Vec<u8>> = Default::default();
@@ -1013,6 +1014,7 @@ fn test_overlapping_immut() {
10131014
assert_eq!(guard1[2], guard2[0]);
10141015
}
10151016

1017+
#[allow(clippy::undocumented_unsafe_blocks)]
10161018
#[test]
10171019
#[cfg_attr(debug_assertions, should_panic)]
10181020
fn test_overlapping_mut() {
@@ -1026,6 +1028,7 @@ fn test_overlapping_mut() {
10261028
assert_eq!(guard1[2], 42);
10271029
}
10281030

1031+
#[allow(clippy::undocumented_unsafe_blocks)]
10291032
#[cfg(debug_assertions)]
10301033
#[test]
10311034
fn test_pointer_write_debug() {
@@ -1052,6 +1055,7 @@ fn test_pointer_write_debug() {
10521055

10531056
// Run with miri using the following command:
10541057
// RUSTFLAGS="-C debug-assertions=off" cargo miri test
1058+
#[allow(clippy::undocumented_unsafe_blocks)]
10551059
#[cfg(not(debug_assertions))]
10561060
#[test]
10571061
fn test_pointer_write_release() {

src/filmgrain.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
2-
#![warn(clippy::all)]
3-
#![allow(clippy::too_many_arguments)]
42

53
use crate::include::common::bitdepth::AsPrimitive;
64
use crate::include::common::bitdepth::BitDepth;
@@ -288,7 +286,7 @@ unsafe extern "C" fn generate_grain_y_c_erased<BD: BitDepth>(
288286
data: &Dav1dFilmGrainData,
289287
bitdepth_max: c_int,
290288
) {
291-
// Safety: Casting back to the original type from the `generate_grain_y::Fn::call`.
289+
// SAFETY: Casting back to the original type from the `generate_grain_y::Fn::call`.
292290
let buf = unsafe { &mut *buf.cast() };
293291
let data = &data.clone().into();
294292
let bd = BD::from_c(bitdepth_max);
@@ -448,7 +446,7 @@ fn generate_grain_uv_rust<BD: BitDepth>(
448446
let (luma_y, luma_x) = is_sub.luma((y, x));
449447
const _: () = IsSub::check_buf_index_all(&None::<GrainLut<()>>);
450448
// The optimizer is not smart enough to deduce this on its own.
451-
// Safety: The above static check checks all maximum index possibilities.
449+
// SAFETY: The above static check checks all maximum index possibilities.
452450
unsafe {
453451
assume(luma_y < GRAIN_HEIGHT + 1 - 1);
454452
assume(luma_x < GRAIN_WIDTH - 1);
@@ -492,9 +490,9 @@ unsafe extern "C" fn generate_grain_uv_c_erased<
492490
uv: intptr_t,
493491
bitdepth_max: c_int,
494492
) {
495-
// Safety: Casting back to the original type from the `generate_grain_uv::Fn::call`.
493+
// SAFETY: Casting back to the original type from the `generate_grain_uv::Fn::call`.
496494
let buf = unsafe { &mut *buf.cast() };
497-
// Safety: Casting back to the original type from the `generate_grain_uv::Fn::call`.
495+
// SAFETY: Casting back to the original type from the `generate_grain_uv::Fn::call`.
498496
let buf_y = unsafe { &*buf_y.cast() };
499497
let data = &data.clone().into();
500498
let is_uv = uv != 0;
@@ -546,9 +544,9 @@ unsafe extern "C" fn fgy_32x32xn_c_erased<BD: BitDepth>(
546544
// SAFETY: Was passed as `FFISafe::new(_)` in `fgy_32x32xn::Fn::call`.
547545
let [dst_row, src_row] = [dst_row, src_row].map(|it| *unsafe { FFISafe::get(it) });
548546
let data = &data.clone().into();
549-
// Safety: Casting back to the original type from the `fn` ptr call.
547+
// SAFETY: Casting back to the original type from the `fn` ptr call.
550548
let scaling = unsafe { &*scaling.cast() };
551-
// Safety: Casting back to the original type from the `fn` ptr call.
549+
// SAFETY: Casting back to the original type from the `fn` ptr call.
552550
let grain_lut = unsafe { &*grain_lut.cast() };
553551
let bh = bh as usize;
554552
let row_num = row_num as usize;
@@ -883,13 +881,14 @@ unsafe extern "C" fn fguv_32x32xn_c_erased<
883881
src_row: *const FFISafe<Rav1dPictureDataComponentOffset>,
884882
luma_row: *const FFISafe<Rav1dPictureDataComponentOffset>,
885883
) {
886-
// SAFETY: Was passed as `FFISafe::new(_)` in `fguv_32x32xn::Fn::call`.
887-
let [dst_row, src_row, luma_row] =
888-
[dst_row, src_row, luma_row].map(|row| *unsafe { FFISafe::get(row) });
884+
let [dst_row, src_row, luma_row] = [dst_row, src_row, luma_row].map(|row| {
885+
// SAFETY: Was passed as `FFISafe::new(_)` in `fguv_32x32xn::Fn::call`.
886+
*unsafe { FFISafe::get(row) }
887+
});
889888
let data = &data.clone().into();
890-
// Safety: Casting back to the original type from the `fn` ptr call.
889+
// SAFETY: Casting back to the original type from the `fn` ptr call.
891890
let scaling = unsafe { &*scaling.cast() };
892-
// Safety: Casting back to the original type from the `fn` ptr call.
891+
// SAFETY: Casting back to the original type from the `fn` ptr call.
893892
let grain_lut = unsafe { &*grain_lut.cast() };
894893
let bh = bh as usize;
895894
let row_num = row_num as usize;

src/intra_edge.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<const SB128: bool, const N_BRANCH: usize, const N_TIP: usize>
344344
if cfg!(debug_assertions) {
345345
&edges[edge.index as usize]
346346
} else {
347-
// Safety: Already checked in `Self::check_indices`, and `EdgeIndex`'s fields are private.
347+
// SAFETY: Already checked in `Self::check_indices`, and `EdgeIndex`'s fields are private.
348348
unsafe { edges.get_unchecked(edge.index as usize) }
349349
}
350350
}

src/lf_mask.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn mask_edges_inter(
250250
}
251251

252252
for y in 0..h4 {
253-
// SAFETY y < h4 and w4 - 1 < w4 so txa[0][0][y][w4 - 1] is initialized.
253+
// SAFETY: y < h4 and w4 - 1 < w4 so txa[0][0][y][w4 - 1] is initialized.
254254
l[y] = unsafe { txa[0][0][y][w4 - 1].assume_init() };
255255
}
256256
// SAFETY: h4 - 1 < h4 and ..w4 < w4 so txa[1][0][h4 - 1][..w4] is

src/log.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl fmt::Write for Dav1dLogger {
4848
// or the Rust API can be used instead.
4949
let fmt = c"%c";
5050
for &byte in s.as_bytes() {
51-
// # Safety
51+
// SAFETY:
5252
//
5353
// The first argument is `self.cookie`
5454
// and the rest are safe to call `printf` with,
@@ -120,7 +120,7 @@ mod marker {
120120
type Callback = extern "C" fn(cookie: *mut c_void, fmt: *const c_char);
121121

122122
const fn cast(callback: Callback) -> Dav1dLoggerCallback {
123-
// Safety: It should always be safe to ignore variadic args.
123+
// SAFETY: It should always be safe to ignore variadic args.
124124
// Declaring a variadic `fn` is unstable, though, which is why we avoid that.
125125
unsafe { std::mem::transmute(callback) }
126126
}

0 commit comments

Comments
 (0)