Null out mjData.buffer and mjData.stack before possibly calling mj_deleteData.

While creating an mjData, any error triggers mj_deleteData. If buffer or stack is not NULL at that point, an uninitialized pointer will be freed.

PiperOrigin-RevId: 465378329
Change-Id: I9be0eef0648e05e3e5f1361da346a5046485cde7
This commit is contained in:
Nimrod Gileadi
2022-08-04 12:36:07 -07:00
committed by Copybara-Service
parent 0e8fd182de
commit 4268d81b55
2 changed files with 28 additions and 1 deletions
+4 -1
View File
@@ -838,6 +838,7 @@ static mjData* _makeData(const mjModel* m) {
// compute buffer size
d->nbuffer = 0;
d->buffer = d->stack = NULL;
#define X(type, name, nr, nc) \
if (!safeAddToBufferSize(&offset, &d->nbuffer, sizeof(type), m->nr, nc)) { \
mju_warning("Invalid data: " #name " too large."); \
@@ -871,7 +872,9 @@ static mjData* _makeData(const mjModel* m) {
mjData* mj_makeData(const mjModel* m) {
mjData* d = _makeData(m);
mj_resetData(m, d);
if (d) {
mj_resetData(m, d);
}
return d;
}