Damien Miller | da380be | 2006-03-31 23:09:17 +1100 | [diff] [blame] | 1 | /* $OpenBSD: xmalloc.c,v 1.21 2006/03/27 01:21:18 deraadt Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Versions of malloc and friends that check their results, and never return |
| 7 | * failure (they call fatal if they encounter an error). |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
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 * |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 35 | xcalloc(size_t nmemb, size_t size) |
| 36 | { |
| 37 | void *ptr; |
| 38 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 39 | if (size == 0 || nmemb == 0) |
| 40 | fatal("xcalloc: zero size"); |
Damien Miller | da380be | 2006-03-31 23:09:17 +1100 | [diff] [blame] | 41 | if (SIZE_T_MAX / nmemb < size) |
| 42 | fatal("xcalloc: nmemb * size > SIZE_T_MAX"); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 43 | ptr = calloc(nmemb, size); |
| 44 | if (ptr == NULL) |
| 45 | fatal("xcalloc: out of memory (allocating %lu bytes)", |
| 46 | (u_long)(size * nmemb)); |
| 47 | return ptr; |
| 48 | } |
| 49 | |
| 50 | void * |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 51 | xrealloc(void *ptr, size_t nmemb, size_t size) |
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 | void *new_ptr; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 54 | size_t new_size = nmemb * size; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 56 | if (new_size == 0) |
| 57 | fatal("xrealloc: zero size"); |
Damien Miller | da380be | 2006-03-31 23:09:17 +1100 | [diff] [blame] | 58 | if (SIZE_T_MAX / nmemb < size) |
| 59 | fatal("xrealloc: nmemb * size > SIZE_T_MAX"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 60 | if (ptr == NULL) |
Damien Miller | 0b1e0a1 | 2001-04-16 18:27:07 +1000 | [diff] [blame] | 61 | new_ptr = malloc(new_size); |
| 62 | else |
| 63 | new_ptr = realloc(ptr, new_size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 64 | if (new_ptr == NULL) |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 65 | fatal("xrealloc: out of memory (new_size %lu bytes)", |
| 66 | (u_long) new_size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | return new_ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | } |
| 69 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 70 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | xfree(void *ptr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 73 | if (ptr == NULL) |
| 74 | fatal("xfree: NULL pointer given as argument"); |
| 75 | free(ptr); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | } |
| 77 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | char * |
| 79 | xstrdup(const char *str) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | { |
Ben Lindstrom | ff6458e | 2001-08-06 21:03:23 +0000 | [diff] [blame] | 81 | size_t len; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 82 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | |
Ben Lindstrom | ff6458e | 2001-08-06 21:03:23 +0000 | [diff] [blame] | 84 | len = strlen(str) + 1; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 85 | cp = xmalloc(len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 86 | strlcpy(cp, str, len); |
| 87 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | } |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 89 | |
| 90 | int |
| 91 | xasprintf(char **ret, const char *fmt, ...) |
| 92 | { |
| 93 | va_list ap; |
| 94 | int i; |
| 95 | |
| 96 | va_start(ap, fmt); |
| 97 | i = vasprintf(ret, fmt, ap); |
| 98 | va_end(ap); |
| 99 | |
| 100 | if (i < 0 || *ret == NULL) |
| 101 | fatal("xasprintf: could not allocate memory"); |
| 102 | |
| 103 | return (i); |
| 104 | } |