xmalloc.c 2.17 KB
Newer Older
1
/* $OpenBSD: xmalloc.c,v 1.29 2014/01/04 17:50:55 tedu Exp $ */
Damien Miller's avatar
Damien Miller committed
2
/*
3 4 5 6 7
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
 *                    All rights reserved
 * Versions of malloc and friends that check their results, and never return
 * failure (they call fatal if they encounter an error).
8
 *
9 10 11 12 13
 * As far as I am concerned, the code I have written for this software
 * can be used freely for any purpose.  Any derived versions of this
 * software must be clearly marked as such, and if the derived work is
 * incompatible with the protocol description in the RFC file, it must be
 * called by a name other than "ssh" or "Secure Shell".
14
 */
Damien Miller's avatar
Damien Miller committed
15 16 17

#include "includes.h"

18
#include <sys/param.h>
19
#include <stdarg.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23

24 25
#include "xmalloc.h"
#include "log.h"
Damien Miller's avatar
Damien Miller committed
26

27 28
void *
xmalloc(size_t size)
Damien Miller's avatar
Damien Miller committed
29
{
30 31 32 33 34
	void *ptr;

	if (size == 0)
		fatal("xmalloc: zero size");
	ptr = malloc(size);
35
	if (ptr == NULL)
36
		fatal("xmalloc: out of memory (allocating %zu bytes)", size);
37
	return ptr;
Damien Miller's avatar
Damien Miller committed
38 39
}

40 41 42 43 44 45 46
void *
xcalloc(size_t nmemb, size_t size)
{
	void *ptr;

	if (size == 0 || nmemb == 0)
		fatal("xcalloc: zero size");
Damien Miller's avatar
Damien Miller committed
47 48
	if (SIZE_T_MAX / nmemb < size)
		fatal("xcalloc: nmemb * size > SIZE_T_MAX");
49 50
	ptr = calloc(nmemb, size);
	if (ptr == NULL)
51 52
		fatal("xcalloc: out of memory (allocating %zu bytes)",
		    size * nmemb);
53 54 55
	return ptr;
}

56
void *
57
xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Miller's avatar
Damien Miller committed
58
{
59
	void *new_ptr;
60
	size_t new_size = nmemb * size;
61

62 63
	if (new_size == 0)
		fatal("xrealloc: zero size");
Damien Miller's avatar
Damien Miller committed
64 65
	if (SIZE_T_MAX / nmemb < size)
		fatal("xrealloc: nmemb * size > SIZE_T_MAX");
66
	if (ptr == NULL)
67 68 69
		new_ptr = malloc(new_size);
	else
		new_ptr = realloc(ptr, new_size);
70
	if (new_ptr == NULL)
71 72
		fatal("xrealloc: out of memory (new_size %zu bytes)",
		    new_size);
73
	return new_ptr;
Damien Miller's avatar
Damien Miller committed
74 75
}

76 77
char *
xstrdup(const char *str)
Damien Miller's avatar
Damien Miller committed
78
{
79
	size_t len;
80
	char *cp;
Damien Miller's avatar
Damien Miller committed
81

82
	len = strlen(str) + 1;
83
	cp = xmalloc(len);
84 85
	strlcpy(cp, str, len);
	return cp;
Damien Miller's avatar
Damien Miller committed
86
}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

int
xasprintf(char **ret, const char *fmt, ...)
{
	va_list ap;
	int i;

	va_start(ap, fmt);
	i = vasprintf(ret, fmt, ap);
	va_end(ap);

	if (i < 0 || *ret == NULL)
		fatal("xasprintf: could not allocate memory");

	return (i);
}