]> andersk Git - openssh.git/blob - xmalloc.c
- djm@cvs.openbsd.org 2006/03/25 01:13:23
[openssh.git] / xmalloc.c
1 /*
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).
7  *
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".
13  */
14
15 #include "includes.h"
16
17 #include "xmalloc.h"
18 #include "log.h"
19
20 void *
21 xmalloc(size_t size)
22 {
23         void *ptr;
24
25         if (size == 0)
26                 fatal("xmalloc: zero size");
27         ptr = malloc(size);
28         if (ptr == NULL)
29                 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
30         return ptr;
31 }
32
33 void *
34 xcalloc(size_t nmemb, size_t size)
35 {
36         void *ptr;
37
38         if (nmemb && size && SIZE_T_MAX / nmemb < size)
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 *
50 xrealloc(void *ptr, size_t nmemb, size_t size)
51 {
52         void *new_ptr;
53         size_t new_size = nmemb * size;
54
55         if (nmemb && size && SIZE_T_MAX / nmemb < size)
56                 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
57         if (new_size == 0)
58                 fatal("xrealloc: zero size");
59         if (ptr == NULL)
60                 new_ptr = malloc(new_size);
61         else
62                 new_ptr = realloc(ptr, new_size);
63         if (new_ptr == NULL)
64                 fatal("xrealloc: out of memory (new_size %lu bytes)",
65                     (u_long) new_size);
66         return new_ptr;
67 }
68
69 void
70 xfree(void *ptr)
71 {
72         if (ptr == NULL)
73                 fatal("xfree: NULL pointer given as argument");
74         free(ptr);
75 }
76
77 char *
78 xstrdup(const char *str)
79 {
80         size_t len;
81         char *cp;
82
83         len = strlen(str) + 1;
84         cp = xmalloc(len);
85         strlcpy(cp, str, len);
86         return cp;
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);
103 }
This page took 0.043188 seconds and 5 git commands to generate.