Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions io/io/inc/ROOT/RFile.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ public:
/// Puts object `namedObj` in the file under the path `dirPath/namedObj.GetName()`, giving it a title equal to
/// `namedObj.GetTitle()`.
/// If `dirPath` is empty (default), the object is put at the top level of the file.
///
/// \throws ROOT::RException if `dirPath/namedObj.GetName()` results in an invalid object path. Notably, this
/// includes the case where `namedObj.GetName()` contains a slash character.
Comment thread
pcanal marked this conversation as resolved.
Resolved
void Put(const class TNamed &namedObj, std::string_view dirPath = "");

/// Puts an object into the file, overwriting any previously-existing object at that path.
Expand Down
4 changes: 4 additions & 0 deletions io/io/src/RFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ void *RFile::GetUntyped(std::string_view path,

void RFile::Put(const TNamed &namedObj, std::string_view dirPath)
{
if (strchr(namedObj.GetName(), '/')) {
throw ROOT::RException(R__FAIL(std::string("TNamed object '") + namedObj.GetName() +
"' given to RFile::Put cannot have '/' in its name!"));
}
std::string completePath = (dirPath.empty() ? "" : std::string(dirPath) + '/') + namedObj.GetName();
Put(completePath, namedObj, namedObj.GetTitle());
}
Expand Down
36 changes: 36 additions & 0 deletions io/io/test/rfile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -931,3 +931,39 @@ TEST(RFile, PutTNamed)
EXPECT_EQ(key2.GetTitle(), "title 2");
}
}

TEST(RFile, PutTNamedInvalid)
{
FileRaii fileGuard("test_rfile_put_tnamed_invalid.root");

{
TNamed named1("my/funny/name", "title 1");
TNamed named2("", "title 2");
TNamed named3("invalid.name", "title 3");
auto file = RFile::Recreate(fileGuard.GetPath());
try {
file->Put(named1);
FAIL() << "putting objects with a slash in their name should fail.";
} catch (const ROOT::RException &ex) {
EXPECT_THAT(ex.what(), testing::HasSubstr("cannot have '/'"));
}
try {
file->Put(named2, "my/dir");
FAIL() << "putting objects with an empty name should fail.";
} catch (const ROOT::RException &ex) {
EXPECT_THAT(ex.what(), testing::HasSubstr("Invalid object path"));
}
try {
file->Put(named2);
FAIL() << "putting objects with an empty name should fail.";
} catch (const ROOT::RException &ex) {
EXPECT_THAT(ex.what(), testing::HasSubstr("Invalid object path"));
}
try {
file->Put(named3);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is invalid because of the . character in the name? I'm not opposed to it, I just wonder because it's not included in the PR/commit description.

FAIL() << "putting objects with an invalid name should fail.";
} catch (const ROOT::RException &ex) {
EXPECT_THAT(ex.what(), testing::HasSubstr("Invalid object path"));
}
}
}
Loading