From c962188655923aae9bb94f3ed48e792e845bf627 Mon Sep 17 00:00:00 2001 From: ashutosh0x Date: Wed, 11 Feb 2026 22:33:28 +0530 Subject: [PATCH 1/2] Fix zero-sized allocation crash on Windows On Windows, _aligned_malloc(0) returns NULL, which MuJoCo interprets as a fatal out-of-memory error. This causes crashes on models with empty arrays. Ensure a minimum allocation of 64 bytes for any 0-sized request. Fixes #2992 --- src/engine/engine_util_errmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/engine/engine_util_errmem.c b/src/engine/engine_util_errmem.c index cbd373d3..1920bdf8 100644 --- a/src/engine/engine_util_errmem.c +++ b/src/engine/engine_util_errmem.c @@ -207,8 +207,10 @@ void* mju_malloc(size_t size) { // default allocator else { // pad size to multiple of 64 - if ((size%64)) { - size += 64 - (size%64); + if (size == 0) { + size = 64; + } else if ((size % 64)) { + size += 64 - (size % 64); } // allocate From 04e5df08a9f41fc3e7e493b9249645583e83eee8 Mon Sep 17 00:00:00 2001 From: ashutosh0x Date: Fri, 13 Feb 2026 19:22:05 +0530 Subject: [PATCH 2/2] Update mju_malloc to return NULL for zero-sized allocations --- src/engine/engine_util_errmem.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/engine/engine_util_errmem.c b/src/engine/engine_util_errmem.c index 1920bdf8..f06a2830 100644 --- a/src/engine/engine_util_errmem.c +++ b/src/engine/engine_util_errmem.c @@ -207,18 +207,18 @@ void* mju_malloc(size_t size) { // default allocator else { // pad size to multiple of 64 - if (size == 0) { - size = 64; - } else if ((size % 64)) { + if (size > 0 && (size % 64)) { size += 64 - (size % 64); } // allocate - ptr = mju_alignedMalloc(size, 64); + if (size > 0) { + ptr = mju_alignedMalloc(size, 64); + } } // error if null pointer - if (!ptr) { + if (!ptr && size > 0) { mju_error("Could not allocate memory"); }