Annotation of src/usr.bin/ssh/xmalloc.c, Revision 1.19
1.1 deraadt 1: /*
1.5 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
5: * Versions of malloc and friends that check their results, and never return
6: * failure (they call fatal if they encounter an error).
1.11 stevesk 7: *
1.8 deraadt 8: * As far as I am concerned, the code I have written for this software
9: * can be used freely for any purpose. Any derived versions of this
10: * software must be clearly marked as such, and if the derived work is
11: * incompatible with the protocol description in the RFC file, it must be
12: * called by a name other than "ssh" or "Secure Shell".
1.5 deraadt 13: */
1.1 deraadt 14:
15: #include "includes.h"
16:
1.9 markus 17: #include "xmalloc.h"
18: #include "log.h"
1.1 deraadt 19:
1.4 markus 20: void *
21: xmalloc(size_t size)
1.1 deraadt 22: {
1.12 deraadt 23: void *ptr;
24:
25: if (size == 0)
26: fatal("xmalloc: zero size");
27: ptr = malloc(size);
1.4 markus 28: if (ptr == NULL)
1.14 itojun 29: fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
1.4 markus 30: return ptr;
1.1 deraadt 31: }
32:
1.4 markus 33: void *
1.18 djm 34: xcalloc(size_t nmemb, size_t size)
35: {
36: void *ptr;
37:
1.19 ! djm 38: if (nmemb && size && SIZE_T_MAX / nmemb < size)
1.18 djm 39: fatal("xcalloc: nmemb * size > SIZE_T_MAX");
40: if (size == 0 || nmemb == 0)
41: fatal("xcalloc: zero size");
42: ptr = calloc(nmemb, size);
43: if (ptr == NULL)
44: fatal("xcalloc: out of memory (allocating %lu bytes)",
45: (u_long)(size * nmemb));
46: return ptr;
47: }
48:
49: void *
1.19 ! djm 50: xrealloc(void *ptr, size_t nmemb, size_t size)
1.1 deraadt 51: {
1.4 markus 52: void *new_ptr;
1.19 ! djm 53: size_t new_size = nmemb * size;
1.1 deraadt 54:
1.19 ! djm 55: if (nmemb && size && SIZE_T_MAX / nmemb < size)
! 56: fatal("xrealloc: nmemb * size > SIZE_T_MAX");
1.12 deraadt 57: if (new_size == 0)
1.13 markus 58: fatal("xrealloc: zero size");
1.4 markus 59: if (ptr == NULL)
1.15 deraadt 60: new_ptr = malloc(new_size);
61: else
62: new_ptr = realloc(ptr, new_size);
1.4 markus 63: if (new_ptr == NULL)
1.19 ! djm 64: fatal("xrealloc: out of memory (new_size %lu bytes)",
! 65: (u_long) new_size);
1.4 markus 66: return new_ptr;
1.1 deraadt 67: }
68:
1.6 markus 69: void
1.4 markus 70: xfree(void *ptr)
1.1 deraadt 71: {
1.4 markus 72: if (ptr == NULL)
73: fatal("xfree: NULL pointer given as argument");
74: free(ptr);
1.1 deraadt 75: }
76:
1.4 markus 77: char *
78: xstrdup(const char *str)
1.1 deraadt 79: {
1.16 stevesk 80: size_t len;
1.12 deraadt 81: char *cp;
1.2 deraadt 82:
1.16 stevesk 83: len = strlen(str) + 1;
1.12 deraadt 84: cp = xmalloc(len);
1.4 markus 85: strlcpy(cp, str, len);
86: return cp;
1.18 djm 87: }
88:
89: int
90: xasprintf(char **ret, const char *fmt, ...)
91: {
92: va_list ap;
93: int i;
94:
95: va_start(ap, fmt);
96: i = vasprintf(ret, fmt, ap);
97: va_end(ap);
98:
99: if (i < 0 || *ret == NULL)
100: fatal("xasprintf: could not allocate memory");
101:
102: return (i);
1.1 deraadt 103: }