blob: 2f1cd2306945e90f2e2b450d45e8e2d077a4ed4e [file] [log] [blame]
Damien Miller3e498532014-01-10 10:37:05 +11001/* $OpenBSD: xmalloc.c,v 1.29 2014/01/04 17:50:55 tedu Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Versions of malloc and friends that check their results, and never return
7 * failure (they call fatal if they encounter an error).
Kevin Stevesef4eea92001-02-05 12:42:17 +00008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
Damien Miller8dbffe72006-08-05 11:02:17 +100018#include <sys/param.h>
Darren Tucker5d196262006-07-12 22:15:16 +100019#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100020#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100021#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100022#include <string.h>
Darren Tucker5d196262006-07-12 22:15:16 +100023
Ben Lindstrom226cfa02001-01-22 05:34:40 +000024#include "xmalloc.h"
25#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026
Damien Miller95def091999-11-25 00:26:21 +110027void *
28xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000030 void *ptr;
31
32 if (size == 0)
33 fatal("xmalloc: zero size");
34 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110035 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110036 fatal("xmalloc: out of memory (allocating %zu bytes)", size);
Damien Miller95def091999-11-25 00:26:21 +110037 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038}
39
Damien Miller95def091999-11-25 00:26:21 +110040void *
Damien Miller07d86be2006-03-26 14:19:21 +110041xcalloc(size_t nmemb, size_t size)
42{
43 void *ptr;
44
Damien Miller07d86be2006-03-26 14:19:21 +110045 if (size == 0 || nmemb == 0)
46 fatal("xcalloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110047 if (SIZE_T_MAX / nmemb < size)
48 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110049 ptr = calloc(nmemb, size);
50 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110051 fatal("xcalloc: out of memory (allocating %zu bytes)",
52 size * nmemb);
Damien Miller07d86be2006-03-26 14:19:21 +110053 return ptr;
54}
55
56void *
Damien Miller36812092006-03-26 14:22:47 +110057xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058{
Damien Miller95def091999-11-25 00:26:21 +110059 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110060 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Ben Lindstroma905ecd2001-02-10 23:34:54 +000062 if (new_size == 0)
63 fatal("xrealloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110064 if (SIZE_T_MAX / nmemb < size)
65 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Damien Miller95def091999-11-25 00:26:21 +110066 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100067 new_ptr = malloc(new_size);
68 else
69 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110070 if (new_ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110071 fatal("xrealloc: out of memory (new_size %zu bytes)",
72 new_size);
Damien Miller95def091999-11-25 00:26:21 +110073 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074}
75
Damien Miller95def091999-11-25 00:26:21 +110076char *
77xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078{
Ben Lindstromff6458e2001-08-06 21:03:23 +000079 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000080 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Ben Lindstromff6458e2001-08-06 21:03:23 +000082 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000083 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110084 strlcpy(cp, str, len);
85 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086}
Damien Miller07d86be2006-03-26 14:19:21 +110087
88int
89xasprintf(char **ret, const char *fmt, ...)
90{
91 va_list ap;
92 int i;
93
94 va_start(ap, fmt);
95 i = vasprintf(ret, fmt, ap);
96 va_end(ap);
97
98 if (i < 0 || *ret == NULL)
99 fatal("xasprintf: could not allocate memory");
100
101 return (i);
102}