blob: 5cc0310a4766ccdca8e0ecba5332fdf6ab051e0f [file] [log] [blame]
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001/* $OpenBSD: xmalloc.c,v 1.34 2017/05/31 09:15: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
Darren Tucker5d196262006-07-12 22:15:16 +100018#include <stdarg.h>
Damien Miller9af21972015-02-24 09:04:32 +110019#ifdef HAVE_STDINT_H
millert@openbsd.orgfd368342015-02-06 23:21:59 +000020#include <stdint.h>
Damien Miller9af21972015-02-24 09:04:32 +110021#endif
Damien Millera7a73ee2006-08-05 11:37:59 +100022#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100023#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100024#include <string.h>
Darren Tucker5d196262006-07-12 22:15:16 +100025
Ben Lindstrom226cfa02001-01-22 05:34:40 +000026#include "xmalloc.h"
27#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +000029void
30ssh_malloc_init(void)
31{
Damien Miller5ac712d2016-02-16 10:45:02 +110032#if defined(__OpenBSD__)
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +000033 extern char *malloc_options;
34
35 malloc_options = "S";
Damien Miller5ac712d2016-02-16 10:45:02 +110036#endif /* __OpenBSD__ */
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +000037}
38
Damien Miller95def091999-11-25 00:26:21 +110039void *
40xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000042 void *ptr;
43
44 if (size == 0)
45 fatal("xmalloc: zero size");
46 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110047 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110048 fatal("xmalloc: out of memory (allocating %zu bytes)", size);
Damien Miller95def091999-11-25 00:26:21 +110049 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050}
51
Damien Miller95def091999-11-25 00:26:21 +110052void *
Damien Miller07d86be2006-03-26 14:19:21 +110053xcalloc(size_t nmemb, size_t size)
54{
55 void *ptr;
56
Damien Miller07d86be2006-03-26 14:19:21 +110057 if (size == 0 || nmemb == 0)
58 fatal("xcalloc: zero size");
millert@openbsd.orgfd368342015-02-06 23:21:59 +000059 if (SIZE_MAX / nmemb < size)
60 fatal("xcalloc: nmemb * size > SIZE_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110061 ptr = calloc(nmemb, size);
62 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110063 fatal("xcalloc: out of memory (allocating %zu bytes)",
64 size * nmemb);
Damien Miller07d86be2006-03-26 14:19:21 +110065 return ptr;
66}
67
68void *
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000069xreallocarray(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070{
Damien Miller95def091999-11-25 00:26:21 +110071 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000073 new_ptr = reallocarray(ptr, nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110074 if (new_ptr == NULL)
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000075 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
76 nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110077 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078}
79
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +000080void *
81xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
82{
83 void *new_ptr;
84
85 new_ptr = recallocarray(ptr, onmemb, nmemb, size);
86 if (new_ptr == NULL)
87 fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
88 nmemb, size);
89 return new_ptr;
90}
91
Damien Miller95def091999-11-25 00:26:21 +110092char *
93xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094{
Ben Lindstromff6458e2001-08-06 21:03:23 +000095 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000096 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Ben Lindstromff6458e2001-08-06 21:03:23 +000098 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000099 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +1100100 strlcpy(cp, str, len);
101 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102}
Damien Miller07d86be2006-03-26 14:19:21 +1100103
104int
105xasprintf(char **ret, const char *fmt, ...)
106{
107 va_list ap;
108 int i;
109
110 va_start(ap, fmt);
111 i = vasprintf(ret, fmt, ap);
112 va_end(ap);
113
114 if (i < 0 || *ret == NULL)
115 fatal("xasprintf: could not allocate memory");
116
117 return (i);
118}