blob: 8f9c3e12e28f4e7aee2e0dcbc318c463e8f7497c [file] [log] [blame]
Darren Tucker5d196262006-07-12 22:15:16 +10001/* $OpenBSD: xmalloc.c,v 1.22 2006/07/10 16:37:36 stevesk 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
Darren Tucker5d196262006-07-12 22:15:16 +100018#include <stdarg.h>
19
Ben Lindstrom226cfa02001-01-22 05:34:40 +000020#include "xmalloc.h"
21#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
Damien Miller95def091999-11-25 00:26:21 +110023void *
24xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000026 void *ptr;
27
28 if (size == 0)
29 fatal("xmalloc: zero size");
30 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110031 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000032 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110033 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034}
35
Damien Miller95def091999-11-25 00:26:21 +110036void *
Damien Miller07d86be2006-03-26 14:19:21 +110037xcalloc(size_t nmemb, size_t size)
38{
39 void *ptr;
40
Damien Miller07d86be2006-03-26 14:19:21 +110041 if (size == 0 || nmemb == 0)
42 fatal("xcalloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110043 if (SIZE_T_MAX / nmemb < size)
44 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110045 ptr = calloc(nmemb, size);
46 if (ptr == NULL)
47 fatal("xcalloc: out of memory (allocating %lu bytes)",
48 (u_long)(size * nmemb));
49 return ptr;
50}
51
52void *
Damien Miller36812092006-03-26 14:22:47 +110053xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054{
Damien Miller95def091999-11-25 00:26:21 +110055 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110056 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057
Ben Lindstroma905ecd2001-02-10 23:34:54 +000058 if (new_size == 0)
59 fatal("xrealloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110060 if (SIZE_T_MAX / nmemb < size)
61 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Damien Miller95def091999-11-25 00:26:21 +110062 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100063 new_ptr = malloc(new_size);
64 else
65 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110066 if (new_ptr == NULL)
Damien Miller36812092006-03-26 14:22:47 +110067 fatal("xrealloc: out of memory (new_size %lu bytes)",
68 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110069 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070}
71
Damien Miller4af51302000-04-16 11:18:38 +100072void
Damien Miller95def091999-11-25 00:26:21 +110073xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074{
Damien Miller95def091999-11-25 00:26:21 +110075 if (ptr == NULL)
76 fatal("xfree: NULL pointer given as argument");
77 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
Damien Miller95def091999-11-25 00:26:21 +110080char *
81xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082{
Ben Lindstromff6458e2001-08-06 21:03:23 +000083 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000084 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstromff6458e2001-08-06 21:03:23 +000086 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000087 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110088 strlcpy(cp, str, len);
89 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090}
Damien Miller07d86be2006-03-26 14:19:21 +110091
92int
93xasprintf(char **ret, const char *fmt, ...)
94{
95 va_list ap;
96 int i;
97
98 va_start(ap, fmt);
99 i = vasprintf(ret, fmt, ap);
100 va_end(ap);
101
102 if (i < 0 || *ret == NULL)
103 fatal("xasprintf: could not allocate memory");
104
105 return (i);
106}