blob: b58323677ee7a82232638fa6b629160a6c06f1f1 [file] [log] [blame]
dtucker@openbsd.orgffb1e7e2016-02-15 09:47:49 +00001/* $OpenBSD: xmalloc.c,v 1.33 2016/02/15 09:47:49 dtucker 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
Damien Miller95def091999-11-25 00:26:21 +110080char *
81xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082{
Ben Lindstromff6458e2001-08-06 21:03:23 +000083 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000084 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Ben Lindstromff6458e2001-08-06 21:03:23 +000086 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000087 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110088 strlcpy(cp, str, len);
89 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090}
Damien Miller07d86be2006-03-26 14:19:21 +110091
92int
93xasprintf(char **ret, const char *fmt, ...)
94{
95 va_list ap;
96 int i;
97
98 va_start(ap, fmt);
99 i = vasprintf(ret, fmt, ap);
100 va_end(ap);
101
102 if (i < 0 || *ret == NULL)
103 fatal("xasprintf: could not allocate memory");
104
105 return (i);
106}