blob: 6d56781d921925df514608aacad1db30f9824a9b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Versions of malloc and friends that check their results, and never return
6 * failure (they call fatal if they encounter an error).
Kevin Stevesef4eea92001-02-05 12:42:17 +00007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "xmalloc.h"
18#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Damien Miller95def091999-11-25 00:26:21 +110020void *
21xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000023 void *ptr;
24
25 if (size == 0)
26 fatal("xmalloc: zero size");
27 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110028 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000029 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110030 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031}
32
Damien Miller95def091999-11-25 00:26:21 +110033void *
Damien Miller07d86be2006-03-26 14:19:21 +110034xcalloc(size_t nmemb, size_t size)
35{
36 void *ptr;
37
38 if (nmemb && size && SIZE_T_MAX / nmemb < size)
39 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
40 if (size == 0 || nmemb == 0)
41 fatal("xcalloc: zero size");
42 ptr = calloc(nmemb, size);
43 if (ptr == NULL)
44 fatal("xcalloc: out of memory (allocating %lu bytes)",
45 (u_long)(size * nmemb));
46 return ptr;
47}
48
49void *
Damien Miller95def091999-11-25 00:26:21 +110050xrealloc(void *ptr, size_t new_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051{
Damien Miller95def091999-11-25 00:26:21 +110052 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Ben Lindstroma905ecd2001-02-10 23:34:54 +000054 if (new_size == 0)
55 fatal("xrealloc: zero size");
Damien Miller95def091999-11-25 00:26:21 +110056 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100057 new_ptr = malloc(new_size);
58 else
59 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110060 if (new_ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000061 fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110062 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063}
64
Damien Miller4af51302000-04-16 11:18:38 +100065void
Damien Miller95def091999-11-25 00:26:21 +110066xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067{
Damien Miller95def091999-11-25 00:26:21 +110068 if (ptr == NULL)
69 fatal("xfree: NULL pointer given as argument");
70 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071}
72
Damien Miller95def091999-11-25 00:26:21 +110073char *
74xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075{
Ben Lindstromff6458e2001-08-06 21:03:23 +000076 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000077 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Ben Lindstromff6458e2001-08-06 21:03:23 +000079 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000080 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110081 strlcpy(cp, str, len);
82 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083}
Damien Miller07d86be2006-03-26 14:19:21 +110084
85int
86xasprintf(char **ret, const char *fmt, ...)
87{
88 va_list ap;
89 int i;
90
91 va_start(ap, fmt);
92 i = vasprintf(ret, fmt, ap);
93 va_end(ap);
94
95 if (i < 0 || *ret == NULL)
96 fatal("xasprintf: could not allocate memory");
97
98 return (i);
99}