blob: 98cbf8776bd617b751519cc95980f816533b61f0 [file] [log] [blame]
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +00001/* $OpenBSD: xmalloc.c,v 1.32 2015/04/24 01:36:01 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
Damien Miller95def091999-11-25 00:26:21 +110029void *
30xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000032 void *ptr;
33
34 if (size == 0)
35 fatal("xmalloc: zero size");
36 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110037 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110038 fatal("xmalloc: out of memory (allocating %zu bytes)", size);
Damien Miller95def091999-11-25 00:26:21 +110039 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040}
41
Damien Miller95def091999-11-25 00:26:21 +110042void *
Damien Miller07d86be2006-03-26 14:19:21 +110043xcalloc(size_t nmemb, size_t size)
44{
45 void *ptr;
46
Damien Miller07d86be2006-03-26 14:19:21 +110047 if (size == 0 || nmemb == 0)
48 fatal("xcalloc: zero size");
millert@openbsd.orgfd368342015-02-06 23:21:59 +000049 if (SIZE_MAX / nmemb < size)
50 fatal("xcalloc: nmemb * size > SIZE_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110051 ptr = calloc(nmemb, size);
52 if (ptr == NULL)
Damien Miller3e498532014-01-10 10:37:05 +110053 fatal("xcalloc: out of memory (allocating %zu bytes)",
54 size * nmemb);
Damien Miller07d86be2006-03-26 14:19:21 +110055 return ptr;
56}
57
58void *
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000059xreallocarray(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060{
Damien Miller95def091999-11-25 00:26:21 +110061 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000063 new_ptr = reallocarray(ptr, nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110064 if (new_ptr == NULL)
deraadt@openbsd.org657a5fb2015-04-24 01:36:00 +000065 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
66 nmemb, size);
Damien Miller95def091999-11-25 00:26:21 +110067 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068}
69
Damien Miller95def091999-11-25 00:26:21 +110070char *
71xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072{
Ben Lindstromff6458e2001-08-06 21:03:23 +000073 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000074 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Ben Lindstromff6458e2001-08-06 21:03:23 +000076 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000077 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110078 strlcpy(cp, str, len);
79 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080}
Damien Miller07d86be2006-03-26 14:19:21 +110081
82int
83xasprintf(char **ret, const char *fmt, ...)
84{
85 va_list ap;
86 int i;
87
88 va_start(ap, fmt);
89 i = vasprintf(ret, fmt, ap);
90 va_end(ap);
91
92 if (i < 0 || *ret == NULL)
93 fatal("xasprintf: could not allocate memory");
94
95 return (i);
96}