From e79e5c4899e82eff1032b1f8e530234c8fcbd8b9 Mon Sep 17 00:00:00 2001 From: DJ Delorie Date: Thu, 14 Nov 2024 15:12:57 -0500 Subject: assert: ensure posix compliance, add tests for such Fix assert.c so that even the fallback case conforms to POSIX, although not exactly the same as the default case so a test can tell the difference. Add a test that verifies that abort is called, and that the message printed to stderr has all the info that POSIX requires. Verify this even when malloc isn't usable. Reviewed-by: Paul Eggert --- assert/assert.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'assert/assert.c') diff --git a/assert/assert.c b/assert/assert.c index c29629f5f6..a271125f08 100644 --- a/assert/assert.c +++ b/assert/assert.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include extern const char *__progname; @@ -87,8 +89,35 @@ __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[INT_STRLEN_BOUND (int) + sizeof ":: "]; + struct iovec v[9]; + int i = 0; + +#define WS(s) (v[i].iov_len = strlen (v[i].iov_base = (void *) (s)), i++) + + if (__progname) + { + WS (__progname); + WS (": "); + } + + WS (file); + v[i++] = (struct iovec) {.iov_base = linebuf, + .iov_len = sprintf (linebuf, ":%d: ", line)}; + + if (function) + { + WS (function); + WS (": "); + } + + WS ("Assertion `"); + WS (assertion); + /* We omit the '.' here so that the assert tests can tell when + this code path is taken. */ + WS ("' failed\n"); + + (void) writev (STDERR_FILENO, v, i); } abort (); -- cgit v1.2.3