xref: /dflybsd-src/usr.bin/diff/xmalloc.c (revision c9733229451fac5faa53b1a016b01866eae75a1c)
1*c9733229SMatthew Dillon /* $OpenBSD: xmalloc.c,v 1.10 2019/06/28 05:44:09 deraadt Exp $ */
2*c9733229SMatthew Dillon /*
3*c9733229SMatthew Dillon  * Author: Tatu Ylonen <ylo@cs.hut.fi>
4*c9733229SMatthew Dillon  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5*c9733229SMatthew Dillon  *                    All rights reserved
6*c9733229SMatthew Dillon  * Versions of malloc and friends that check their results, and never return
7*c9733229SMatthew Dillon  * failure (they call fatal if they encounter an error).
8*c9733229SMatthew Dillon  *
9*c9733229SMatthew Dillon  * As far as I am concerned, the code I have written for this software
10*c9733229SMatthew Dillon  * can be used freely for any purpose.  Any derived versions of this
11*c9733229SMatthew Dillon  * software must be clearly marked as such, and if the derived work is
12*c9733229SMatthew Dillon  * incompatible with the protocol description in the RFC file, it must be
13*c9733229SMatthew Dillon  * called by a name other than "ssh" or "Secure Shell".
14*c9733229SMatthew Dillon  */
15*c9733229SMatthew Dillon 
16*c9733229SMatthew Dillon #include <err.h>
17*c9733229SMatthew Dillon #include <stdarg.h>
18*c9733229SMatthew Dillon #include <stdint.h>
19*c9733229SMatthew Dillon #include <stdio.h>
20*c9733229SMatthew Dillon #include <stdlib.h>
21*c9733229SMatthew Dillon #include <string.h>
22*c9733229SMatthew Dillon 
23*c9733229SMatthew Dillon #include "xmalloc.h"
24*c9733229SMatthew Dillon 
25*c9733229SMatthew Dillon void *
xmalloc(size_t size)26*c9733229SMatthew Dillon xmalloc(size_t size)
27*c9733229SMatthew Dillon {
28*c9733229SMatthew Dillon 	void *ptr;
29*c9733229SMatthew Dillon 
30*c9733229SMatthew Dillon 	if (size == 0)
31*c9733229SMatthew Dillon 		errx(2, "xmalloc: zero size");
32*c9733229SMatthew Dillon 	ptr = malloc(size);
33*c9733229SMatthew Dillon 	if (ptr == NULL)
34*c9733229SMatthew Dillon 		err(2, "xmalloc: allocating %zu bytes", size);
35*c9733229SMatthew Dillon 	return ptr;
36*c9733229SMatthew Dillon }
37*c9733229SMatthew Dillon 
38*c9733229SMatthew Dillon void *
xcalloc(size_t nmemb,size_t size)39*c9733229SMatthew Dillon xcalloc(size_t nmemb, size_t size)
40*c9733229SMatthew Dillon {
41*c9733229SMatthew Dillon 	void *ptr;
42*c9733229SMatthew Dillon 
43*c9733229SMatthew Dillon 	ptr = calloc(nmemb, size);
44*c9733229SMatthew Dillon 	if (ptr == NULL)
45*c9733229SMatthew Dillon 		err(2, "xcalloc: allocating %zu * %zu bytes", nmemb, size);
46*c9733229SMatthew Dillon 	return ptr;
47*c9733229SMatthew Dillon }
48*c9733229SMatthew Dillon 
49*c9733229SMatthew Dillon void *
xreallocarray(void * ptr,size_t nmemb,size_t size)50*c9733229SMatthew Dillon xreallocarray(void *ptr, size_t nmemb, size_t size)
51*c9733229SMatthew Dillon {
52*c9733229SMatthew Dillon 	void *new_ptr;
53*c9733229SMatthew Dillon 
54*c9733229SMatthew Dillon 	new_ptr = reallocarray(ptr, nmemb, size);
55*c9733229SMatthew Dillon 	if (new_ptr == NULL)
56*c9733229SMatthew Dillon 		err(2, "xreallocarray: allocating %zu * %zu bytes",
57*c9733229SMatthew Dillon 		    nmemb, size);
58*c9733229SMatthew Dillon 	return new_ptr;
59*c9733229SMatthew Dillon }
60*c9733229SMatthew Dillon 
61*c9733229SMatthew Dillon char *
xstrdup(const char * str)62*c9733229SMatthew Dillon xstrdup(const char *str)
63*c9733229SMatthew Dillon {
64*c9733229SMatthew Dillon 	char *cp;
65*c9733229SMatthew Dillon 
66*c9733229SMatthew Dillon 	if ((cp = strdup(str)) == NULL)
67*c9733229SMatthew Dillon 		err(2, "xstrdup");
68*c9733229SMatthew Dillon 	return cp;
69*c9733229SMatthew Dillon }
70*c9733229SMatthew Dillon 
71*c9733229SMatthew Dillon int
xasprintf(char ** ret,const char * fmt,...)72*c9733229SMatthew Dillon xasprintf(char **ret, const char *fmt, ...)
73*c9733229SMatthew Dillon {
74*c9733229SMatthew Dillon 	va_list ap;
75*c9733229SMatthew Dillon 	int i;
76*c9733229SMatthew Dillon 
77*c9733229SMatthew Dillon 	va_start(ap, fmt);
78*c9733229SMatthew Dillon 	i = vasprintf(ret, fmt, ap);
79*c9733229SMatthew Dillon 	va_end(ap);
80*c9733229SMatthew Dillon 
81*c9733229SMatthew Dillon 	if (i == -1)
82*c9733229SMatthew Dillon 		err(2, "xasprintf");
83*c9733229SMatthew Dillon 
84*c9733229SMatthew Dillon 	return i;
85*c9733229SMatthew Dillon }
86