fix: range-check justify width before narrowing to LBUF_OFFSET

centerjustcombo() cast the parsed width to LBUF_OFFSET (uint16_t at
LBUF_SIZE 32768) before the LBUF_SIZE range check, so widths wrapped
mod 65536: ljust(x,999999) silently produced a 16959-column result
while ljust(x,100000) errored.  Parse into a long, range-check, then
narrow.  Negative widths still return #-1 OUT OF RANGE and zero still
returns empty, as before.  Affects ljust(), rjust(), and center().

Found while probing string-boundary behavior for #860.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Stephen Dennis 2026-07-10 01:48:39 -06:00
parent 6c2bd9cf92
commit b8f11142bc

View file

@ -10187,17 +10187,21 @@ static void centerjustcombo
{
return;
}
LBUF_OFFSET nWidth = static_cast<LBUF_OFFSET>(mux_atol(strip_color(fargs[1])));
if (0 == nWidth)
// Range-check the width before narrowing to LBUF_OFFSET (uint16_t
// at this LBUF_SIZE) — casting first wraps widths mod 65536, letting
// e.g. 999999 slip under the limit as 16959 (#860).
long lWidth = mux_atol(strip_color(fargs[1]));
if (0 == lWidth)
{
return;
}
if (LBUF_SIZE <= nWidth)
if (lWidth < 0 || LBUF_SIZE <= lWidth)
{
safe_range(buff, bufc);
return;
}
LBUF_OFFSET nWidth = static_cast<LBUF_OFFSET>(lWidth);
const unsigned char *pStr = reinterpret_cast<const unsigned char *>(fargs[0]);
size_t lenStr = strlen(reinterpret_cast<const char *>(pStr));