major formatted output function bug with %c and the value 0
marco.bodrato at tutanota.com
marco.bodrato at tutanota.com
Wed Dec 13 20:03:13 CET 2023
Ciao,
13 dic 2023, 15:53 da vincent at vinc17.net:
> On 2023-12-03 20:19:10 +0100, Vincent Lefevre wrote:
>
>> With GMP 6.3.0, the formatted output functions do not handle %c
>> with the value 0 correctly. For gmp_sprintf, the return value is
>> incorrect.
>>
> In printf/sprintffuns.c, function gmp_sprintf_format(), I suppose that
>
> vsprintf (buf, fmt, ap);
> ret = strlen (buf);
>
> should actually be something like
>
> ret = vsprintf (buf, fmt, ap);
> if (ret < 0)
> ret = 0;
>
> to avoid issues due to non-terminating null characters (not tested).
>
It was changed in 2001, probably a workaround, because the comment was
"Don't use sprintf return value (it's a pointer on SunOS 4)"
https://gmplib.org/repo/gmp/rev/0889877bb94a
Maybe we should simply "revert" that change, and use the return value both from sprintf (in printf/doprntf.c) and from vsprintf (in printf/sprintffuns.c)?
Or, if we care not to modify the pointer bufp, we can use something like the following:
diff -r f6073853d16a printf/sprintffuns.c
--- a/printf/sprintffuns.c Mon Oct 16 08:16:06 2023 +0200
+++ b/printf/sprintffuns.c Wed Dec 13 19:53:50 2023 +0100
@@ -53,9 +53,9 @@
{
char *buf = *bufp;
int ret;
- vsprintf (buf, fmt, ap);
- ret = strlen (buf);
- *bufp = buf + ret;
+ ret = vsprintf (buf, fmt, ap);
+ if (ret > 0)
+ *bufp = buf + ret;
return ret;
}
It passes the test suite, but I didn't really think about what it does.
Ĝis,
mb
More information about the gmp-bugs
mailing list