Skip to content

Commit 40ab0aa

Browse files
committed
[ntuple] Throw instead of asserting in RMiniFileReader::ReadBuffer
1 parent 11d6dd9 commit 40ab0aa

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

tree/ntuple/inc/ROOT/RMiniFile.hxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ public:
8686
/// Reads a given byte range from the file into the provided memory buffer.
8787
/// If `nbytes > fMaxKeySize` it will perform chunked read from multiple blobs,
8888
/// whose addresses are listed at the end of the first chunk.
89+
/// \throw ROOT::RException if the read fails.
8990
void ReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset);
91+
/// Like ReadBuffer but returns a RResult instead of throwing.
92+
ROOT::RResult<void> TryReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset);
9093
/// Attempts to load the streamer info from the file.
9194
void LoadStreamerInfo();
9295

tree/ntuple/src/RMiniFile.cxx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,16 @@ ROOT::RResult<ROOT::RNTuple> ROOT::Internal::RMiniFileReader::GetNTupleBare(std:
887887

888888
void ROOT::Internal::RMiniFileReader::ReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset)
889889
{
890+
TryReadBuffer(buffer, nbytes, offset).ThrowOnError();
891+
}
892+
893+
ROOT::RResult<void> ROOT::Internal::RMiniFileReader::TryReadBuffer(void *buffer, size_t nbytes, std::uint64_t offset)
894+
{
895+
const auto ByteReadErr = [](std::size_t expected, std::size_t nread) {
896+
return R__FAIL("invalid read (expected bytes: " + std::to_string(expected) + ", read: " + std::to_string(nread) +
897+
")");
898+
};
899+
890900
size_t nread;
891901
if (fMaxKeySize == 0 || nbytes <= fMaxKeySize) {
892902
// Fast path: read single blob
@@ -900,7 +910,9 @@ void ROOT::Internal::RMiniFileReader::ReadBuffer(void *buffer, size_t nbytes, st
900910

901911
// Read first chunk
902912
nread = fRawFile->ReadAt(bufCur, fMaxKeySize, offset);
903-
R__ASSERT(nread == fMaxKeySize);
913+
if (nread != fMaxKeySize)
914+
return ByteReadErr(fMaxKeySize, nread);
915+
904916
// NOTE: we read the entire chunk in `bufCur`, but we only advance the pointer by `nbytesFirstChunk`,
905917
// since the last part of `bufCur` will later be overwritten by the next chunk's payload.
906918
// We do this to avoid a second ReadAt to read in the chunk offsets.
@@ -923,14 +935,19 @@ void ROOT::Internal::RMiniFileReader::ReadBuffer(void *buffer, size_t nbytes, st
923935
R__ASSERT(static_cast<size_t>(bufCur - reinterpret_cast<uint8_t *>(buffer)) <= nbytes - bytesToRead);
924936

925937
auto nbytesRead = fRawFile->ReadAt(bufCur, bytesToRead, chunkOffset);
926-
R__ASSERT(nbytesRead == bytesToRead);
938+
if (nbytesRead != bytesToRead)
939+
return ByteReadErr(bytesToRead, nbytesRead);
927940

928941
nread += bytesToRead;
929942
bufCur += bytesToRead;
930943
remainingBytes -= bytesToRead;
931944
} while (remainingBytes > 0);
932945
}
933-
R__ASSERT(nread == nbytes);
946+
947+
if (nread != nbytes)
948+
return ByteReadErr(nbytes, nread);
949+
950+
return RResult<void>::Success();
934951
}
935952

936953
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)