blob: dea9dd9febf1765d22784522d858900b8c5279e9 [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{
32 extern char *malloc_options;
33
34 malloc_options = "S";
35}
36
Damien Miller95def091999-11-25 00:26:21 +110037void *
38xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000040 void *ptr;
41
42 if (size == 0)
43 fatal("xmalloc: zero size");
44 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110045 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110046 fatal("xmalloc: out of memory (allocating %zu bytes)", size);
Damien Miller95def091999-11-25 00:26:21 +110047 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048}
49
Damien Miller95def091999-11-25 00:26:21 +110050void *
Damien Miller07d86be2006-03-26 14:19:21 +110051xcalloc(size_t nmemb, size_t size)
52{
53 void *ptr;
54
Damien Miller07d86be2006-03-26 14:19:21 +110055 if (size == 0 || nmemb == 0)
56 fatal("xcalloc: zero size");
millert@openbsd.orgfd368342015-02-06 23:21:59 +000057 if (SIZE_MAX / nmemb < size)
58 fatal("xcalloc: nmemb * size > SIZE_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110059 ptr = calloc(nmemb, size);
60 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110061 fatal("xcalloc: out of memory (allocating %zu bytes)",
62 size * nmemb);
Damien Miller07d86be2006-03-26 14:19:21 +110063 return ptr;
64}
65
66void *
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000067xreallocarray(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068{
Damien Miller95def091999-11-25 00:26:21 +110069 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000071 new_ptr = reallocarray(ptr, nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110072 if (new_ptr == NULL)
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000073 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
74 nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110075 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076}
77
Damien Miller95def091999-11-25 00:26:21 +110078char *
79xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080{
Ben Lindstromff6458e2001-08-06 21:03:23 +000081 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000082 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Ben Lindstromff6458e2001-08-06 21:03:23 +000084 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000085 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110086 strlcpy(cp, str, len);
87 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088}
Damien Miller07d86be2006-03-26 14:19:21 +110089
90int
91xasprintf(char **ret, const char *fmt, ...)
92{
93 va_list ap;
94 int i;
95
96 va_start(ap, fmt);
97 i = vasprintf(ret, fmt, ap);
98 va_end(ap);
99
100 if (i < 0 || *ret == NULL)
101 fatal("xasprintf: could not allocate memory");
102
103 return (i);
104}