Blame


1 014c66b6 2023-06-25 op /* $OpenBSD: xmalloc.c,v 1.4 2019/06/28 05:44:09 deraadt Exp $ */
2 014c66b6 2023-06-25 op /*
3 014c66b6 2023-06-25 op * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 014c66b6 2023-06-25 op * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 014c66b6 2023-06-25 op * All rights reserved
6 014c66b6 2023-06-25 op * Versions of malloc and friends that check their results, and never return
7 014c66b6 2023-06-25 op * failure (they call fatal if they encounter an error).
8 014c66b6 2023-06-25 op *
9 014c66b6 2023-06-25 op * As far as I am concerned, the code I have written for this software
10 014c66b6 2023-06-25 op * can be used freely for any purpose. Any derived versions of this
11 014c66b6 2023-06-25 op * software must be clearly marked as such, and if the derived work is
12 014c66b6 2023-06-25 op * incompatible with the protocol description in the RFC file, it must be
13 014c66b6 2023-06-25 op * called by a name other than "ssh" or "Secure Shell".
14 014c66b6 2023-06-25 op */
15 014c66b6 2023-06-25 op
16 014c66b6 2023-06-25 op #include <stdarg.h>
17 014c66b6 2023-06-25 op #include <stdint.h>
18 014c66b6 2023-06-25 op #include <stdio.h>
19 014c66b6 2023-06-25 op #include <stdlib.h>
20 014c66b6 2023-06-25 op #include <string.h>
21 014c66b6 2023-06-25 op #include <syslog.h>
22 014c66b6 2023-06-25 op
23 014c66b6 2023-06-25 op #include "log.h"
24 014c66b6 2023-06-25 op #include "xmalloc.h"
25 014c66b6 2023-06-25 op
26 014c66b6 2023-06-25 op void *
27 014c66b6 2023-06-25 op xmalloc(size_t size)
28 014c66b6 2023-06-25 op {
29 014c66b6 2023-06-25 op void *ptr;
30 014c66b6 2023-06-25 op
31 014c66b6 2023-06-25 op if (size == 0)
32 014c66b6 2023-06-25 op fatal("xmalloc: zero size");
33 014c66b6 2023-06-25 op ptr = malloc(size);
34 014c66b6 2023-06-25 op if (ptr == NULL)
35 014c66b6 2023-06-25 op fatal("xmalloc: allocating %zu bytes", size);
36 014c66b6 2023-06-25 op return ptr;
37 014c66b6 2023-06-25 op }
38 014c66b6 2023-06-25 op
39 014c66b6 2023-06-25 op void *
40 014c66b6 2023-06-25 op xcalloc(size_t nmemb, size_t size)
41 014c66b6 2023-06-25 op {
42 014c66b6 2023-06-25 op void *ptr;
43 014c66b6 2023-06-25 op
44 014c66b6 2023-06-25 op if (size == 0 || nmemb == 0)
45 014c66b6 2023-06-25 op fatal("xcalloc: zero size");
46 014c66b6 2023-06-25 op ptr = calloc(nmemb, size);
47 014c66b6 2023-06-25 op if (ptr == NULL)
48 014c66b6 2023-06-25 op fatal("xcalloc: allocating %zu * %zu bytes", nmemb, size);
49 014c66b6 2023-06-25 op return ptr;
50 014c66b6 2023-06-25 op }
51 014c66b6 2023-06-25 op
52 014c66b6 2023-06-25 op void *
53 014c66b6 2023-06-25 op xreallocarray(void *ptr, size_t nmemb, size_t size)
54 014c66b6 2023-06-25 op {
55 014c66b6 2023-06-25 op void *new_ptr;
56 014c66b6 2023-06-25 op
57 014c66b6 2023-06-25 op new_ptr = reallocarray(ptr, nmemb, size);
58 014c66b6 2023-06-25 op if (new_ptr == NULL)
59 014c66b6 2023-06-25 op fatal("xreallocarray: allocating %zu * %zu bytes",
60 014c66b6 2023-06-25 op nmemb, size);
61 014c66b6 2023-06-25 op return new_ptr;
62 014c66b6 2023-06-25 op }
63 014c66b6 2023-06-25 op
64 014c66b6 2023-06-25 op void *
65 014c66b6 2023-06-25 op xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
66 014c66b6 2023-06-25 op {
67 014c66b6 2023-06-25 op void *new_ptr;
68 014c66b6 2023-06-25 op
69 014c66b6 2023-06-25 op new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
70 014c66b6 2023-06-25 op if (new_ptr == NULL)
71 014c66b6 2023-06-25 op fatal("xrecallocarray: allocating %zu * %zu bytes",
72 014c66b6 2023-06-25 op nmemb, size);
73 014c66b6 2023-06-25 op return new_ptr;
74 014c66b6 2023-06-25 op }
75 014c66b6 2023-06-25 op
76 014c66b6 2023-06-25 op char *
77 014c66b6 2023-06-25 op xstrdup(const char *str)
78 014c66b6 2023-06-25 op {
79 014c66b6 2023-06-25 op char *cp;
80 014c66b6 2023-06-25 op
81 014c66b6 2023-06-25 op if ((cp = strdup(str)) == NULL)
82 014c66b6 2023-06-25 op fatal("xstrdup");
83 014c66b6 2023-06-25 op return cp;
84 014c66b6 2023-06-25 op }
85 014c66b6 2023-06-25 op
86 014c66b6 2023-06-25 op int
87 014c66b6 2023-06-25 op xasprintf(char **ret, const char *fmt, ...)
88 014c66b6 2023-06-25 op {
89 014c66b6 2023-06-25 op va_list ap;
90 014c66b6 2023-06-25 op int i;
91 014c66b6 2023-06-25 op
92 014c66b6 2023-06-25 op va_start(ap, fmt);
93 014c66b6 2023-06-25 op i = vasprintf(ret, fmt, ap);
94 014c66b6 2023-06-25 op va_end(ap);
95 014c66b6 2023-06-25 op
96 014c66b6 2023-06-25 op if (i == -1)
97 014c66b6 2023-06-25 op fatal("xasprintf");
98 014c66b6 2023-06-25 op
99 014c66b6 2023-06-25 op return i;
100 014c66b6 2023-06-25 op }