]>
Commit | Line | Data |
---|---|---|
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 | 20 | void * |
21 | xmalloc(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 | 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 | ||
5260325f | 49 | void * |
50 | xrealloc(void *ptr, size_t new_size) | |
8efc0c15 | 51 | { |
5260325f | 52 | void *new_ptr; |
53 | ||
a2e6d17d | 54 | if (new_size == 0) |
55 | fatal("xrealloc: zero size"); | |
5260325f | 56 | if (ptr == NULL) |
764291b3 | 57 | new_ptr = malloc(new_size); |
58 | else | |
59 | new_ptr = realloc(ptr, new_size); | |
5260325f | 60 | if (new_ptr == NULL) |
a2e6d17d | 61 | fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); |
5260325f | 62 | return new_ptr; |
8efc0c15 | 63 | } |
64 | ||
6ae2364d | 65 | void |
5260325f | 66 | xfree(void *ptr) |
8efc0c15 | 67 | { |
5260325f | 68 | if (ptr == NULL) |
69 | fatal("xfree: NULL pointer given as argument"); | |
70 | free(ptr); | |
8efc0c15 | 71 | } |
72 | ||
5260325f | 73 | char * |
74 | xstrdup(const char *str) | |
8efc0c15 | 75 | { |
bac2ef55 | 76 | size_t len; |
a2e6d17d | 77 | char *cp; |
8efc0c15 | 78 | |
bac2ef55 | 79 | len = strlen(str) + 1; |
a2e6d17d | 80 | cp = xmalloc(len); |
5260325f | 81 | strlcpy(cp, str, len); |
82 | return cp; | |
8efc0c15 | 83 | } |
52e3daed | 84 | |
85 | int | |
86 | xasprintf(char **ret, const char *fmt, ...) | |
87 | { | |
88 | va_list ap; | |
89 | int i; | |
90 | ||
91 | va_start(ap, fmt); | |
92 | i = vasprintf(ret, fmt, ap); | |
93 | va_end(ap); | |
94 | ||
95 | if (i < 0 || *ret == NULL) | |
96 | fatal("xasprintf: could not allocate memory"); | |
97 | ||
98 | return (i); | |
99 | } |