Annotation of src/usr.bin/ssh/xmalloc.c, Revision 1.20
1.20 ! djm 1: /* $OpenBSD$ */
1.1 deraadt 2: /*
1.5 deraadt 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).
1.11 stevesk 8: *
1.8 deraadt 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".
1.5 deraadt 14: */
1.1 deraadt 15:
16: #include "includes.h"
17:
1.9 markus 18: #include "xmalloc.h"
19: #include "log.h"
1.1 deraadt 20:
1.4 markus 21: void *
22: xmalloc(size_t size)
1.1 deraadt 23: {
1.12 deraadt 24: void *ptr;
25:
26: if (size == 0)
27: fatal("xmalloc: zero size");
28: ptr = malloc(size);
1.4 markus 29: if (ptr == NULL)
1.14 itojun 30: fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
1.4 markus 31: return ptr;
1.1 deraadt 32: }
33:
1.4 markus 34: void *
1.18 djm 35: xcalloc(size_t nmemb, size_t size)
36: {
37: void *ptr;
38:
1.19 djm 39: if (nmemb && size && SIZE_T_MAX / nmemb < size)
1.18 djm 40: fatal("xcalloc: nmemb * size > SIZE_T_MAX");
41: if (size == 0 || nmemb == 0)
42: fatal("xcalloc: zero size");
43: ptr = calloc(nmemb, size);
44: if (ptr == NULL)
45: fatal("xcalloc: out of memory (allocating %lu bytes)",
46: (u_long)(size * nmemb));
47: return ptr;
48: }
49:
50: void *
1.19 djm 51: xrealloc(void *ptr, size_t nmemb, size_t size)
1.1 deraadt 52: {
1.4 markus 53: void *new_ptr;
1.19 djm 54: size_t new_size = nmemb * size;
1.1 deraadt 55:
1.19 djm 56: if (nmemb && size && SIZE_T_MAX / nmemb < size)
57: fatal("xrealloc: nmemb * size > SIZE_T_MAX");
1.12 deraadt 58: if (new_size == 0)
1.13 markus 59: fatal("xrealloc: zero size");
1.4 markus 60: if (ptr == NULL)
1.15 deraadt 61: new_ptr = malloc(new_size);
62: else
63: new_ptr = realloc(ptr, new_size);
1.4 markus 64: if (new_ptr == NULL)
1.19 djm 65: fatal("xrealloc: out of memory (new_size %lu bytes)",
66: (u_long) new_size);
1.4 markus 67: return new_ptr;
1.1 deraadt 68: }
69:
1.6 markus 70: void
1.4 markus 71: xfree(void *ptr)
1.1 deraadt 72: {
1.4 markus 73: if (ptr == NULL)
74: fatal("xfree: NULL pointer given as argument");
75: free(ptr);
1.1 deraadt 76: }
77:
1.4 markus 78: char *
79: xstrdup(const char *str)
1.1 deraadt 80: {
1.16 stevesk 81: size_t len;
1.12 deraadt 82: char *cp;
1.2 deraadt 83:
1.16 stevesk 84: len = strlen(str) + 1;
1.12 deraadt 85: cp = xmalloc(len);
1.4 markus 86: strlcpy(cp, str, len);
87: return cp;
1.18 djm 88: }
89:
90: int
91: xasprintf(char **ret, const char *fmt, ...)
92: {
93: va_list ap;
94: int i;
95:
96: va_start(ap, fmt);
97: i = vasprintf(ret, fmt, ap);
98: va_end(ap);
99:
100: if (i < 0 || *ret == NULL)
101: fatal("xasprintf: could not allocate memory");
102:
103: return (i);
1.1 deraadt 104: }