public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Max R. Carrara" <m.carrara@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [PATCH proxmox-backup v2 07/10] tape: tape block: make `payload` field private
Date: Fri, 21 Aug 2026 16:02:31 +0200	[thread overview]
Message-ID: <20260821140238.615302-8-m.carrara@proxmox.com> (raw)
In-Reply-To: <20260821140238.615302-1-m.carrara@proxmox.com>

While all cases where we directly use the `payload` field are fine,
direct field access should IMO be used as sparingly as possible, as
the field *itself* is otherwise part of a type's public API.

Therefore, make the `payload` field of `TapeBlock` private and add
corresponding getters to allow obtaining it as a (mutable) slice.

Signed-off-by: Max R. Carrara <m.carrara@proxmox.com>
---
 pbs-tape/src/blocked_reader.rs | 11 +++++------
 pbs-tape/src/blocked_writer.rs | 13 ++++++++-----
 pbs-tape/src/tape_block.rs     | 12 +++++++++++-
 3 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/pbs-tape/src/blocked_reader.rs b/pbs-tape/src/blocked_reader.rs
index cf8150ba3..6b1e3c692 100644
--- a/pbs-tape/src/blocked_reader.rs
+++ b/pbs-tape/src/blocked_reader.rs
@@ -81,11 +81,11 @@ impl<R: BlockRead> BlockedReader<R> {
         let size = tape_block.size();
         let found_end_marker = tape_block.flags().contains(TapeBlockFlags::END_OF_STREAM);
 
-        if size > tape_block.payload.len() {
+        if size > tape_block.payload().len() {
             proxmox_lang::io_bail!(
                 "detected tape block with wrong payload size ({} > {}",
                 size,
-                tape_block.payload.len()
+                tape_block.payload().len()
             );
         } else if size == 0 && !found_end_marker {
             proxmox_lang::io_bail!("detected tape block with zero payload size");
@@ -130,7 +130,7 @@ impl<R: BlockRead> BlockedReader<R> {
             Ok(()) => { /* ok */ }
             Err(BlockReadError::EndOfFile) => {
                 self.got_eod = true;
-                self.read_pos = self.tape_block.payload.len();
+                self.read_pos = self.tape_block.payload().len();
                 if !self.found_end_marker && check_end_marker {
                     proxmox_lang::io_bail!("detected tape stream without end marker");
                 }
@@ -228,9 +228,8 @@ impl<R: BlockRead> Read for BlockedReader<R> {
             } else {
                 rest as usize
             };
-            buffer[..copy_len].copy_from_slice(
-                &self.tape_block.payload[self.read_pos..(self.read_pos + copy_len)],
-            );
+            let payload = self.tape_block.payload();
+            buffer[..copy_len].copy_from_slice(&payload[self.read_pos..(self.read_pos + copy_len)]);
             self.read_pos += copy_len;
             Ok(copy_len)
         }
diff --git a/pbs-tape/src/blocked_writer.rs b/pbs-tape/src/blocked_writer.rs
index 5e94f54b8..0d5d10147 100644
--- a/pbs-tape/src/blocked_writer.rs
+++ b/pbs-tape/src/blocked_writer.rs
@@ -69,16 +69,18 @@ impl<W: BlockWrite> BlockedWriter<W> {
             return Ok(0);
         }
 
-        let rest = self.tape_block.payload.len() - self.buffer_pos;
+        let payload = self.tape_block.payload_mut();
+        let payload_len = payload.len();
+
+        let rest = payload_len - self.buffer_pos;
         let bytes = if data.len() < rest { data.len() } else { rest };
-        self.tape_block.payload[self.buffer_pos..(self.buffer_pos + bytes)]
-            .copy_from_slice(&data[..bytes]);
+        payload[self.buffer_pos..(self.buffer_pos + bytes)].copy_from_slice(&data[..bytes]);
 
         let rest = rest - bytes;
 
         if rest == 0 {
             self.tape_block.set_flags(TapeBlockFlags::empty());
-            self.tape_block.set_size(self.tape_block.payload.len());
+            self.tape_block.set_size(payload_len);
             self.tape_block.set_seq_nr(self.seq_nr);
             self.seq_nr += 1;
             let leom = Self::write_block(&self.tape_block, &mut self.writer)?;
@@ -115,7 +117,8 @@ impl<W: BlockWrite> TapeWrite for BlockedWriter<W> {
     /// Note: This may write an empty block just including the
     /// END_OF_STREAM flag.
     fn finish(&mut self, incomplete: bool) -> Result<bool, std::io::Error> {
-        vec::clear(&mut self.tape_block.payload[self.buffer_pos..]);
+        let payload = self.tape_block.payload_mut();
+        vec::clear(&mut payload[self.buffer_pos..]);
         let mut flags = TapeBlockFlags::END_OF_STREAM;
         if incomplete {
             flags |= TapeBlockFlags::INCOMPLETE;
diff --git a/pbs-tape/src/tape_block.rs b/pbs-tape/src/tape_block.rs
index 2db5ed137..c73efafa7 100644
--- a/pbs-tape/src/tape_block.rs
+++ b/pbs-tape/src/tape_block.rs
@@ -33,7 +33,7 @@ static_assert_size!(TapeBlockHeader, 16);
 #[repr(C, packed)]
 pub struct TapeBlock {
     header: TapeBlockHeader,
-    pub payload: [u8],
+    payload: [u8],
 }
 
 bitflags! {
@@ -107,4 +107,14 @@ impl TapeBlock {
     pub fn set_seq_nr(&mut self, seq_nr: u32) {
         self.header.seq_nr = seq_nr.to_le();
     }
+
+    /// Returns the tape block's data payload as a byte slice.
+    pub fn payload(&self) -> &[u8] {
+        &self.payload
+    }
+
+    /// Returns the tape block's data payload as a mutable byte slice.
+    pub fn payload_mut(&mut self) -> &mut [u8] {
+        &mut self.payload
+    }
 }
-- 
2.47.3





  parent reply	other threads:[~2026-08-21 14:03 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-21 14:02 [PATCH proxmox{,-backup} v2 00/10] Fix Undefined Behavior in Tape Block Header Deallocation Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox v2 01/10] proxmox-alloc: introduce proxmox-alloc with `LayoutAwareBox<T>` type Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox v2 02/10] proxmox-alloc: document undefined behavior regarding custom allocs Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 03/10] tape: move tape block structs into separate file module Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 04/10] tape: rename `BlockHeader` and `BlockHeaderFlags` Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 05/10] tape: blocked_{reader,writer}: rename `buffer` to `tape_block` Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 06/10] tape: tape block: represent tape block header with its own struct Max R. Carrara
2026-08-21 14:02 ` Max R. Carrara [this message]
2026-08-21 14:02 ` [PATCH proxmox-backup v2 08/10] tape: blocked_{reader,writer}: remove haphazard `unsafe` blocks Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 09/10] tape: tape block: fix undefined behavior on tape block deallocation Max R. Carrara
2026-08-21 14:02 ` [PATCH proxmox-backup v2 10/10] tape: sgutils2: fix undefined behavior in dealloc of buffer Max R. Carrara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260821140238.615302-8-m.carrara@proxmox.com \
    --to=m.carrara@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal