1296633Sdes/* $OpenBSD: xmalloc.c,v 1.33 2016/02/15 09:47:49 dtucker Exp $ */
257429Smarkm/*
357429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi>
457429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
557429Smarkm *                    All rights reserved
657429Smarkm * Versions of malloc and friends that check their results, and never return
757429Smarkm * failure (they call fatal if they encounter an error).
876259Sgreen *
965668Skris * As far as I am concerned, the code I have written for this software
1065668Skris * can be used freely for any purpose.  Any derived versions of this
1165668Skris * software must be clearly marked as such, and if the derived work is
1265668Skris * incompatible with the protocol description in the RFC file, it must be
1365668Skris * called by a name other than "ssh" or "Secure Shell".
1457429Smarkm */
1557429Smarkm
1657429Smarkm#include "includes.h"
1757429Smarkm
18162852Sdes#include <stdarg.h>
19294332Sdes#ifdef HAVE_STDINT_H
20294332Sdes#include <stdint.h>
21294332Sdes#endif
22162852Sdes#include <stdio.h>
23162852Sdes#include <stdlib.h>
24162852Sdes#include <string.h>
25162852Sdes
2676259Sgreen#include "xmalloc.h"
2776259Sgreen#include "log.h"
2857429Smarkm
29296633Sdesvoid
30296633Sdesssh_malloc_init(void)
31296633Sdes{
32296633Sdes#if defined(__OpenBSD__)
33296633Sdes	extern char *malloc_options;
34296633Sdes
35296633Sdes	malloc_options = "S";
36296633Sdes#endif /* __OpenBSD__ */
37296633Sdes}
38296633Sdes
3957429Smarkmvoid *
4057429Smarkmxmalloc(size_t size)
4157429Smarkm{
4276259Sgreen	void *ptr;
4376259Sgreen
4476259Sgreen	if (size == 0)
4576259Sgreen		fatal("xmalloc: zero size");
4676259Sgreen	ptr = malloc(size);
4757429Smarkm	if (ptr == NULL)
48261320Sdes		fatal("xmalloc: out of memory (allocating %zu bytes)", size);
4957429Smarkm	return ptr;
5057429Smarkm}
5157429Smarkm
5257429Smarkmvoid *
53162852Sdesxcalloc(size_t nmemb, size_t size)
5457429Smarkm{
55162852Sdes	void *ptr;
56162852Sdes
57162852Sdes	if (size == 0 || nmemb == 0)
58162852Sdes		fatal("xcalloc: zero size");
59294332Sdes	if (SIZE_MAX / nmemb < size)
60294332Sdes		fatal("xcalloc: nmemb * size > SIZE_MAX");
61162852Sdes	ptr = calloc(nmemb, size);
62162852Sdes	if (ptr == NULL)
63261320Sdes		fatal("xcalloc: out of memory (allocating %zu bytes)",
64261320Sdes		    size * nmemb);
65162852Sdes	return ptr;
66162852Sdes}
67162852Sdes
68162852Sdesvoid *
69294336Sdesxreallocarray(void *ptr, size_t nmemb, size_t size)
70162852Sdes{
7157429Smarkm	void *new_ptr;
7257429Smarkm
73294336Sdes	new_ptr = reallocarray(ptr, nmemb, size);
7457429Smarkm	if (new_ptr == NULL)
75294336Sdes		fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
76294336Sdes		    nmemb, size);
7757429Smarkm	return new_ptr;
7857429Smarkm}
7957429Smarkm
8057429Smarkmchar *
8157429Smarkmxstrdup(const char *str)
8257429Smarkm{
8392555Sdes	size_t len;
8476259Sgreen	char *cp;
8557429Smarkm
8692555Sdes	len = strlen(str) + 1;
8776259Sgreen	cp = xmalloc(len);
8857429Smarkm	strlcpy(cp, str, len);
8957429Smarkm	return cp;
9057429Smarkm}
91162852Sdes
92162852Sdesint
93162852Sdesxasprintf(char **ret, const char *fmt, ...)
94162852Sdes{
95162852Sdes	va_list ap;
96162852Sdes	int i;
97162852Sdes
98162852Sdes	va_start(ap, fmt);
99162852Sdes	i = vasprintf(ret, fmt, ap);
100162852Sdes	va_end(ap);
101162852Sdes
102162852Sdes	if (i < 0 || *ret == NULL)
103162852Sdes		fatal("xasprintf: could not allocate memory");
104162852Sdes
105162852Sdes	return (i);
106162852Sdes}
107