Blame


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