blob: 8a23b8b70ffd700854739bb059f85b1ba9cd1335 [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"
Ben Lindstroma905ecd2001-02-10 23:34:54 +000016RCSID("$OpenBSD: xmalloc.c,v 1.14 2001/02/07 18:04:50 itojun 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)
42 fatal("xrealloc: NULL pointer given as argument");
43 new_ptr = realloc(ptr, new_size);
44 if (new_ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000045 fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size);
Damien Miller95def091999-11-25 00:26:21 +110046 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047}
48
Damien Miller4af51302000-04-16 11:18:38 +100049void
Damien Miller95def091999-11-25 00:26:21 +110050xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051{
Damien Miller95def091999-11-25 00:26:21 +110052 if (ptr == NULL)
53 fatal("xfree: NULL pointer given as argument");
54 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055}
56
Damien Miller95def091999-11-25 00:26:21 +110057char *
58xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059{
Ben Lindstrom91fd62a2001-01-29 08:10:11 +000060 size_t len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000061 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Ben Lindstroma905ecd2001-02-10 23:34:54 +000063 if (len == 0)
64 fatal("xstrdup: zero size");
65 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110066 strlcpy(cp, str, len);
67 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100068}