From 10c7207ceaf9669d0cd3c0dfce6974bc796da6c0 Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Wed, 15 Jan 2025 05:08:48 -0800 Subject: [PATCH] Process asset path before adding VFS buffer. Also, handle VFS errors. PiperOrigin-RevId: 715755127 Change-Id: Iaf455d448f16e37ab89ceaa5797319135acd73c9 --- python/mujoco/specs.cc | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/python/mujoco/specs.cc b/python/mujoco/specs.cc index d5141765..f8167182 100644 --- a/python/mujoco/specs.cc +++ b/python/mujoco/specs.cc @@ -400,11 +400,22 @@ PYBIND11_MODULE(_specs, m) { } mjVFS vfs; mj_defaultVFS(&vfs); - for (auto item : self.assets) { - std::string buffer = py::cast(item.second); - mj_addBufferVFS(&vfs, py::cast(item.first).c_str(), - buffer.c_str(), buffer.size()); - }; + for (const auto& asset : self.assets) { + std::string buffer_name = + _impl::StripPath(py::cast(asset.first).c_str()); + std::string buffer = py::cast(asset.second); + const int vfs_error = InterceptMjErrors(mj_addBufferVFS)( + &vfs, buffer_name.c_str(), buffer.c_str(), buffer.size()); + if (vfs_error) { + mj_deleteVFS(&vfs); + if (vfs_error == 2) { + throw py::value_error("Repeated file name in assets dict: " + + buffer_name); + } else { + throw py::value_error("Asset failed to load: " + buffer_name); + } + } + } auto model = mjmodel_from_spec_ptr(reinterpret_cast(self.ptr), reinterpret_cast(&vfs));