[patch v1 1/1] assert: ensure posix compliance, add tests for such
Florian Weimer
fweimer@redhat.com
Fri Nov 15 12:57:55 GMT 2024
* DJ Delorie:
> diff --git a/assert/assert.c b/assert/assert.c
> index c29629f5f6..f1711f6995 100644
> --- a/assert/assert.c
> +++ b/assert/assert.c
> @@ -87,8 +87,28 @@ __assert_fail_base (const char *fmt, const char *assertion, const char *file,
> else
> {
> /* At least print a minimal message. */
> - static const char errstr[] = "Unexpected error.\n";
> - __libc_write (STDERR_FILENO, errstr, sizeof (errstr) - 1);
> + char linebuf[100];
> + sprintf(linebuf, "%d", line);
> +#define W(s) __libc_write (STDERR_FILENO, s, sizeof (s) - 1);
> +#define WS(s) __libc_write (STDERR_FILENO, s, strlen (s));
> + if (__progname)
> + {
> + WS(__progname);
> + W(": ");
> + }
> + WS(file);
> + W(":");
> + WS(linebuf);
> + W(": ")
> + if (function)
> + {
> + WS(function);
> + W(": ");
> + }
> + W("Assertion `");
> + WS(assertion);
> + /* Intentionally different for testing purposes. */
> + W("' failed\n");
> }
I think we should do a single write system call (or perhaps use writev),
so that the output is less likely to get interleaved.
But we really should avoid calling malloc (or the full vfprintf) in this
code path. We should also avoid gettext, by looking up the translation
upfront. And we definitely should avoid %n in the format strings.
Thanks,
Florian
More information about the Libc-alpha
mailing list