Improve bounds checking in engine_util_solve.c.
Add checks to ensure `snprintf` does not write beyond the allocated log buffer size, preventing buffer overflows. Fixes #2661, #2673 PiperOrigin-RevId: 853643867 Change-Id: Ibdc47810158466dc961767284ac297df98916dac
This commit is contained in:
committed by
Copybara-Service
parent
a92574837e
commit
8f5eb1e4f4
@@ -1534,12 +1534,15 @@ int mju_boxQPoption(mjtNum* res, mjtNum* R, int* index, // outputs
|
|||||||
|
|
||||||
|
|
||||||
// print iteration info
|
// print iteration info
|
||||||
if (log) {
|
if (log && logptr < logsz) {
|
||||||
logptr += snprintf(log+logptr, logsz-logptr,
|
int written = snprintf(log+logptr, logsz-logptr,
|
||||||
"iter %-3d: |grad|: %-8.2g reduction: %-8.2g improvement: %-8.4g "
|
"iter %-3d: |grad|: %-8.2g reduction: %-8.2g improvement: %-8.4g "
|
||||||
"linesearch: %g^%-2d factorized: %d nfree: %d\n",
|
"linesearch: %g^%-2d factorized: %d nfree: %d\n",
|
||||||
iter+1, mju_sqrt(norm2), oldvalue-value, improvement,
|
iter+1, mju_sqrt(norm2), oldvalue-value, improvement,
|
||||||
backtrack, nstep-1, factorize, nfree);
|
backtrack, nstep-1, factorize, nfree);
|
||||||
|
if (written > 0) {
|
||||||
|
logptr = mjMIN(logptr + written, logsz);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// accept candidate
|
// accept candidate
|
||||||
@@ -1552,7 +1555,7 @@ int mju_boxQPoption(mjtNum* res, mjtNum* R, int* index, // outputs
|
|||||||
}
|
}
|
||||||
|
|
||||||
// print final info
|
// print final info
|
||||||
if (log) {
|
if (log && logptr < logsz) {
|
||||||
snprintf(log+logptr, logsz-logptr, "BOXQP: %s.\n"
|
snprintf(log+logptr, logsz-logptr, "BOXQP: %s.\n"
|
||||||
"iterations= %d, factorizations= %d, |grad|= %-12.6g, final value= %-12.6g\n",
|
"iterations= %d, factorizations= %d, |grad|= %-12.6g, final value= %-12.6g\n",
|
||||||
status_string[status+1], iter, nfactor, mju_sqrt(norm2), value);
|
status_string[status+1], iter, nfactor, mju_sqrt(norm2), value);
|
||||||
|
|||||||
Reference in New Issue
Block a user