xref: /openssh-portable/xmalloc.c (revision 166927fd)
1*166927fdSdjm@openbsd.org /* $OpenBSD: xmalloc.c,v 1.36 2019/11/12 22:32:48 djm Exp $ */
2d4a8b7e3SDamien Miller /*
395def098SDamien Miller  * Author: Tatu Ylonen <ylo@cs.hut.fi>
495def098SDamien Miller  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
595def098SDamien Miller  *                    All rights reserved
695def098SDamien Miller  * Versions of malloc and friends that check their results, and never return
795def098SDamien Miller  * failure (they call fatal if they encounter an error).
8e4340be5SDamien Miller  *
9e4340be5SDamien Miller  * As far as I am concerned, the code I have written for this software
10e4340be5SDamien Miller  * can be used freely for any purpose.  Any derived versions of this
11e4340be5SDamien Miller  * software must be clearly marked as such, and if the derived work is
12e4340be5SDamien Miller  * incompatible with the protocol description in the RFC file, it must be
13e4340be5SDamien Miller  * called by a name other than "ssh" or "Secure Shell".
14d4a8b7e3SDamien Miller  */
15d4a8b7e3SDamien Miller 
16d4a8b7e3SDamien Miller #include "includes.h"
17d4a8b7e3SDamien Miller 
185d19626aSDarren Tucker #include <stdarg.h>
199af21979SDamien Miller #ifdef HAVE_STDINT_H
20fd368348Smillert@openbsd.org # include <stdint.h>
219af21979SDamien Miller #endif
22a7a73ee3SDamien Miller #include <stdio.h>
23e7a1e5cfSDamien Miller #include <stdlib.h>
24e3476ed0SDamien Miller #include <string.h>
255d19626aSDarren Tucker 
26226cfa03SBen Lindstrom #include "xmalloc.h"
27226cfa03SBen Lindstrom #include "log.h"
28d4a8b7e3SDamien Miller 
295ac712d8SDamien Miller #if defined(__OpenBSD__)
30130ef069SDarren Tucker char *malloc_options = "S";
315ac712d8SDamien Miller #endif /* __OpenBSD__ */
32ffb1e7e8Sdtucker@openbsd.org 
3395def098SDamien Miller void *
xmalloc(size_t size)3495def098SDamien Miller xmalloc(size_t size)
35d4a8b7e3SDamien Miller {
36a905ecd9SBen Lindstrom 	void *ptr;
37a905ecd9SBen Lindstrom 
38a905ecd9SBen Lindstrom 	if (size == 0)
39a905ecd9SBen Lindstrom 		fatal("xmalloc: zero size");
40a905ecd9SBen Lindstrom 	ptr = malloc(size);
41d4a8b7e3SDamien Miller 	if (ptr == NULL)
423e498536SDamien Miller 		fatal("xmalloc: out of memory (allocating %zu bytes)", size);
43d4a8b7e3SDamien Miller 	return ptr;
44d4a8b7e3SDamien Miller }
45d4a8b7e3SDamien Miller 
4695def098SDamien Miller void *
xcalloc(size_t nmemb,size_t size)4707d86becSDamien Miller xcalloc(size_t nmemb, size_t size)
4807d86becSDamien Miller {
4907d86becSDamien Miller 	void *ptr;
5007d86becSDamien Miller 
5107d86becSDamien Miller 	if (size == 0 || nmemb == 0)
5207d86becSDamien Miller 		fatal("xcalloc: zero size");
53fd368348Smillert@openbsd.org 	if (SIZE_MAX / nmemb < size)
54fd368348Smillert@openbsd.org 		fatal("xcalloc: nmemb * size > SIZE_MAX");
5507d86becSDamien Miller 	ptr = calloc(nmemb, size);
5607d86becSDamien Miller 	if (ptr == NULL)
573e498536SDamien Miller 		fatal("xcalloc: out of memory (allocating %zu bytes)",
583e498536SDamien Miller 		    size * nmemb);
5907d86becSDamien Miller 	return ptr;
6007d86becSDamien Miller }
6107d86becSDamien Miller 
6207d86becSDamien Miller void *
xreallocarray(void * ptr,size_t nmemb,size_t size)63657a5fbcSderaadt@openbsd.org xreallocarray(void *ptr, size_t nmemb, size_t size)
64d4a8b7e3SDamien Miller {
65d4a8b7e3SDamien Miller 	void *new_ptr;
66d4a8b7e3SDamien Miller 
67657a5fbcSderaadt@openbsd.org 	new_ptr = reallocarray(ptr, nmemb, size);
68d4a8b7e3SDamien Miller 	if (new_ptr == NULL)
69657a5fbcSderaadt@openbsd.org 		fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
70657a5fbcSderaadt@openbsd.org 		    nmemb, size);
71d4a8b7e3SDamien Miller 	return new_ptr;
72d4a8b7e3SDamien Miller }
73d4a8b7e3SDamien Miller 
749e509d4eSderaadt@openbsd.org void *
xrecallocarray(void * ptr,size_t onmemb,size_t nmemb,size_t size)759e509d4eSderaadt@openbsd.org xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
769e509d4eSderaadt@openbsd.org {
779e509d4eSderaadt@openbsd.org 	void *new_ptr;
789e509d4eSderaadt@openbsd.org 
799e509d4eSderaadt@openbsd.org 	new_ptr = recallocarray(ptr, onmemb, nmemb, size);
809e509d4eSderaadt@openbsd.org 	if (new_ptr == NULL)
819e509d4eSderaadt@openbsd.org 		fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
829e509d4eSderaadt@openbsd.org 		    nmemb, size);
839e509d4eSderaadt@openbsd.org 	return new_ptr;
849e509d4eSderaadt@openbsd.org }
859e509d4eSderaadt@openbsd.org 
8695def098SDamien Miller char *
xstrdup(const char * str)8795def098SDamien Miller xstrdup(const char *str)
88d4a8b7e3SDamien Miller {
89ff6458e0SBen Lindstrom 	size_t len;
90a905ecd9SBen Lindstrom 	char *cp;
91d4a8b7e3SDamien Miller 
92ff6458e0SBen Lindstrom 	len = strlen(str) + 1;
93a905ecd9SBen Lindstrom 	cp = xmalloc(len);
94d4a8b7e3SDamien Miller 	strlcpy(cp, str, len);
95d4a8b7e3SDamien Miller 	return cp;
96d4a8b7e3SDamien Miller }
9707d86becSDamien Miller 
9807d86becSDamien Miller int
xvasprintf(char ** ret,const char * fmt,va_list ap)99*166927fdSdjm@openbsd.org xvasprintf(char **ret, const char *fmt, va_list ap)
100*166927fdSdjm@openbsd.org {
101*166927fdSdjm@openbsd.org 	int i;
102*166927fdSdjm@openbsd.org 
103*166927fdSdjm@openbsd.org 	i = vasprintf(ret, fmt, ap);
104*166927fdSdjm@openbsd.org 	if (i < 0 || *ret == NULL)
105*166927fdSdjm@openbsd.org 		fatal("xvasprintf: could not allocate memory");
106*166927fdSdjm@openbsd.org 	return i;
107*166927fdSdjm@openbsd.org }
108*166927fdSdjm@openbsd.org 
109*166927fdSdjm@openbsd.org int
xasprintf(char ** ret,const char * fmt,...)11007d86becSDamien Miller xasprintf(char **ret, const char *fmt, ...)
11107d86becSDamien Miller {
11207d86becSDamien Miller 	va_list ap;
11307d86becSDamien Miller 	int i;
11407d86becSDamien Miller 
11507d86becSDamien Miller 	va_start(ap, fmt);
116*166927fdSdjm@openbsd.org 	i = xvasprintf(ret, fmt, ap);
11707d86becSDamien Miller 	va_end(ap);
118*166927fdSdjm@openbsd.org 	return i;
11907d86becSDamien Miller }
120