1 /* $OpenBSD: xmalloc.c,v 1.38 2025/05/23 00:40:45 deraadt Exp $ */ 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 18 #include <stdarg.h> 19 #include <stdint.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include "xmalloc.h" 25 #include "log.h" 26 27 #if defined(__OpenBSD__) 28 const char * const malloc_options = "S"; 29 #endif /* __OpenBSD__ */ 30 31 void * 32 xmalloc(size_t size) 33 { 34 void *ptr; 35 36 if (size == 0) 37 fatal("xmalloc: zero size"); 38 ptr = malloc(size); 39 if (ptr == NULL) 40 fatal("xmalloc: out of memory (allocating %zu bytes)", size); 41 return ptr; 42 } 43 44 void * 45 xcalloc(size_t nmemb, size_t size) 46 { 47 void *ptr; 48 49 if (size == 0 || nmemb == 0) 50 fatal("xcalloc: zero size"); 51 if (SIZE_MAX / nmemb < size) 52 fatal("xcalloc: nmemb * size > SIZE_MAX"); 53 ptr = calloc(nmemb, size); 54 if (ptr == NULL) 55 fatal("xcalloc: out of memory (allocating %zu bytes)", 56 size * nmemb); 57 return ptr; 58 } 59 60 void * 61 xreallocarray(void *ptr, size_t nmemb, size_t size) 62 { 63 void *new_ptr; 64 65 new_ptr = reallocarray(ptr, nmemb, size); 66 if (new_ptr == NULL) 67 fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", 68 nmemb, size); 69 return new_ptr; 70 } 71 72 void * 73 xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size) 74 { 75 void *new_ptr; 76 77 new_ptr = recallocarray(ptr, onmemb, nmemb, size); 78 if (new_ptr == NULL) 79 fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)", 80 nmemb, size); 81 return new_ptr; 82 } 83 84 char * 85 xstrdup(const char *str) 86 { 87 size_t len; 88 char *cp; 89 90 len = strlen(str) + 1; 91 cp = xmalloc(len); 92 return memcpy(cp, str, len); 93 } 94 95 int 96 xvasprintf(char **ret, const char *fmt, va_list ap) 97 { 98 int i; 99 100 i = vasprintf(ret, fmt, ap); 101 if (i < 0 || *ret == NULL) 102 fatal("xvasprintf: could not allocate memory"); 103 return i; 104 } 105 106 int 107 xasprintf(char **ret, const char *fmt, ...) 108 { 109 va_list ap; 110 int i; 111 112 va_start(ap, fmt); 113 i = xvasprintf(ret, fmt, ap); 114 va_end(ap); 115 return i; 116 } 117