blob: 35f668df39f33352b53988db9986143f6fe2573e [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 Lindstrom91fd62a2001-01-29 08:10:11 +000016RCSID("$OpenBSD: xmalloc.c,v 1.10 2001/01/28 20:53:21 stevesk 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{
Damien Miller95def091999-11-25 00:26:21 +110024 void *ptr = malloc(size);
25 if (ptr == NULL)
26 fatal("xmalloc: out of memory (allocating %d bytes)", (int) size);
27 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028}
29
Damien Miller95def091999-11-25 00:26:21 +110030void *
31xrealloc(void *ptr, size_t new_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032{
Damien Miller95def091999-11-25 00:26:21 +110033 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034
Damien Miller95def091999-11-25 00:26:21 +110035 if (ptr == NULL)
36 fatal("xrealloc: NULL pointer given as argument");
37 new_ptr = realloc(ptr, new_size);
38 if (new_ptr == NULL)
39 fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size);
40 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100041}
42
Damien Miller4af51302000-04-16 11:18:38 +100043void
Damien Miller95def091999-11-25 00:26:21 +110044xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045{
Damien Miller95def091999-11-25 00:26:21 +110046 if (ptr == NULL)
47 fatal("xfree: NULL pointer given as argument");
48 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100049}
50
Damien Miller95def091999-11-25 00:26:21 +110051char *
52xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053{
Ben Lindstrom91fd62a2001-01-29 08:10:11 +000054 size_t len = strlen(str) + 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100055
Damien Miller95def091999-11-25 00:26:21 +110056 char *cp = xmalloc(len);
57 strlcpy(cp, str, len);
58 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059}