Check that element is a nullptr before casting.

The bug was caused by the fact that the `mjs_as*` functions would return a pointer to a `mjC*` object, even if the input `mjsElement` pointer was null. This would cause a segfault.

PiperOrigin-RevId: 651802736
Change-Id: Ibec00d269a4befadff8a68e0513fd381c7e38a2a
This commit is contained in:
Alessio Quaglino
2024-07-12 09:45:18 -07:00
committed by Copybara-Service
parent e76f3ce1e3
commit 5d51b4cd5f
2 changed files with 108 additions and 23 deletions
+16
View File
@@ -234,5 +234,21 @@ class SpecsTest(absltest.TestCase):
self.assertEqual(spec.sensors[1].name, 'sensor2')
self.assertEqual(spec.sensors[2].name, 'sensor3')
def test_iterators(self):
spec = mujoco.MjSpec()
geom1 = spec.worldbody.add_geom()
geom2 = spec.worldbody.add_geom()
geom3 = spec.worldbody.add_geom()
geom1.name = 'geom1'
geom2.name = 'geom2'
geom3.name = 'geom3'
geom = spec.worldbody.first_geom()
i = 1
while geom:
self.assertEqual(geom.name, 'geom' + str(i))
geom = spec.worldbody.next_geom(geom)
i += 1
if __name__ == '__main__':
absltest.main()