Skip to content

Commit f748018

Browse files
committed
Fix two asan detected memory bugs
I am running through simulate.swift (my port of simulate.cc) with address sanitizer to discover related bugs. Besides ones in my port, there are two in MuJoCo: 1. in maketext, the logic to find . is not protected against j is less than 0 (due to the decreasing logic above), creating out of bound access. 2. in mj_printFormattedData, qfrc_applied should use length nv not nq, otherwise out of bound access could be triggered. Test Plan: Run through the simulate.cc with asan. Before this fix, when presenting profiler view, it will trigger bug google-deepmind#1. When print data, it will trigger bug google-deepmind#2. Both are using model/humanoid/22_humanoids.xml.
1 parent d7ae7f5 commit f748018

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/engine/engine_print.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ void mj_printFormattedData(const mjModel* m, mjData* d, const char* filename,
787787
printArray("ACT", m->na, 1, d->act, fp, float_format);
788788
printArray("QACC_WARMSTART", m->nv, 1, d->qacc_warmstart, fp, float_format);
789789
printArray("CTRL", m->nu, 1, d->ctrl, fp, float_format);
790-
printArray("QFRC_APPLIED", m->nq, 1, d->qfrc_applied, fp, float_format);
790+
printArray("QFRC_APPLIED", m->nv, 1, d->qfrc_applied, fp, float_format);
791791
printArray("XFRC_APPLIED", m->nbody, 6, d->xfrc_applied, fp, float_format);
792792
printArray("MOCAP_POS", m->nmocap, 3, d->mocap_pos, fp, float_format);
793793
printArray("MOCAP_QUAT", m->nmocap, 4, d->mocap_quat, fp, float_format);

src/render/render_gl2.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ static void maketext(const char* format, char* txt, float num, int txt_sz) {
711711
}
712712

713713
// '.' found: strip
714-
if (txt[j]=='.') {
714+
if (j>=0 && txt[j]=='.') {
715715
txt[i] = 0;
716716
}
717717
}

0 commit comments

Comments
 (0)