blob: 110d8cb7fb505b5bf6e493a5da1b9dca10d4e875 [file] [log] [blame]
Damien Millerda380be2006-03-31 23:09:17 +11001/* $OpenBSD: xmalloc.c,v 1.21 2006/03/27 01:21:18 deraadt 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
Ben Lindstrom226cfa02001-01-22 05:34:40 +000018#include "xmalloc.h"
19#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Miller95def091999-11-25 00:26:21 +110021void *
22xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000024 void *ptr;
25
26 if (size == 0)
27 fatal("xmalloc: zero size");
28 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110029 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000030 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110031 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032}
33
Damien Miller95def091999-11-25 00:26:21 +110034void *
Damien Miller07d86be2006-03-26 14:19:21 +110035xcalloc(size_t nmemb, size_t size)
36{
37 void *ptr;
38
Damien Miller07d86be2006-03-26 14:19:21 +110039 if (size == 0 || nmemb == 0)
40 fatal("xcalloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110041 if (SIZE_T_MAX / nmemb < size)
42 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110043 ptr = calloc(nmemb, size);
44 if (ptr == NULL)
45 fatal("xcalloc: out of memory (allocating %lu bytes)",
46 (u_long)(size * nmemb));
47 return ptr;
48}
49
50void *
Damien Miller36812092006-03-26 14:22:47 +110051xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052{
Damien Miller95def091999-11-25 00:26:21 +110053 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110054 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Ben Lindstroma905ecd2001-02-10 23:34:54 +000056 if (new_size == 0)
57 fatal("xrealloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110058 if (SIZE_T_MAX / nmemb < size)
59 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Damien Miller95def091999-11-25 00:26:21 +110060 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100061 new_ptr = malloc(new_size);
62 else
63 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110064 if (new_ptr == NULL)
Damien Miller36812092006-03-26 14:22:47 +110065 fatal("xrealloc: out of memory (new_size %lu bytes)",
66 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110067 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068}
69
Damien Miller4af51302000-04-16 11:18:38 +100070void
Damien Miller95def091999-11-25 00:26:21 +110071xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072{
Damien Miller95def091999-11-25 00:26:21 +110073 if (ptr == NULL)
74 fatal("xfree: NULL pointer given as argument");
75 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076}
77
Damien Miller95def091999-11-25 00:26:21 +110078char *
79xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080{
Ben Lindstromff6458e2001-08-06 21:03:23 +000081 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000082 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Ben Lindstromff6458e2001-08-06 21:03:23 +000084 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000085 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110086 strlcpy(cp, str, len);
87 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088}
Damien Miller07d86be2006-03-26 14:19:21 +110089
90int
91xasprintf(char **ret, const char *fmt, ...)
92{
93 va_list ap;
94 int i;
95
96 va_start(ap, fmt);
97 i = vasprintf(ret, fmt, ap);
98 va_end(ap);
99
100 if (i < 0 || *ret == NULL)
101 fatal("xasprintf: could not allocate memory");
102
103 return (i);
104}