Annotation of src/usr.bin/cvs/xmalloc.c, Revision 1.14
1.14 ! deraadt 1: /* $OpenBSD: xmalloc.c,v 1.13 2015/11/17 18:25:02 tobias Exp $ */
1.1 joris 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
1.6 ray 6: * Versions of malloc and friends that check their results, and never return
1.1 joris 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:
1.13 tobias 16: #include <errno.h>
17: #include <limits.h>
1.11 millert 18: #include <stdint.h>
1.8 otto 19: #include <stdio.h>
20: #include <stdlib.h>
21: #include <string.h>
1.1 joris 22:
1.8 otto 23: #include "log.h"
1.3 xsa 24: #include "xmalloc.h"
1.1 joris 25:
26: void *
27: xmalloc(size_t size)
28: {
29: void *ptr;
30:
31: if (size == 0)
32: fatal("xmalloc: zero size");
33: ptr = malloc(size);
34: if (ptr == NULL)
1.13 tobias 35: fatal("xmalloc: allocating %zu bytes: %s",
36: size, strerror(errno));
1.1 joris 37: return ptr;
38: }
39:
40: void *
1.6 ray 41: xcalloc(size_t nmemb, size_t size)
42: {
43: void *ptr;
44:
45: if (size == 0 || nmemb == 0)
46: fatal("xcalloc: zero size");
47: ptr = calloc(nmemb, size);
48: if (ptr == NULL)
1.13 tobias 49: fatal("xcalloc: allocating %zu * %zu bytes: %s",
50: nmemb, size, strerror(errno));
1.6 ray 51: return ptr;
52: }
53:
54: void *
1.10 deraadt 55: xreallocarray(void *ptr, size_t nmemb, size_t size)
1.1 joris 56: {
57: void *new_ptr;
58:
1.13 tobias 59: if (nmemb == 0 || size == 0)
60: fatal("xreallocarray: zero size");
61: new_ptr = reallocarray(ptr, nmemb, size);
1.1 joris 62: if (new_ptr == NULL)
1.13 tobias 63: fatal("xreallocarray: allocating %zu * %zu bytes: %s",
64: nmemb, size, strerror(errno));
1.1 joris 65: return new_ptr;
66: }
67:
68: char *
69: xstrdup(const char *str)
70: {
71: char *cp;
72:
1.13 tobias 73: if ((cp = strdup(str)) == NULL)
74: fatal("xstrdup: %s", strerror(errno));
1.1 joris 75: return cp;
1.6 ray 76: }
77:
78: int
79: xasprintf(char **ret, const char *fmt, ...)
80: {
81: va_list ap;
82: int i;
83:
84: va_start(ap, fmt);
85: i = vasprintf(ret, fmt, ap);
86: va_end(ap);
87:
1.14 ! deraadt 88: if (i == -1)
1.13 tobias 89: fatal("xasprintf: %s", strerror(errno));
1.7 xsa 90:
1.13 tobias 91: return i;
1.7 xsa 92: }
93:
94: int
1.13 tobias 95: xsnprintf(char *str, size_t len, const char *fmt, ...)
1.7 xsa 96: {
97: va_list ap;
98: int i;
99:
1.13 tobias 100: if (len > INT_MAX)
101: fatal("xsnprintf: len > INT_MAX");
102:
1.7 xsa 103: va_start(ap, fmt);
1.13 tobias 104: i = vsnprintf(str, len, fmt, ap);
1.7 xsa 105: va_end(ap);
106:
1.13 tobias 107: if (i < 0 || i >= (int)len)
1.7 xsa 108: fatal("xsnprintf: overflow");
1.6 ray 109:
1.13 tobias 110: return i;
1.1 joris 111: }