blob: 738c9cdc390265a6f01f24b30bef79e87a8c8fc2 [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).
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * 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 Millere4340be2000-09-16 13:29:08 +110016RCSID("$OpenBSD: xmalloc.c,v 1.8 2000/09/07 20:27:55 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#include "ssh.h"
19
Damien Miller95def091999-11-25 00:26:21 +110020void *
21xmalloc(size_t size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022{
Damien Miller95def091999-11-25 00:26:21 +110023 void *ptr = malloc(size);
24 if (ptr == NULL)
25 fatal("xmalloc: out of memory (allocating %d bytes)", (int) size);
26 return ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100027}
28
Damien Miller95def091999-11-25 00:26:21 +110029void *
30xrealloc(void *ptr, size_t new_size)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100031{
Damien Miller95def091999-11-25 00:26:21 +110032 void *new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
Damien Miller95def091999-11-25 00:26:21 +110034 if (ptr == NULL)
35 fatal("xrealloc: NULL pointer given as argument");
36 new_ptr = realloc(ptr, new_size);
37 if (new_ptr == NULL)
38 fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size);
39 return new_ptr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040}
41
Damien Miller4af51302000-04-16 11:18:38 +100042void
Damien Miller95def091999-11-25 00:26:21 +110043xfree(void *ptr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044{
Damien Miller95def091999-11-25 00:26:21 +110045 if (ptr == NULL)
46 fatal("xfree: NULL pointer given as argument");
47 free(ptr);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048}
49
Damien Miller95def091999-11-25 00:26:21 +110050char *
51xstrdup(const char *str)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052{
Damien Miller95def091999-11-25 00:26:21 +110053 int len = strlen(str) + 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller95def091999-11-25 00:26:21 +110055 char *cp = xmalloc(len);
56 strlcpy(cp, str, len);
57 return cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058}