blob: 9985b4cc2a3494cc89c1c6e3242b95de88712246 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: xmalloc.c,v 1.27 2006/08/03 03:34:42 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
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)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000036 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) 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)
51 fatal("xcalloc: out of memory (allocating %lu bytes)",
52 (u_long)(size * nmemb));
53 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 Miller36812092006-03-26 14:22:47 +110071 fatal("xrealloc: out of memory (new_size %lu bytes)",
72 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110073 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074}
75
Damien Miller4af51302000-04-16 11:18:38 +100076void
Damien Miller95def091999-11-25 00:26:21 +110077xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078{
Damien Miller95def091999-11-25 00:26:21 +110079 if (ptr == NULL)
80 fatal("xfree: NULL pointer given as argument");
81 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082}
83
Damien Miller95def091999-11-25 00:26:21 +110084char *
85xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086{
Ben Lindstromff6458e2001-08-06 21:03:23 +000087 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000088 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Ben Lindstromff6458e2001-08-06 21:03:23 +000090 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000091 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110092 strlcpy(cp, str, len);
93 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094}
Damien Miller07d86be2006-03-26 14:19:21 +110095
96int
97xasprintf(char **ret, const char *fmt, ...)
98{
99 va_list ap;
100 int i;
101
102 va_start(ap, fmt);
103 i = vasprintf(ret, fmt, ap);
104 va_end(ap);
105
106 if (i < 0 || *ret == NULL)
107 fatal("xasprintf: could not allocate memory");
108
109 return (i);
110}