]> andersk Git - openssh.git/blame - xmalloc.c
- djm@cvs.openbsd.org 2006/03/25 01:30:23
[openssh.git] / xmalloc.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Versions of malloc and friends that check their results, and never return
6 * failure (they call fatal if they encounter an error).
2b87da3b 7 *
bcbf86ec 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".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
8efc0c15 16
42f11eb2 17#include "xmalloc.h"
18#include "log.h"
8efc0c15 19
5260325f 20void *
21xmalloc(size_t size)
8efc0c15 22{
a2e6d17d 23 void *ptr;
24
25 if (size == 0)
26 fatal("xmalloc: zero size");
27 ptr = malloc(size);
5260325f 28 if (ptr == NULL)
a2e6d17d 29 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
5260325f 30 return ptr;
8efc0c15 31}
32
52e3daed 33void *
34xcalloc(size_t nmemb, size_t size)
35{
36 void *ptr;
37
c5d10563 38 if (nmemb && size && SIZE_T_MAX / nmemb < size)
52e3daed 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
5260325f 49void *
c5d10563 50xrealloc(void *ptr, size_t nmemb, size_t size)
8efc0c15 51{
5260325f 52 void *new_ptr;
c5d10563 53 size_t new_size = nmemb * size;
5260325f 54
c5d10563 55 if (nmemb && size && SIZE_T_MAX / nmemb < size)
56 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
a2e6d17d 57 if (new_size == 0)
58 fatal("xrealloc: zero size");
5260325f 59 if (ptr == NULL)
764291b3 60 new_ptr = malloc(new_size);
61 else
62 new_ptr = realloc(ptr, new_size);
5260325f 63 if (new_ptr == NULL)
c5d10563 64 fatal("xrealloc: out of memory (new_size %lu bytes)",
65 (u_long) new_size);
5260325f 66 return new_ptr;
8efc0c15 67}
68
6ae2364d 69void
5260325f 70xfree(void *ptr)
8efc0c15 71{
5260325f 72 if (ptr == NULL)
73 fatal("xfree: NULL pointer given as argument");
74 free(ptr);
8efc0c15 75}
76
5260325f 77char *
78xstrdup(const char *str)
8efc0c15 79{
bac2ef55 80 size_t len;
a2e6d17d 81 char *cp;
8efc0c15 82
bac2ef55 83 len = strlen(str) + 1;
a2e6d17d 84 cp = xmalloc(len);
5260325f 85 strlcpy(cp, str, len);
86 return cp;
8efc0c15 87}
52e3daed 88
89int
90xasprintf(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);
103}
This page took 0.273294 seconds and 5 git commands to generate.