blob: 5046627490769c3a9926f800b001c2a3bf0fa93b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * Versions of malloc and friends that check their results, and never return
6 * failure (they call fatal if they encounter an error).
Kevin Stevesef4eea92001-02-05 12:42:17 +00007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Miller0b1e0a12001-04-16 18:27:07 +100016RCSID("$OpenBSD: xmalloc.c,v 1.15 2001/04/16 08:05:34 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
Ben Lindstrom226cfa02001-01-22 05:34:40 +000018#include "xmalloc.h"
19#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
Damien Miller95def091999-11-25 00:26:21 +110021void *
22xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100023{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000024 void *ptr;
25
26 if (size == 0)
27 fatal("xmalloc: zero size");
28 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110029 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000030 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110031 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032}
33
Damien Miller95def091999-11-25 00:26:21 +110034void *
35xrealloc(void *ptr, size_t new_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036{
Damien Miller95def091999-11-25 00:26:21 +110037 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
Ben Lindstroma905ecd2001-02-10 23:34:54 +000039 if (new_size == 0)
40 fatal("xrealloc: zero size");
Damien Miller95def091999-11-25 00:26:21 +110041 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100042 new_ptr = malloc(new_size);
43 else
44 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110045 if (new_ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000046 fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110047 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048}
49
Damien Miller4af51302000-04-16 11:18:38 +100050void
Damien Miller95def091999-11-25 00:26:21 +110051xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052{
Damien Miller95def091999-11-25 00:26:21 +110053 if (ptr == NULL)
54 fatal("xfree: NULL pointer given as argument");
55 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
57
Damien Miller95def091999-11-25 00:26:21 +110058char *
59xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060{
Ben Lindstrom91fd62a2001-01-29 08:10:11 +000061 size_t len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000062 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
Ben Lindstroma905ecd2001-02-10 23:34:54 +000064 if (len == 0)
65 fatal("xstrdup: zero size");
66 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110067 strlcpy(cp, str, len);
68 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069}