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