Skip to contentSkip to conversation

rust: bitmap: document panics in `next_bit` and `next_zero_bit`

PATCH In review Georgios Androutsopoulos 1 participant 2 messages Sep 8 – Oct 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 rust: bitmap: document panics in `next_bit` and `next_zero_bit`

next_bit() and next_zero_bit() use bitmap_assert!() to check that start is less than self.len(), which panics when CONFIG_RUST_BITMAP_HARDENED is enabled. However, neither function has a # Panics section, and both document that None is returned for exactly the input that triggers the panic.

Update the documentation of next_bit() and next_zero_bit() to add the missing # Panics sections, add the missing blank doc comment line before Returns in next_zero_bit(), and simplify the wording.

---
Changes in v3:
- Use Closes: instead of Link:, and add Fixes:, following feedback from
Alexandre Courbot and Miguel Ojeda.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/

Changes in v2:
- Simplify doc wording following feedback from Alice Ryhl.
- Remove extra empty line between Link: and Signed-off-by:.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/kernel/bitmap.rs | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
rust/kernel/bitmap.rs +15 -2
index b27e0ec80d64..b51c8a96f1a8 100644
@@ -459,7 +459,13 @@ pub fn last_bit(&self) -> Option<usize> {
 
     /// Finds next set bit, starting from `start`.
     ///
-    /// Returns `None` if `start` is greater or equal to `self.nbits`.
+    /// Returns `None` if no bits are set on or after the given index. The
+    /// index `start` must be in bounds.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// out of bounds.
     #[inline]
     pub fn next_bit(&self, start: usize) -> Option<usize> {
         bitmap_assert!(
@@ -479,7 +485,14 @@ pub fn next_bit(&self, start: usize) -> Option<usize> {
     }
 
     /// Finds next zero bit, starting from `start`.
-    /// Returns `None` if `start` is greater than or equal to `self.len()`.
+    ///
+    /// Returns `None` if no bits are zero on or after the given index. The
+    /// index `start` must be in bounds.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// out of bounds.
     #[inline]
     pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
         bitmap_assert!(

base-commit: 73e3f0710014fe6d4ed98cfc02292f6121db7558

Signature · 1 line
2.47.3
#1 lore raw
Quoted 65 lines · Georgios Androutsopoulos

next_bit() and next_zero_bit() use bitmap_assert!() to check that start is less than self.len(), which panics when CONFIG_RUST_BITMAP_HARDENED is enabled. However, neither function has a # Panics section, and both document that None is returned for exactly the input that triggers the panic.

Update the documentation of next_bit() and next_zero_bit() to add the missing # Panics sections, add the missing blank doc comment line before Returns in next_zero_bit(), and simplify the wording.

---
Changes in v3:
- Use Closes: instead of Link:, and add Fixes:, following feedback from
Alexandre Courbot and Miguel Ojeda.
- Link to v2: https://lore.kernel.org/rust-for-linux/[email protected]/

Changes in v2:
- Simplify doc wording following feedback from Alice Ryhl.
- Remove extra empty line between Link: and Signed-off-by:.
- Link to v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/kernel/bitmap.rs | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
rust/kernel/bitmap.rs +15 -2
index b27e0ec80d64..b51c8a96f1a8 100644
@@ -459,7 +459,13 @@ pub fn last_bit(&self) -> Option<usize> {
 
     /// Finds next set bit, starting from `start`.
     ///
-    /// Returns `None` if `start` is greater or equal to `self.nbits`.
+    /// Returns `None` if no bits are set on or after the given index. The
+    /// index `start` must be in bounds.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// out of bounds.
     #[inline]
     pub fn next_bit(&self, start: usize) -> Option<usize> {
         bitmap_assert!(
@@ -479,7 +485,14 @@ pub fn next_bit(&self, start: usize) -> Option<usize> {
     }
 
     /// Finds next zero bit, starting from `start`.
-    /// Returns `None` if `start` is greater than or equal to `self.len()`.
+    ///
+    /// Returns `None` if no bits are zero on or after the given index. The
+    /// index `start` must be in bounds.
+    ///
+    /// # Panics
+    ///
+    /// Panics if `CONFIG_RUST_BITMAP_HARDENED` is enabled and `start` is
+    /// out of bounds.
     #[inline]
     pub fn next_zero_bit(&self, start: usize) -> Option<usize> {
         bitmap_assert!(
Quoted 4 lines

base-commit: 73e3f0710014fe6d4ed98cfc02292f6121db7558 --
2.47.3

Hi,

Just checking if there are any remaining concerns with this patch, or if it can be picked up.

Thanks,
Georgios