Line  | Count  | Source  | 
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  | 1.29M  | { | 
34  | 1.29M  |   void *ptr;  | 
35  |  |  | 
36  | 1.29M  |   if (size == 0)  | 
37  | 0  |     fatal("xmalloc: zero size"); | 
38  | 1.29M  |   ptr = malloc(size);  | 
39  | 1.29M  |   if (ptr == NULL)  | 
40  | 0  |     fatal("xmalloc: out of memory (allocating %zu bytes)", size); | 
41  | 1.29M  |   return ptr;  | 
42  | 1.29M  | }  | 
43  |  |  | 
44  |  | void *  | 
45  |  | xcalloc(size_t nmemb, size_t size)  | 
46  | 16.2k  | { | 
47  | 16.2k  |   void *ptr;  | 
48  |  |  | 
49  | 16.2k  |   if (size == 0 || nmemb == 0)  | 
50  | 0  |     fatal("xcalloc: zero size"); | 
51  | 16.2k  |   if (SIZE_MAX / nmemb < size)  | 
52  | 0  |     fatal("xcalloc: nmemb * size > SIZE_MAX"); | 
53  | 16.2k  |   ptr = calloc(nmemb, size);  | 
54  | 16.2k  |   if (ptr == NULL)  | 
55  | 0  |     fatal("xcalloc: out of memory (allocating %zu bytes)", | 
56  | 16.2k  |         size * nmemb);  | 
57  | 16.2k  |   return ptr;  | 
58  | 16.2k  | }  | 
59  |  |  | 
60  |  | void *  | 
61  |  | xreallocarray(void *ptr, size_t nmemb, size_t size)  | 
62  | 0  | { | 
63  | 0  |   void *new_ptr;  | 
64  |  | 
  | 
65  | 0  |   new_ptr = reallocarray(ptr, nmemb, size);  | 
66  | 0  |   if (new_ptr == NULL)  | 
67  | 0  |     fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", | 
68  | 0  |         nmemb, size);  | 
69  | 0  |   return new_ptr;  | 
70  | 0  | }  | 
71  |  |  | 
72  |  | void *  | 
73  |  | xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)  | 
74  | 1.16k  | { | 
75  | 1.16k  |   void *new_ptr;  | 
76  |  |  | 
77  | 1.16k  |   new_ptr = recallocarray(ptr, onmemb, nmemb, size);  | 
78  | 1.16k  |   if (new_ptr == NULL)  | 
79  | 0  |     fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)", | 
80  | 1.16k  |         nmemb, size);  | 
81  | 1.16k  |   return new_ptr;  | 
82  | 1.16k  | }  | 
83  |  |  | 
84  |  | char *  | 
85  |  | xstrdup(const char *str)  | 
86  | 1.29M  | { | 
87  | 1.29M  |   size_t len;  | 
88  | 1.29M  |   char *cp;  | 
89  |  |  | 
90  | 1.29M  |   len = strlen(str) + 1;  | 
91  | 1.29M  |   cp = xmalloc(len);  | 
92  | 1.29M  |   return memcpy(cp, str, len);  | 
93  | 1.29M  | }  | 
94  |  |  | 
95  |  | int  | 
96  |  | xvasprintf(char **ret, const char *fmt, va_list ap)  | 
97  | 0  | { | 
98  | 0  |   int i;  | 
99  |  | 
  | 
100  | 0  |   i = vasprintf(ret, fmt, ap);  | 
101  | 0  |   if (i < 0 || *ret == NULL)  | 
102  | 0  |     fatal("xvasprintf: could not allocate memory"); | 
103  | 0  |   return i;  | 
104  | 0  | }  | 
105  |  |  | 
106  |  | int  | 
107  |  | xasprintf(char **ret, const char *fmt, ...)  | 
108  | 0  | { | 
109  | 0  |   va_list ap;  | 
110  | 0  |   int i;  | 
111  |  | 
  | 
112  | 0  |   va_start(ap, fmt);  | 
113  | 0  |   i = xvasprintf(ret, fmt, ap);  | 
114  |  |   va_end(ap);  | 
115  | 0  |   return i;  | 
116  | 0  | }  |