[PATCH] misc: Report insufficient fcvt_r buffer space
Muhammed Sariyildiz
asiyee994@gmail.com
Fri Nov 20 06:58:46 GMT 2026
Return an error when the trailing zeroes required for negative precision
cannot fit in the caller-provided buffer. Add tests for insufficient and
exactly sufficient buffer sizes.
Signed-off-by: Muhammed Sariyildiz <asiyee994@gmail.com>
---
misc/efgcvt_r-template.c | 13 +++++++------
misc/tst-efgcvt-template.c | 15 +++++++++++++++
2 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/misc/efgcvt_r-template.c b/misc/efgcvt_r-template.c
index e1d28c0521..47ba4c926e 100644
--- a/misc/efgcvt_r-template.c
+++ b/misc/efgcvt_r-template.c
@@ -117,12 +117,13 @@ __FCVT_R (FLOAT_TYPE value, int ndigit, int *decpt, int *sign,
if (left)
{
*decpt += left;
- if ((ssize_t) --len > n)
- {
- while (left-- > 0 && n < (ssize_t) len)
- buf[n++] = '0';
- buf[n] = '\0';
- }
+ /* Not enough room for the trailing zeroes and the terminating null
+ byte. */
+ if ((size_t) n + (size_t) left >= len)
+ return -1;
+ while (left-- > 0)
+ buf[n++] = '0';
+ buf[n] = '\0';
}
return 0;
diff --git a/misc/tst-efgcvt-template.c b/misc/tst-efgcvt-template.c
index 7ce40efecc..2f0c9afc8c 100644
--- a/misc/tst-efgcvt-template.c
+++ b/misc/tst-efgcvt-template.c
@@ -217,6 +217,21 @@ special (void)
printf (NAME (FCVT_R) " with a too small buffer was successful.\n");
support_record_failure ();
}
+
+ res = FCVT_R (100.0, -2, &decpt, &sign, buf, 3);
+ if (res == 0)
+ {
+ printf (NAME (FCVT_R) " with insufficient room for trailing zeroes "
+ "was successful.\n");
+ support_record_failure ();
+ }
+
+ res = FCVT_R (100.0, -2, &decpt, &sign, buf, 4);
+ if (res != 0 || strcmp (buf, "100") != 0 || decpt != 3)
+ {
+ printf (NAME (FCVT_R) " failed with an exactly sized buffer.\n");
+ support_record_failure ();
+ }
}
--
2.56.1
More information about the Libc-alpha
mailing list