Blame


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