Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Versions of malloc and friends that check their results, and never return |
| 6 | * failure (they call fatal if they encounter an error). |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 13 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 14 | |
| 15 | #include "includes.h" |
Damien Miller | 0b1e0a1 | 2001-04-16 18:27:07 +1000 | [diff] [blame] | 16 | RCSID("$OpenBSD: xmalloc.c,v 1.15 2001/04/16 08:05:34 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 17 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 18 | #include "xmalloc.h" |
| 19 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 21 | void * |
| 22 | xmalloc(size_t size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 23 | { |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 24 | void *ptr; |
| 25 | |
| 26 | if (size == 0) |
| 27 | fatal("xmalloc: zero size"); |
| 28 | ptr = malloc(size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 29 | if (ptr == NULL) |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 30 | fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 31 | return ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | } |
| 33 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 34 | void * |
| 35 | xrealloc(void *ptr, size_t new_size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | void *new_ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 39 | if (new_size == 0) |
| 40 | fatal("xrealloc: zero size"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | if (ptr == NULL) |
Damien Miller | 0b1e0a1 | 2001-04-16 18:27:07 +1000 | [diff] [blame] | 42 | new_ptr = malloc(new_size); |
| 43 | else |
| 44 | new_ptr = realloc(ptr, new_size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | if (new_ptr == NULL) |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 46 | fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | return new_ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | } |
| 49 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 50 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | xfree(void *ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 53 | if (ptr == NULL) |
| 54 | fatal("xfree: NULL pointer given as argument"); |
| 55 | free(ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | } |
| 57 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | char * |
| 59 | xstrdup(const char *str) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | { |
Ben Lindstrom | 91fd62a | 2001-01-29 08:10:11 +0000 | [diff] [blame] | 61 | size_t len = strlen(str) + 1; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 62 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 64 | if (len == 0) |
| 65 | fatal("xstrdup: zero size"); |
| 66 | cp = xmalloc(len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | strlcpy(cp, str, len); |
| 68 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | } |