aboutsummaryrefslogtreecommitdiff
path: root/assert/assert.c
diff options
context:
space:
mode:
authorDJ Delorie <dj@redhat.com>2024-11-14 15:12:57 -0500
committerDJ Delorie <dj@redhat.com>2024-12-20 22:44:01 -0500
commite79e5c4899e82eff1032b1f8e530234c8fcbd8b9 (patch)
treeeeda6f5356f5f0c5b261e776f1a05bbbe803b2da /assert/assert.c
parentb3a7a15d99065fe2fc20f40da4ba20eb946b1f52 (diff)
downloadglibc-e79e5c4899e82eff1032b1f8e530234c8fcbd8b9.tar.xz
glibc-e79e5c4899e82eff1032b1f8e530234c8fcbd8b9.zip
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 <eggert@cs.ucla.edu>
Diffstat (limited to 'assert/assert.c')
-rw-r--r--assert/assert.c33
1 files changed, 31 insertions, 2 deletions
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 <unistd.h>
#include <sys/mman.h>
#include <setvmaname.h>
+#include <sys/uio.h>
+#include <intprops.h>
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 ();