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