blob: 6aea495ef93565c7e7ca92eaf3f8b62a4d64557a [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: xmalloc.c,v 1.26 2006/08/01 23:22:48 stevesk 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
Damien Miller8dbffe72006-08-05 11:02:17 +100018#include <sys/param.h>
19
Darren Tucker5d196262006-07-12 22:15:16 +100020#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100021#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100022#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100023#include <string.h>
Darren Tucker5d196262006-07-12 22:15:16 +100024
Ben Lindstrom226cfa02001-01-22 05:34:40 +000025#include "xmalloc.h"
26#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027
Damien Miller95def091999-11-25 00:26:21 +110028void *
29xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000031 void *ptr;
32
33 if (size == 0)
34 fatal("xmalloc: zero size");
35 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110036 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000037 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110038 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039}
40
Damien Miller95def091999-11-25 00:26:21 +110041void *
Damien Miller07d86be2006-03-26 14:19:21 +110042xcalloc(size_t nmemb, size_t size)
43{
44 void *ptr;
45
Damien Miller07d86be2006-03-26 14:19:21 +110046 if (size == 0 || nmemb == 0)
47 fatal("xcalloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110048 if (SIZE_T_MAX / nmemb < size)
49 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
Damien Miller07d86be2006-03-26 14:19:21 +110050 ptr = calloc(nmemb, size);
51 if (ptr == NULL)
52 fatal("xcalloc: out of memory (allocating %lu bytes)",
53 (u_long)(size * nmemb));
54 return ptr;
55}
56
57void *
Damien Miller36812092006-03-26 14:22:47 +110058xrealloc(void *ptr, size_t nmemb, size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059{
Damien Miller95def091999-11-25 00:26:21 +110060 void *new_ptr;
Damien Miller36812092006-03-26 14:22:47 +110061 size_t new_size = nmemb * size;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Ben Lindstroma905ecd2001-02-10 23:34:54 +000063 if (new_size == 0)
64 fatal("xrealloc: zero size");
Damien Millerda380be2006-03-31 23:09:17 +110065 if (SIZE_T_MAX / nmemb < size)
66 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
Damien Miller95def091999-11-25 00:26:21 +110067 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100068 new_ptr = malloc(new_size);
69 else
70 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110071 if (new_ptr == NULL)
Damien Miller36812092006-03-26 14:22:47 +110072 fatal("xrealloc: out of memory (new_size %lu bytes)",
73 (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110074 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075}
76
Damien Miller4af51302000-04-16 11:18:38 +100077void
Damien Miller95def091999-11-25 00:26:21 +110078xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079{
Damien Miller95def091999-11-25 00:26:21 +110080 if (ptr == NULL)
81 fatal("xfree: NULL pointer given as argument");
82 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083}
84
Damien Miller95def091999-11-25 00:26:21 +110085char *
86xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087{
Ben Lindstromff6458e2001-08-06 21:03:23 +000088 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000089 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Ben Lindstromff6458e2001-08-06 21:03:23 +000091 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000092 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110093 strlcpy(cp, str, len);
94 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095}
Damien Miller07d86be2006-03-26 14:19:21 +110096
97int
98xasprintf(char **ret, const char *fmt, ...)
99{
100 va_list ap;
101 int i;
102
103 va_start(ap, fmt);
104 i = vasprintf(ret, fmt, ap);
105 va_end(ap);
106
107 if (i < 0 || *ret == NULL)
108 fatal("xasprintf: could not allocate memory");
109
110 return (i);
111}