diff options
| author | Hauke Mehrtens | 2026-04-08 22:49:03 +0000 |
|---|---|---|
| committer | Hauke Mehrtens | 2026-06-18 22:06:48 +0000 |
| commit | 320322291ece1db97c5b05ceb97fd0f5776c5398 (patch) | |
| tree | 265731b7478af8b5a880d670c8fb4eb3f5e07245 | |
| parent | 9db2171daf28d7ad0b3bcaa7477f3f9ff8077ad8 (diff) | |
| download | libubox-320322291ece1db97c5b05ceb97fd0f5776c5398.tar.gz | |
udebug: fix double off-by-one in udebug_entry_vprintf()
Two off-by-one errors cause silent string truncation:
1. The check after the first vsnprintf uses <= instead of <. When the
output needs exactly UDEBUG_MIN_ALLOC_LEN chars, vsnprintf returns
that count to indicate truncation, but the <= condition skips the
reallocation path, silently using the truncated result.
2. After reallocating len+1 bytes, the second vsnprintf is called with
len as the size limit. Since vsnprintf writes at most size-1 chars
plus a null terminator, the last character is always lost. Pass
len+1 to use the full allocated buffer.
Link: https://github.com/openwrt/libubox/pull/42
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
(cherry picked from commit 406e342bb900cda5ce56481895db85a86a8f5dd7)
| -rw-r--r-- | udebug.c | 4 |
1 files changed, 2 insertions, 2 deletions
@@ -625,14 +625,14 @@ int udebug_entry_vprintf(struct udebug_buf *buf, const char *fmt, va_list ap) va_copy(ap2, ap); len = vsnprintf(str, UDEBUG_MIN_ALLOC_LEN, fmt, ap2); va_end(ap2); - if (len <= UDEBUG_MIN_ALLOC_LEN) + if (len < UDEBUG_MIN_ALLOC_LEN) goto out; if (ptr->len + len > buf->data_size / 2) return -1; udebug_buf_alloc(buf, ofs, len + 1); - len = vsnprintf(str, len, fmt, ap); + len = vsnprintf(str, len + 1, fmt, ap); out: ptr->len += len; |