blob: d5d7b6bc5cd52a73ebcd5f32c7e91aa2089e2eb7 [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
Damien Miller36812092006-03-26 14:22:47 +110038 if (nmemb && size && SIZE_T_MAX / nmemb < size)
Damien Miller07d86be2006-03-26 14:19:21 +110039 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 Miller36812092006-03-26 14:22:47 +110050xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051{
Damien Miller95def091999-11-25 00:26:21 +110052 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110053 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller36812092006-03-26 14:22:47 +110055 if (nmemb && size && SIZE_T_MAX / nmemb < size)
56 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Ben Lindstroma905ecd2001-02-10 23:34:54 +000057 if (new_size == 0)
58 fatal("xrealloc: zero size");
Damien Miller95def091999-11-25 00:26:21 +110059 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100060 new_ptr = malloc(new_size);
61 else
62 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110063 if (new_ptr == NULL)
Damien Miller36812092006-03-26 14:22:47 +110064 fatal("xrealloc: out of memory (new_size %lu bytes)",
65 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110066 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067}
68
Damien Miller4af51302000-04-16 11:18:38 +100069void
Damien Miller95def091999-11-25 00:26:21 +110070xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Damien Miller95def091999-11-25 00:26:21 +110072 if (ptr == NULL)
73 fatal("xfree: NULL pointer given as argument");
74 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075}
76
Damien Miller95def091999-11-25 00:26:21 +110077char *
78xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079{
Ben Lindstromff6458e2001-08-06 21:03:23 +000080 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000081 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Ben Lindstromff6458e2001-08-06 21:03:23 +000083 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000084 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110085 strlcpy(cp, str, len);
86 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087}
Damien Miller07d86be2006-03-26 14:19:21 +110088
89int
90xasprintf(char **ret, const char *fmt, ...)
91{
92 va_list ap;
93 int i;
94
95 va_start(ap, fmt);
96 i = vasprintf(ret, fmt, ap);
97 va_end(ap);
98
99 if (i < 0 || *ret == NULL)
100 fatal("xasprintf: could not allocate memory");
101
102 return (i);
103}