Annotation of src/usr.bin/diff/xmalloc.c, Revision 1.10
1.10 ! deraadt 1: /* $OpenBSD: xmalloc.c,v 1.9 2015/11/17 18:25:02 tobias Exp $ */
1.1 ray 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 <err.h>
17: #include <stdarg.h>
1.5 millert 18: #include <stdint.h>
1.1 ray 19: #include <stdio.h>
20: #include <stdlib.h>
21: #include <string.h>
22:
23: #include "xmalloc.h"
24:
25: void *
26: xmalloc(size_t size)
27: {
28: void *ptr;
29:
1.9 tobias 30: if (size == 0)
31: errx(2, "xmalloc: zero size");
1.1 ray 32: ptr = malloc(size);
33: if (ptr == NULL)
1.9 tobias 34: err(2, "xmalloc: allocating %zu bytes", size);
1.1 ray 35: return ptr;
36: }
37:
38: void *
1.2 ray 39: xcalloc(size_t nmemb, size_t size)
40: {
41: void *ptr;
42:
43: ptr = calloc(nmemb, size);
44: if (ptr == NULL)
1.9 tobias 45: err(2, "xcalloc: allocating %zu * %zu bytes", nmemb, size);
1.2 ray 46: return ptr;
47: }
48:
49: void *
1.6 deraadt 50: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1 ray 51: {
52: void *new_ptr;
53:
1.6 deraadt 54: new_ptr = reallocarray(ptr, nmemb, size);
1.1 ray 55: if (new_ptr == NULL)
1.9 tobias 56: err(2, "xreallocarray: allocating %zu * %zu bytes",
57: nmemb, size);
1.1 ray 58: return new_ptr;
59: }
60:
61: char *
62: xstrdup(const char *str)
63: {
64: char *cp;
65:
1.7 nicm 66: if ((cp = strdup(str)) == NULL)
1.9 tobias 67: err(2, "xstrdup");
1.1 ray 68: return cp;
69: }
70:
71: int
72: xasprintf(char **ret, const char *fmt, ...)
73: {
74: va_list ap;
75: int i;
76:
77: va_start(ap, fmt);
78: i = vasprintf(ret, fmt, ap);
79: va_end(ap);
80:
1.10 ! deraadt 81: if (i == -1)
1.8 tedu 82: err(2, "xasprintf");
1.1 ray 83:
1.9 tobias 84: return i;
1.1 ray 85: }