Skip to content

Commit a20c8cd

Browse files
committed
[ntuple] Detect entry-model mismatch in LoadEntry
1 parent 5a8a119 commit a20c8cd

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

tree/ntuple/v7/inc/ROOT/RNTupleReader.hxx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,13 @@ public:
234234
LoadEntry(index, fModel->GetDefaultEntry());
235235
}
236236
/// Fills a user provided entry after checking that the entry has been instantiated from the ntuple model
237-
void LoadEntry(NTupleSize_t index, REntry &entry) { entry.Read(index); }
237+
void LoadEntry(NTupleSize_t index, REntry &entry)
238+
{
239+
if (R__unlikely(entry.GetModelId() != fModel->GetModelId()))
240+
throw RException(R__FAIL("mismatch between entry and model"));
241+
242+
entry.Read(index);
243+
}
238244

239245
/// Returns an iterator over the entry indices of the RNTuple.
240246
///

tree/ntuple/v7/test/ntuple_basics.cxx

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -547,27 +547,43 @@ TEST(RNTuple, ModelId)
547547

548548
TEST(RNTuple, Entry)
549549
{
550-
auto m1 = RNTupleModel::Create();
550+
auto m = RNTupleModel::Create();
551551
try {
552-
m1->CreateEntry();
552+
m->CreateEntry();
553553
FAIL() << "creating entry of unfrozen model should throw";
554554
} catch (const ROOT::RException &err) {
555555
EXPECT_THAT(err.what(), testing::HasSubstr("invalid attempt to create entry"));
556556
}
557-
m1->Freeze();
558-
auto e1 = m1->CreateEntry();
557+
m->Freeze();
558+
auto e = m->CreateEntry();
559559

560-
auto m2 = RNTupleModel::Create();
561-
m2->Freeze();
562-
auto e2 = m2->CreateEntry();
560+
auto mWrite = m->Clone();
561+
mWrite->Freeze();
562+
auto eWrite = mWrite->CreateEntry();
563563

564564
FileRaii fileGuard("test_ntuple_entry.root");
565-
auto ntuple = RNTupleWriter::Recreate(std::move(m1), "ntpl", fileGuard.GetPath());
566-
ntuple->Fill();
567-
ntuple->Fill(*e1);
565+
{
566+
auto ntuple = RNTupleWriter::Recreate(std::move(mWrite), "ntpl", fileGuard.GetPath());
567+
ntuple->Fill();
568+
ntuple->Fill(*eWrite);
569+
try {
570+
ntuple->Fill(*e);
571+
FAIL() << "filling with wrong entry should throw";
572+
} catch (const ROOT::RException &err) {
573+
EXPECT_THAT(err.what(), testing::HasSubstr("mismatch between entry and model"));
574+
}
575+
}
576+
577+
auto mRead = m->Clone();
578+
mRead->Freeze();
579+
auto eRead = mRead->CreateEntry();
580+
581+
auto ntuple = RNTupleReader::Open(std::move(mRead), "ntpl", fileGuard.GetPath());
582+
ntuple->LoadEntry(0);
583+
ntuple->LoadEntry(0, *eRead);
568584
try {
569-
ntuple->Fill(*e2);
570-
FAIL() << "filling with wrong entry should throw";
585+
ntuple->LoadEntry(0, *e);
586+
FAIL() << "loading the wrong entry should throw";
571587
} catch (const ROOT::RException &err) {
572588
EXPECT_THAT(err.what(), testing::HasSubstr("mismatch between entry and model"));
573589
}

0 commit comments

Comments
 (0)