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