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