From ac4874226c29e182b2b4512508e43c1a969a6729 Mon Sep 17 00:00:00 2001 From: Kyle Bayes Date: Mon, 15 Sep 2025 05:59:06 -0700 Subject: [PATCH] Support NULL in dir parameter in mj_addFileVFS. Fixes #2839. PiperOrigin-RevId: 807208005 Change-Id: I8def3b1e388dc19fa2fa8a92d53abef30dee5b88 --- src/user/user_vfs.cc | 3 ++- test/user/user_vfs_test.cc | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/user/user_vfs.cc b/src/user/user_vfs.cc index 6f364581..80e1f323 100644 --- a/src/user/user_vfs.cc +++ b/src/user/user_vfs.cc @@ -219,7 +219,8 @@ int mj_addFileVFS(mjVFS* vfs, const char* directory, const char* filename) { VFS* cvfs = GetVFSImpl(vfs); // make full name - FilePath fullname = FilePath(directory, filename); + const char* dir = directory != nullptr ? directory : ""; + FilePath fullname = FilePath(dir, filename); // strip path FilePath newname = StripPath(filename); diff --git a/test/user/user_vfs_test.cc b/test/user/user_vfs_test.cc index 2210c8d6..12ca78f2 100644 --- a/test/user/user_vfs_test.cc +++ b/test/user/user_vfs_test.cc @@ -106,6 +106,18 @@ TEST_F(UserVfsTest, AddFileStripPath) { mj_deleteVFS(&vfs); } +TEST_F(UserVfsTest, NullDirectory) { + mjVFS vfs; + mj_defaultVFS(&vfs); + + const std::string file = "engine/testdata/actuation/activation.xml"; + const std::string path = GetTestDataFilePath(file); + mj_addFileVFS(&vfs, nullptr, path.c_str()); + + EXPECT_TRUE(HasFile(&vfs, "activation.xml")); + mj_deleteVFS(&vfs); +} + TEST_F(UserVfsTest, AddFileRepeat) { mjVFS vfs; mj_defaultVFS(&vfs);