Damien Miller | 3e49853 | 2014-01-10 10:37:05 +1100 | [diff] [blame] | 1 | /* $OpenBSD: xmalloc.c,v 1.29 2014/01/04 17:50:55 tedu 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 | |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 18 | #include <sys/param.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 19 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 20 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 21 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 22 | #include <string.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 23 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 24 | #include "xmalloc.h" |
| 25 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 26 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 27 | void * |
| 28 | xmalloc(size_t size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 29 | { |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 30 | void *ptr; |
| 31 | |
| 32 | if (size == 0) |
| 33 | fatal("xmalloc: zero size"); |
| 34 | ptr = malloc(size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | if (ptr == NULL) |
Damien Miller | 3e49853 | 2014-01-10 10:37:05 +1100 | [diff] [blame] | 36 | fatal("xmalloc: out of memory (allocating %zu bytes)", size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | return ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | } |
| 39 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | void * |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 41 | xcalloc(size_t nmemb, size_t size) |
| 42 | { |
| 43 | void *ptr; |
| 44 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 45 | if (size == 0 || nmemb == 0) |
| 46 | fatal("xcalloc: zero size"); |
Damien Miller | da380be | 2006-03-31 23:09:17 +1100 | [diff] [blame] | 47 | if (SIZE_T_MAX / nmemb < size) |
| 48 | fatal("xcalloc: nmemb * size > SIZE_T_MAX"); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 49 | ptr = calloc(nmemb, size); |
| 50 | if (ptr == NULL) |
Damien Miller | 3e49853 | 2014-01-10 10:37:05 +1100 | [diff] [blame] | 51 | fatal("xcalloc: out of memory (allocating %zu bytes)", |
| 52 | size * nmemb); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 53 | return ptr; |
| 54 | } |
| 55 | |
| 56 | void * |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 57 | xrealloc(void *ptr, size_t nmemb, size_t size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 58 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | void *new_ptr; |
Damien Miller | 3681209 | 2006-03-26 14:22:47 +1100 | [diff] [blame] | 60 | size_t new_size = nmemb * size; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 62 | if (new_size == 0) |
| 63 | fatal("xrealloc: zero size"); |
Damien Miller | da380be | 2006-03-31 23:09:17 +1100 | [diff] [blame] | 64 | if (SIZE_T_MAX / nmemb < size) |
| 65 | fatal("xrealloc: nmemb * size > SIZE_T_MAX"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | if (ptr == NULL) |
Damien Miller | 0b1e0a1 | 2001-04-16 18:27:07 +1000 | [diff] [blame] | 67 | new_ptr = malloc(new_size); |
| 68 | else |
| 69 | new_ptr = realloc(ptr, new_size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 70 | if (new_ptr == NULL) |
Damien Miller | 3e49853 | 2014-01-10 10:37:05 +1100 | [diff] [blame] | 71 | fatal("xrealloc: out of memory (new_size %zu bytes)", |
| 72 | new_size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 73 | return new_ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | } |
| 75 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 76 | char * |
| 77 | xstrdup(const char *str) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | { |
Ben Lindstrom | ff6458e | 2001-08-06 21:03:23 +0000 | [diff] [blame] | 79 | size_t len; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 80 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 81 | |
Ben Lindstrom | ff6458e | 2001-08-06 21:03:23 +0000 | [diff] [blame] | 82 | len = strlen(str) + 1; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 83 | cp = xmalloc(len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 84 | strlcpy(cp, str, len); |
| 85 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 86 | } |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 87 | |
| 88 | int |
| 89 | xasprintf(char **ret, const char *fmt, ...) |
| 90 | { |
| 91 | va_list ap; |
| 92 | int i; |
| 93 | |
| 94 | va_start(ap, fmt); |
| 95 | i = vasprintf(ret, fmt, ap); |
| 96 | va_end(ap); |
| 97 | |
| 98 | if (i < 0 || *ret == NULL) |
| 99 | fatal("xasprintf: could not allocate memory"); |
| 100 | |
| 101 | return (i); |
| 102 | } |