[openssh-commits] [openssh] 06/87: fix BROKEN_SNPRINTF replacement for max-width ints
git+noreply at mindrot.org
git+noreply at mindrot.org
Fri Oct 30 20:20:43 AEDT 2026
This is an automated email from the git hooks/post-receive script.
djm pushed a commit to branch master
in repository openssh.
commit 74551008cc2f1ec5f34a7c5b5b1aa62df52cecdd
Author: Damien Miller <djm at mindrot.org>
AuthorDate: Fri Oct 30 18:56:22 2026 +1100
fix BROKEN_SNPRINTF replacement for max-width ints
Previous implementation incorrectly truncating integers that needed more
than 19 decimal places.
Anthropic ANT-2026-LRFWCWHP
---
openbsd-compat/bsd-snprintf.c | 5 ++---
openbsd-compat/regress/snprintftest.c | 10 +++++++++-
2 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/openbsd-compat/bsd-snprintf.c b/openbsd-compat/bsd-snprintf.c
index 56160eeca..2ee173fd6 100644
--- a/openbsd-compat/bsd-snprintf.c
+++ b/openbsd-compat/bsd-snprintf.c
@@ -544,7 +544,7 @@ fmtint(char *buffer, size_t *currlen, size_t maxlen,
{
int signvalue = 0;
unsigned LLONG uvalue;
- char convert[20];
+ char convert[sizeof(uvalue) * CHAR_BIT / 3 + 2]; /* octal digits + NUL */
int place = 0;
int spadlen = 0; /* amount to space pad */
int zpadlen = 0; /* amount to zero pad */
@@ -574,8 +574,7 @@ fmtint(char *buffer, size_t *currlen, size_t maxlen,
(caps? "0123456789ABCDEF":"0123456789abcdef")
[uvalue % (unsigned)base ];
uvalue = (uvalue / (unsigned)base );
- } while(uvalue && (place < 20));
- if (place == 20) place--;
+ } while(uvalue && (place < (int)sizeof(convert) - 1));
convert[place] = 0;
zpadlen = max - place;
diff --git a/openbsd-compat/regress/snprintftest.c b/openbsd-compat/regress/snprintftest.c
index 23b7dfac8..8d844289d 100644
--- a/openbsd-compat/regress/snprintftest.c
+++ b/openbsd-compat/regress/snprintftest.c
@@ -48,7 +48,7 @@ int x_snprintf(char *str, size_t count, const char *fmt, ...)
int
main(void)
{
- char b[5];
+ char b[5], n[32];
char *src = NULL;
int ret;
@@ -73,6 +73,14 @@ main(void)
if (x_snprintf(b, 1, "%s %d", "hello", 12345) != 11)
fail("vsnprintf does not return required length");
+ /* check that the largest 64-bit values are printed in full */
+ ret = snprintf(n, sizeof(n), "%llu", 0xffffffffffffffffULL);
+ if (ret != 20 || strcmp(n, "18446744073709551615") != 0)
+ fail("snprintf truncates 20-digit unsigned values");
+ ret = snprintf(n, sizeof(n), "%llo", 0xffffffffffffffffULL);
+ if (ret != 22 || strcmp(n, "1777777777777777777777") != 0)
+ fail("snprintf truncates 22-digit octal values");
+
free(src);
return failed;
}
--
To stop receiving notification emails like this one, please contact
djm at mindrot.org.
More information about the openssh-commits
mailing list