blob: 511a9e12a21f300472c07fb0c309dc1453f9d503 [file] [log] [blame]
Damien Miller8dbffe72006-08-05 11:02:17 +10001/* $OpenBSD: xmalloc.c,v 1.24 2006/07/26 02:35:17 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
Damien Miller8dbffe72006-08-05 11:02:17 +100018#include <sys/param.h>
19
Darren Tucker5d196262006-07-12 22:15:16 +100020#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100021#include <string.h>
Darren Tucker5d196262006-07-12 22:15:16 +100022
Ben Lindstrom226cfa02001-01-22 05:34:40 +000023#include "xmalloc.h"
24#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100025
Damien Miller95def091999-11-25 00:26:21 +110026void *
27xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000029 void *ptr;
30
31 if (size == 0)
32 fatal("xmalloc: zero size");
33 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110034 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000035 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110036 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037}
38
Damien Miller95def091999-11-25 00:26:21 +110039void *
Damien Miller07d86be2006-03-26 14:19:21 +110040xcalloc(size_t nmemb, size_t size)
41{
42 void *ptr;
43
Damien Miller07d86be2006-03-26 14:19:21 +110044 if (size == 0 || nmemb == 0)
45 fatal("xcalloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110046 if (SIZE_T_MAX / nmemb < size)
47 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110048 ptr = calloc(nmemb, size);
49 if (ptr == NULL)
50 fatal("xcalloc: out of memory (allocating %lu bytes)",
51 (u_long)(size * nmemb));
52 return ptr;
53}
54
55void *
Damien Miller36812092006-03-26 14:22:47 +110056xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057{
Damien Miller95def091999-11-25 00:26:21 +110058 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110059 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Ben Lindstroma905ecd2001-02-10 23:34:54 +000061 if (new_size == 0)
62 fatal("xrealloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110063 if (SIZE_T_MAX / nmemb < size)
64 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Damien Miller95def091999-11-25 00:26:21 +110065 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100066 new_ptr = malloc(new_size);
67 else
68 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110069 if (new_ptr == NULL)
Damien Miller36812092006-03-26 14:22:47 +110070 fatal("xrealloc: out of memory (new_size %lu bytes)",
71 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110072 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073}
74
Damien Miller4af51302000-04-16 11:18:38 +100075void
Damien Miller95def091999-11-25 00:26:21 +110076xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077{
Damien Miller95def091999-11-25 00:26:21 +110078 if (ptr == NULL)
79 fatal("xfree: NULL pointer given as argument");
80 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081}
82
Damien Miller95def091999-11-25 00:26:21 +110083char *
84xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085{
Ben Lindstromff6458e2001-08-06 21:03:23 +000086 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000087 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Ben Lindstromff6458e2001-08-06 21:03:23 +000089 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000090 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110091 strlcpy(cp, str, len);
92 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093}
Damien Miller07d86be2006-03-26 14:19:21 +110094
95int
96xasprintf(char **ret, const char *fmt, ...)
97{
98 va_list ap;
99 int i;
100
101 va_start(ap, fmt);
102 i = vasprintf(ret, fmt, ap);
103 va_end(ap);
104
105 if (i < 0 || *ret == NULL)
106 fatal("xasprintf: could not allocate memory");
107
108 return (i);
109}