djm@openbsd.org | 166927f | 2019-11-12 22:32:48 +0000 | [diff] [blame] | 1 | /* $OpenBSD: xmalloc.c,v 1.36 2019/11/12 22:32:48 djm 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 | |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 18 | #include <stdarg.h> |
Damien Miller | 9af2197 | 2015-02-24 09:04:32 +1100 | [diff] [blame] | 19 | #ifdef HAVE_STDINT_H |
Damien Miller | cfc1897 | 2019-10-09 09:06:35 +1100 | [diff] [blame] | 20 | # include <stdint.h> |
Damien Miller | 9af2197 | 2015-02-24 09:04:32 +1100 | [diff] [blame] | 21 | #endif |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 22 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 23 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 24 | #include <string.h> |
Darren Tucker | 5d19626 | 2006-07-12 22:15:16 +1000 | [diff] [blame] | 25 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 26 | #include "xmalloc.h" |
| 27 | #include "log.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
Damien Miller | 5ac712d | 2016-02-16 10:45:02 +1100 | [diff] [blame] | 29 | #if defined(__OpenBSD__) |
Darren Tucker | 130ef06 | 2019-06-08 00:47:07 +1000 | [diff] [blame] | 30 | char *malloc_options = "S"; |
Damien Miller | 5ac712d | 2016-02-16 10:45:02 +1100 | [diff] [blame] | 31 | #endif /* __OpenBSD__ */ |
dtucker@openbsd.org | ffb1e7e | 2016-02-15 09:47:49 +0000 | [diff] [blame] | 32 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 33 | void * |
| 34 | xmalloc(size_t size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | { |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 36 | void *ptr; |
| 37 | |
| 38 | if (size == 0) |
| 39 | fatal("xmalloc: zero size"); |
| 40 | ptr = malloc(size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | if (ptr == NULL) |
Damien Miller | 3e49853 | 2014-01-10 10:37:05 +1100 | [diff] [blame] | 42 | fatal("xmalloc: out of memory (allocating %zu bytes)", size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 43 | return ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | } |
| 45 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 46 | void * |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 47 | xcalloc(size_t nmemb, size_t size) |
| 48 | { |
| 49 | void *ptr; |
| 50 | |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 51 | if (size == 0 || nmemb == 0) |
| 52 | fatal("xcalloc: zero size"); |
millert@openbsd.org | fd36834 | 2015-02-06 23:21:59 +0000 | [diff] [blame] | 53 | if (SIZE_MAX / nmemb < size) |
| 54 | fatal("xcalloc: nmemb * size > SIZE_MAX"); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 55 | ptr = calloc(nmemb, size); |
| 56 | if (ptr == NULL) |
Damien Miller | 3e49853 | 2014-01-10 10:37:05 +1100 | [diff] [blame] | 57 | fatal("xcalloc: out of memory (allocating %zu bytes)", |
| 58 | size * nmemb); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 59 | return ptr; |
| 60 | } |
| 61 | |
| 62 | void * |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 63 | xreallocarray(void *ptr, size_t nmemb, size_t size) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 65 | void *new_ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 67 | new_ptr = reallocarray(ptr, nmemb, size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 68 | if (new_ptr == NULL) |
deraadt@openbsd.org | 657a5fb | 2015-04-24 01:36:00 +0000 | [diff] [blame] | 69 | fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", |
| 70 | nmemb, size); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | return new_ptr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | } |
| 73 | |
deraadt@openbsd.org | 9e509d4 | 2017-05-31 09:15:42 +0000 | [diff] [blame] | 74 | void * |
| 75 | xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size) |
| 76 | { |
| 77 | void *new_ptr; |
| 78 | |
| 79 | new_ptr = recallocarray(ptr, onmemb, nmemb, size); |
| 80 | if (new_ptr == NULL) |
| 81 | fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)", |
| 82 | nmemb, size); |
| 83 | return new_ptr; |
| 84 | } |
| 85 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 86 | char * |
| 87 | xstrdup(const char *str) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | { |
Ben Lindstrom | ff6458e | 2001-08-06 21:03:23 +0000 | [diff] [blame] | 89 | size_t len; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 90 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 91 | |
Ben Lindstrom | ff6458e | 2001-08-06 21:03:23 +0000 | [diff] [blame] | 92 | len = strlen(str) + 1; |
Ben Lindstrom | a905ecd | 2001-02-10 23:34:54 +0000 | [diff] [blame] | 93 | cp = xmalloc(len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | strlcpy(cp, str, len); |
| 95 | return cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 96 | } |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 97 | |
| 98 | int |
djm@openbsd.org | 166927f | 2019-11-12 22:32:48 +0000 | [diff] [blame] | 99 | xvasprintf(char **ret, const char *fmt, va_list ap) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | i = vasprintf(ret, fmt, ap); |
| 104 | if (i < 0 || *ret == NULL) |
| 105 | fatal("xvasprintf: could not allocate memory"); |
| 106 | return i; |
| 107 | } |
| 108 | |
| 109 | int |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 110 | xasprintf(char **ret, const char *fmt, ...) |
| 111 | { |
| 112 | va_list ap; |
| 113 | int i; |
| 114 | |
| 115 | va_start(ap, fmt); |
djm@openbsd.org | 166927f | 2019-11-12 22:32:48 +0000 | [diff] [blame] | 116 | i = xvasprintf(ret, fmt, ap); |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 117 | va_end(ap); |
djm@openbsd.org | 166927f | 2019-11-12 22:32:48 +0000 | [diff] [blame] | 118 | return i; |
Damien Miller | 07d86be | 2006-03-26 14:19:21 +1100 | [diff] [blame] | 119 | } |