blob: b48d33bbf68c4ccff660201913a3bbbab6c30bce [file] [log] [blame]
djm@openbsd.org166927f2019-11-12 22:32:48 +00001/* $OpenBSD: xmalloc.c,v 1.36 2019/11/12 22:32:48 djm 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
Damien Millercfc18972019-10-09 09:06:35 +110020# 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
Damien Miller5ac712d2016-02-16 10:45:02 +110029#if defined(__OpenBSD__)
Darren Tucker130ef062019-06-08 00:47:07 +100030char *malloc_options = "S";
Damien Miller5ac712d2016-02-16 10:45:02 +110031#endif /* __OpenBSD__ */
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +000032
Damien Miller95def091999-11-25 00:26:21 +110033void *
34xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000036 void *ptr;
37
38 if (size == 0)
39 fatal("xmalloc: zero size");
40 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110041 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110042 fatal("xmalloc: out of memory (allocating %zu bytes)", size);
Damien Miller95def091999-11-25 00:26:21 +110043 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044}
45
Damien Miller95def091999-11-25 00:26:21 +110046void *
Damien Miller07d86be2006-03-26 14:19:21 +110047xcalloc(size_t nmemb, size_t size)
48{
49 void *ptr;
50
Damien Miller07d86be2006-03-26 14:19:21 +110051 if (size == 0 || nmemb == 0)
52 fatal("xcalloc: zero size");
millert@openbsd.orgfd368342015-02-06 23:21:59 +000053 if (SIZE_MAX / nmemb < size)
54 fatal("xcalloc: nmemb * size > SIZE_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110055 ptr = calloc(nmemb, size);
56 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110057 fatal("xcalloc: out of memory (allocating %zu bytes)",
58 size * nmemb);
Damien Miller07d86be2006-03-26 14:19:21 +110059 return ptr;
60}
61
62void *
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000063xreallocarray(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064{
Damien Miller95def091999-11-25 00:26:21 +110065 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000067 new_ptr = reallocarray(ptr, nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110068 if (new_ptr == NULL)
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000069 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
70 nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110071 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072}
73
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +000074void *
75xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
76{
77 void *new_ptr;
78
79 new_ptr = recallocarray(ptr, onmemb, nmemb, size);
80 if (new_ptr == NULL)
81 fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
82 nmemb, size);
83 return new_ptr;
84}
85
Damien Miller95def091999-11-25 00:26:21 +110086char *
87xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088{
Ben Lindstromff6458e2001-08-06 21:03:23 +000089 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000090 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Ben Lindstromff6458e2001-08-06 21:03:23 +000092 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000093 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110094 strlcpy(cp, str, len);
95 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096}
Damien Miller07d86be2006-03-26 14:19:21 +110097
98int
djm@openbsd.org166927f2019-11-12 22:32:48 +000099xvasprintf(char **ret, const char *fmt, va_list ap)
100{
101 int i;
102
103 i = vasprintf(ret, fmt, ap);
104 if (i < 0 || *ret == NULL)
105 fatal("xvasprintf: could not allocate memory");
106 return i;
107}
108
109int
Damien Miller07d86be2006-03-26 14:19:21 +1100110xasprintf(char **ret, const char *fmt, ...)
111{
112 va_list ap;
113 int i;
114
115 va_start(ap, fmt);
djm@openbsd.org166927f2019-11-12 22:32:48 +0000116 i = xvasprintf(ret, fmt, ap);
Damien Miller07d86be2006-03-26 14:19:21 +1100117 va_end(ap);
djm@openbsd.org166927f2019-11-12 22:32:48 +0000118 return i;
Damien Miller07d86be2006-03-26 14:19:21 +1100119}