Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 1 | /* $OpenBSD: xmalloc.c,v 1.33 2016/02/15 09:47:49 dtucker Exp $ */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
| 6 | * Versions of malloc and friends that check their results, and never return |
| 7 | * failure (they call fatal if they encounter an error). |
| 8 | * |
| 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". |
| 14 | */ |
| 15 | |
| 16 | #include "includes.h" |
| 17 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 18 | #include <stdarg.h> |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 19 | #ifdef HAVE_STDINT_H |
| 20 | #include <stdint.h> |
| 21 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include "xmalloc.h" |
| 27 | #include "log.h" |
| 28 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 29 | void |
| 30 | ssh_malloc_init(void) |
| 31 | { |
| 32 | #if defined(__OpenBSD__) |
| 33 | extern char *malloc_options; |
| 34 | |
| 35 | malloc_options = "S"; |
| 36 | #endif /* __OpenBSD__ */ |
| 37 | } |
| 38 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 39 | void * |
| 40 | xmalloc(size_t size) |
| 41 | { |
| 42 | void *ptr; |
| 43 | |
| 44 | if (size == 0) |
| 45 | fatal("xmalloc: zero size"); |
| 46 | ptr = malloc(size); |
| 47 | if (ptr == NULL) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 48 | fatal("xmalloc: out of memory (allocating %zu bytes)", size); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 49 | return ptr; |
| 50 | } |
| 51 | |
| 52 | void * |
| 53 | xcalloc(size_t nmemb, size_t size) |
| 54 | { |
| 55 | void *ptr; |
| 56 | |
| 57 | if (size == 0 || nmemb == 0) |
| 58 | fatal("xcalloc: zero size"); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 59 | if (SIZE_MAX / nmemb < size) |
| 60 | fatal("xcalloc: nmemb * size > SIZE_MAX"); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 61 | ptr = calloc(nmemb, size); |
| 62 | if (ptr == NULL) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 63 | fatal("xcalloc: out of memory (allocating %zu bytes)", |
| 64 | size * nmemb); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 65 | return ptr; |
| 66 | } |
| 67 | |
| 68 | void * |
Greg Hartman | ccacbc9 | 2016-02-03 09:59:44 -0800 | [diff] [blame] | 69 | xreallocarray(void *ptr, size_t nmemb, size_t size) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 70 | { |
| 71 | void *new_ptr; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 72 | |
Greg Hartman | ccacbc9 | 2016-02-03 09:59:44 -0800 | [diff] [blame] | 73 | new_ptr = reallocarray(ptr, nmemb, size); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 74 | if (new_ptr == NULL) |
Greg Hartman | ccacbc9 | 2016-02-03 09:59:44 -0800 | [diff] [blame] | 75 | fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", |
| 76 | nmemb, size); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 77 | return new_ptr; |
| 78 | } |
| 79 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 80 | char * |
| 81 | xstrdup(const char *str) |
| 82 | { |
| 83 | size_t len; |
| 84 | char *cp; |
| 85 | |
| 86 | len = strlen(str) + 1; |
| 87 | cp = xmalloc(len); |
| 88 | strlcpy(cp, str, len); |
| 89 | return cp; |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | xasprintf(char **ret, const char *fmt, ...) |
| 94 | { |
| 95 | va_list ap; |
| 96 | int i; |
| 97 | |
| 98 | va_start(ap, fmt); |
| 99 | i = vasprintf(ret, fmt, ap); |
| 100 | va_end(ap); |
| 101 | |
| 102 | if (i < 0 || *ret == NULL) |
| 103 | fatal("xasprintf: could not allocate memory"); |
| 104 | |
| 105 | return (i); |
| 106 | } |