summaryrefslogtreecommitdiffstats
path: root/lib/alloc.c
blob: 962f45a1d90eb165c9710caef1d72f1b00e74dc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * SPDX-FileCopyrightText: 1990 - 1994, Julianne Frances Haugh
 * 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
 */

/* Replacements for malloc and strdup with error checking.  Too trivial
   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 */

#include <config.h>

#ident "$Id$"

#include "alloc.h"

#include <errno.h>
#include <stddef.h>
#include <stdio.h>

#include "defines.h"
#include "prototypes.h"
#include "shadowlog.h"


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  *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);
}


void *
xreallocarray(void *p, size_t nmemb, size_t size)
{
	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);
}