blob: e7a14866b87c845f74fa4f89d9ed353a0e810649 [file] [log] [blame]
Damien Millere3476ed2006-07-24 14:13:33 +10001/* $OpenBSD: xmalloc.c,v 1.23 2006/07/22 20:48:23 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>
Damien Millere3476ed2006-07-24 14:13:33 +100019#include <string.h>
Darren Tucker5d196262006-07-12 22:15:16 +100020
Ben Lindstrom226cfa02001-01-22 05:34:40 +000021#include "xmalloc.h"
22#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023
Damien Miller95def091999-11-25 00:26:21 +110024void *
25xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000027 void *ptr;
28
29 if (size == 0)
30 fatal("xmalloc: zero size");
31 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110032 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000033 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110034 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035}
36
Damien Miller95def091999-11-25 00:26:21 +110037void *
Damien Miller07d86be2006-03-26 14:19:21 +110038xcalloc(size_t nmemb, size_t size)
39{
40 void *ptr;
41
Damien Miller07d86be2006-03-26 14:19:21 +110042 if (size == 0 || nmemb == 0)
43 fatal("xcalloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110044 if (SIZE_T_MAX / nmemb < size)
45 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110046 ptr = calloc(nmemb, size);
47 if (ptr == NULL)
48 fatal("xcalloc: out of memory (allocating %lu bytes)",
49 (u_long)(size * nmemb));
50 return ptr;
51}
52
53void *
Damien Miller36812092006-03-26 14:22:47 +110054xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055{
Damien Miller95def091999-11-25 00:26:21 +110056 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110057 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058
Ben Lindstroma905ecd2001-02-10 23:34:54 +000059 if (new_size == 0)
60 fatal("xrealloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110061 if (SIZE_T_MAX / nmemb < size)
62 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Damien Miller95def091999-11-25 00:26:21 +110063 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100064 new_ptr = malloc(new_size);
65 else
66 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110067 if (new_ptr == NULL)
Damien Miller36812092006-03-26 14:22:47 +110068 fatal("xrealloc: out of memory (new_size %lu bytes)",
69 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110070 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071}
72
Damien Miller4af51302000-04-16 11:18:38 +100073void
Damien Miller95def091999-11-25 00:26:21 +110074xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075{
Damien Miller95def091999-11-25 00:26:21 +110076 if (ptr == NULL)
77 fatal("xfree: NULL pointer given as argument");
78 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079}
80
Damien Miller95def091999-11-25 00:26:21 +110081char *
82xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083{
Ben Lindstromff6458e2001-08-06 21:03:23 +000084 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000085 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Ben Lindstromff6458e2001-08-06 21:03:23 +000087 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000088 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110089 strlcpy(cp, str, len);
90 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091}
Damien Miller07d86be2006-03-26 14:19:21 +110092
93int
94xasprintf(char **ret, const char *fmt, ...)
95{
96 va_list ap;
97 int i;
98
99 va_start(ap, fmt);
100 i = vasprintf(ret, fmt, ap);
101 va_end(ap);
102
103 if (i < 0 || *ret == NULL)
104 fatal("xasprintf: could not allocate memory");
105
106 return (i);
107}