blob: 64e439853e0206859abe5fae5510c76744b236f4 [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 Millerd4a8b7e1999-10-27 13:42:43 +100016
Ben Lindstrom226cfa02001-01-22 05:34:40 +000017#include "xmalloc.h"
18#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
Damien Miller95def091999-11-25 00:26:21 +110020void *
21xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022{
Ben Lindstroma905ecd2001-02-10 23:34:54 +000023 void *ptr;
24
25 if (size == 0)
26 fatal("xmalloc: zero size");
27 ptr = malloc(size);
Damien Miller95def091999-11-25 00:26:21 +110028 if (ptr == NULL)
Ben Lindstroma905ecd2001-02-10 23:34:54 +000029 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
Damien Miller95def091999-11-25 00:26:21 +110030 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031}
32
Damien Miller95def091999-11-25 00:26:21 +110033void *
34xrealloc(void *ptr, size_t new_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035{
Damien Miller95def091999-11-25 00:26:21 +110036 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
Ben Lindstroma905ecd2001-02-10 23:34:54 +000038 if (new_size == 0)
39 fatal("xrealloc: zero size");
Damien Miller95def091999-11-25 00:26:21 +110040 if (ptr == NULL)
Damien Miller0b1e0a12001-04-16 18:27:07 +100041 new_ptr = malloc(new_size);
42 else
43 new_ptr = realloc(ptr, new_size);
Damien Miller95def091999-11-25 00:26:21 +110044 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 Lindstromff6458e2001-08-06 21:03:23 +000060 size_t len;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000061 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Ben Lindstromff6458e2001-08-06 21:03:23 +000063 len = strlen(str) + 1;
Ben Lindstroma905ecd2001-02-10 23:34:54 +000064 cp = xmalloc(len);
Damien Miller95def091999-11-25 00:26:21 +110065 strlcpy(cp, str, len);
66 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067}