unsafe: flush() 中缺少 SAFETY 注释 (avhw.rs:757) #7

Closed
opened 2026-06-04 20:58:57 +08:00 by dailz · 1 comment
Owner

位置

src/avhw.rs:757-762

严重性

🟢

问题描述

flush() 中的 unsafe 块(发送 null frame 刷新编码器)缺少 SAFETY 注释。Rust 项目最佳实践要求每个 unsafe 块都附有解释其安全性的注释。

建议修复

// SAFETY: Sending a null frame flushes the encoder without transferring ownership.
unsafe {
    let ret = ffi::avcodec_send_frame(self.enc_video.as_mut_ptr(), ptr::null());
    if ret < 0 && ret != ffi::AVERROR_EOF {
        bail!("software encoder flush send failed: error {ret}");
    }
}
## 位置 `src/avhw.rs:757-762` ## 严重性 🟢 低 ## 问题描述 `flush()` 中的 `unsafe` 块(发送 null frame 刷新编码器)缺少 SAFETY 注释。Rust 项目最佳实践要求每个 `unsafe` 块都附有解释其安全性的注释。 ## 建议修复 ```rust // SAFETY: Sending a null frame flushes the encoder without transferring ownership. unsafe { let ret = ffi::avcodec_send_frame(self.enc_video.as_mut_ptr(), ptr::null()); if ret < 0 && ret != ffi::AVERROR_EOF { bail!("software encoder flush send failed: error {ret}"); } } ```
dailz closed this issue 2026-06-06 15:54:18 +08:00
Author
Owner

修复完成

Issue 原指 flush() 中第 766 行缺少 SAFETY 注释,实际审核发现文件中共有 19 处 unsafe 缺少文档。

修复内容

1. 补全 SAFETY 注释(19 处)

  • 17 个 unsafe block
  • 2 个 unsafe impl Send

2. 加固防御性检查(2 处)

  • drain_encoder 中 stream array dereference 增加 nb_streams == 0 / streams.is_null() guard
  • software encoder 路径中同样位置增加相同 guard

3. 启用 Clippy lint 防止回归

  • Cargo.toml 添加 undocumented_unsafe_blocks = warn

验证

  • cargo clippy --lib: 0 error, avhw.rs 中 0 unsafe warning
  • commit: 7c1c9b2
## 修复完成 Issue 原指 flush() 中第 766 行缺少 SAFETY 注释,实际审核发现文件中共有 **19 处** unsafe 缺少文档。 ### 修复内容 **1. 补全 SAFETY 注释(19 处)** - 17 个 unsafe block - 2 个 unsafe impl Send **2. 加固防御性检查(2 处)** - drain_encoder 中 stream array dereference 增加 nb_streams == 0 / streams.is_null() guard - software encoder 路径中同样位置增加相同 guard **3. 启用 Clippy lint 防止回归** - Cargo.toml 添加 undocumented_unsafe_blocks = warn ### 验证 - cargo clippy --lib: 0 error, avhw.rs 中 0 unsafe warning - commit: 7c1c9b2
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dailz/wl-webrtc#7