1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-08-07 06:43:00 +03:00

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>
This commit is contained in:
DJ Delorie
2024-11-14 15:12:57 -05:00
parent b3a7a15d99
commit e79e5c4899
3 changed files with 198 additions and 2 deletions

View File

@@ -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 ();