diff --git a/include/stdlib.h b/include/stdlib.h index 39b36bcb77be..96724bc2f282 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -115,6 +115,9 @@ int mbtowc __P((wchar_t *, const char *, size_t)); size_t wcstombs __P((char *, const wchar_t *, size_t)); #if !defined(_ANSI_SOURCE) && !defined(_POSIX_SOURCE) +extern char *_malloc_options; +extern void (*_malloc_message)__P((char *p1, char *p2, char *p3, char *p4)); + int putenv __P((const char *)); int setenv __P((const char *, const char *, int)); diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3 index 03feaa37a01b..e7d53f955d08 100644 --- a/lib/libc/stdlib/malloc.3 +++ b/lib/libc/stdlib/malloc.3 @@ -57,7 +57,9 @@ .Ft void .Fn free "void *ptr" .Ft char * -.Va malloc_options; +.Va _malloc_options +.Ft void +.Va (*_malloc_message)(char *p1, char *p2, char *p3, char *p4) .Sh DESCRIPTION The .Fn malloc @@ -146,7 +148,7 @@ The ``name'' of the file referenced by the symbolic link named the value of the environment variable .Ev MALLOC_OPTIONS , and the string pointed to by the global variable -.Va malloc_options +.Va _malloc_options will be interpreted, in that order, character by character as flags. .Pp Most flags are single letters, @@ -211,8 +213,7 @@ core (using This option should be set at compile time by including the following in the source code: .Bd -literal -offset indent -extern char *malloc_options; -malloc_options = "X"; +_malloc_options = "X"; .Ed .It Z This option implicitly sets the @@ -249,8 +250,7 @@ ln -s 'A<' /etc/malloc.conf To specify in the source that a program does no return value checking on calls to these functions: .Bd -literal -offset indent -extern char *malloc_options; -malloc_options = "X"; +_malloc_options = "X"; .Ed .Sh ENVIRONMENT The following environment variables affect the execution of the allocation @@ -364,6 +364,14 @@ If the .Dq A option is set, all warnings are treated as errors. .Pp +The +.Va _malloc_message +variable allows the programmer to override the function which emits +the text strings forming the errors and warnings if for some reason +the stderr filedescriptor is not suitable for this. +Please note that doing anything which tries to allocate memory in +this function will certain suicide for the process. +.Pp The following is a brief description of possible error messages and their meanings: .Pp diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 3b631e86de79..bb6bb6fb8744 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -251,7 +251,7 @@ static void *malloc_brk; static struct pgfree *px; /* compile-time options */ -char *malloc_options; +char *_malloc_options; /* Name of the current public function */ static char *malloc_func; @@ -269,30 +269,36 @@ static void *imalloc(size_t size); static void ifree(void *ptr); static void *irealloc(void *ptr, size_t size); +static void +wrtmessage(char *p1, char *p2, char *p3, char *p4) +{ + + _write(STDERR_FILENO, p1, strlen(p1)); + _write(STDERR_FILENO, p2, strlen(p2)); + _write(STDERR_FILENO, p3, strlen(p3)); + _write(STDERR_FILENO, p4, strlen(p4)); +} + +void (*_malloc_message)(char *p1, char *p2, char *p3, char *p4) = wrtmessage; + extern char *__progname; static void wrterror(char *p) { - char *q = " error: "; - _write(STDERR_FILENO, __progname, strlen(__progname)); - _write(STDERR_FILENO, malloc_func, strlen(malloc_func)); - _write(STDERR_FILENO, q, strlen(q)); - _write(STDERR_FILENO, p, strlen(p)); + suicide = 1; + _malloc_message(__progname, malloc_func, " error: ", p); abort(); } static void wrtwarning(char *p) { - char *q = " warning: "; + if (malloc_abort) wrterror(p); - _write(STDERR_FILENO, __progname, strlen(__progname)); - _write(STDERR_FILENO, malloc_func, strlen(malloc_func)); - _write(STDERR_FILENO, q, strlen(q)); - _write(STDERR_FILENO, p, strlen(p)); + _malloc_message(__progname, malloc_func, " warning: ", p); } /* @@ -403,7 +409,7 @@ malloc_init () } else if (i == 1) { p = getenv("MALLOC_OPTIONS"); } else { - p = malloc_options; + p = _malloc_options; } for (; p && *p; p++) { switch (*p) {