From 9738c6c19f0abe170af1385e3a145186693cee76 Mon Sep 17 00:00:00 2001 From: Alice Date: Tue, 27 Oct 2026 16:26:10 +0100 Subject: [PATCH] add check to not read more frames than available --- include/aare/MultiThreadedFileReader.hpp | 7 ++++++- src/MultiThreadedFileReader.cpp | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/include/aare/MultiThreadedFileReader.hpp b/include/aare/MultiThreadedFileReader.hpp index d7a930b8..b4542bdc 100644 --- a/include/aare/MultiThreadedFileReader.hpp +++ b/include/aare/MultiThreadedFileReader.hpp @@ -40,12 +40,17 @@ class MultiThreadedFileReader { /** * @brief Read one chunk per active worker into a caller-owned buffer. * + * @param destination Pointer to the buffer where the data will be written. + * @param frames_to_read Optional number of frames to read. If not provided, + * the reader will read the next available frames. + * * The buffer must hold at least next_read_bytes() bytes. The reader's * position advances by the returned number of frames. At the end of the * configured range this function returns zero and does not access the * destination. */ - size_t read_into(std::byte *destination); + size_t read_into(std::byte *destination, + std::optional frames_to_read = std::nullopt); /** @brief Read the next wave of chunks into an owned byte buffer. */ std::vector read(); diff --git a/src/MultiThreadedFileReader.cpp b/src/MultiThreadedFileReader.cpp index 6810e9bf..ace48998 100644 --- a/src/MultiThreadedFileReader.cpp +++ b/src/MultiThreadedFileReader.cpp @@ -85,9 +85,19 @@ void MultiThreadedFileReader::ensure_open() const { } } -size_t MultiThreadedFileReader::read_into(std::byte *destination) { +size_t +MultiThreadedFileReader::read_into(std::byte *destination, + std::optional frames_to_read_) { ensure_open(); - const size_t frames_to_read = next_read_frames(); + const size_t next_frames_to_read = next_read_frames(); + const size_t frames_to_read = frames_to_read_.value_or(next_frames_to_read); + + if (frames_to_read > next_frames_to_read) { + throw std::invalid_argument( + fmt::format("MultiThreadedFileReader cannot read more frames than " + "available: requested {}, available {}", + frames_to_read, next_frames_to_read)); + } if (frames_to_read == 0) { return 0; }