summaryrefslogtreecommitdiffstats
path: root/lib/alloc.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/alloc.c (renamed from libmisc/xmalloc.c)57
1 files changed, 42 insertions, 15 deletions
diff --git a/libmisc/xmalloc.c b/lib/alloc.c
index 056d472..962f45a 100644
--- a/libmisc/xmalloc.c
+++ b/lib/alloc.c
@@ -3,6 +3,7 @@
* SPDX-FileCopyrightText: 1996 - 1998, Marek Michałkiewicz
* SPDX-FileCopyrightText: 2003 - 2006, Tomasz Kłoczko
* SPDX-FileCopyrightText: 2008 , Nicolas François
+ * SPDX-FileCopyrightText: 2023 , Alejandro Colomar <alx@kernel.org>
*
* SPDX-License-Identifier: BSD-3-Clause
*/
@@ -11,7 +12,7 @@
to be worth copyrighting :-). I did that because a lot of code used
malloc and strdup without checking for NULL pointer, and I like some
message better than a core dump... --marekm
-
+
Yeh, but. Remember that bailing out might leave the system in some
bizarre state. You really want to put in error checking, then add
some back-out failure recovery code. -- jfh */
@@ -20,27 +21,53 @@
#ident "$Id$"
-#include <stdio.h>
+#include "alloc.h"
+
#include <errno.h>
+#include <stddef.h>
+#include <stdio.h>
+
#include "defines.h"
#include "prototypes.h"
#include "shadowlog.h"
-/*@maynotreturn@*/ /*@only@*//*@out@*//*@notnull@*/void *xmalloc (size_t size)
+
+extern inline void *xmalloc(size_t size);
+extern inline void *xmallocarray(size_t nmemb, size_t size);
+extern inline void *mallocarray(size_t nmemb, size_t size);
+extern inline void *reallocarrayf(void *p, size_t nmemb, size_t size);
+extern inline char *xstrdup(const char *str);
+
+
+void *
+xcalloc(size_t nmemb, size_t size)
{
- void *ptr;
-
- ptr = malloc (size);
- if (NULL == ptr) {
- (void) fprintf (log_get_logfd(),
- _("%s: failed to allocate memory: %s\n"),
- log_get_progname(), strerror (errno));
- exit (13);
- }
- return ptr;
+ void *p;
+
+ p = calloc(nmemb, size);
+ if (p == NULL)
+ goto x;
+
+ return p;
+
+x:
+ fprintf(log_get_logfd(), _("%s: %s\n"),
+ log_get_progname(), strerror(errno));
+ exit(13);
}
-/*@maynotreturn@*/ /*@only@*//*@notnull@*/char *xstrdup (const char *str)
+
+void *
+xreallocarray(void *p, size_t nmemb, size_t size)
{
- return strcpy (xmalloc (strlen (str) + 1), str);
+ p = reallocarrayf(p, nmemb, size);
+ if (p == NULL)
+ goto x;
+
+ return p;
+
+x:
+ fprintf(log_get_logfd(), _("%s: %s\n"),
+ log_get_progname(), strerror(errno));
+ exit(13);
}