]>
Commit | Line | Data |
---|---|---|
00146caa | 1 | /* $OpenBSD: xmalloc.c,v 1.23 2006/07/22 20:48:23 stevesk Exp $ */ |
8efc0c15 | 2 | /* |
5260325f | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | |
5 | * All rights reserved | |
5260325f | 6 | * Versions of malloc and friends that check their results, and never return |
7 | * failure (they call fatal if they encounter an error). | |
2b87da3b | 8 | * |
bcbf86ec | 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". | |
5260325f | 14 | */ |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
8efc0c15 | 17 | |
4c72fcfd | 18 | #include <stdarg.h> |
00146caa | 19 | #include <string.h> |
4c72fcfd | 20 | |
42f11eb2 | 21 | #include "xmalloc.h" |
22 | #include "log.h" | |
8efc0c15 | 23 | |
5260325f | 24 | void * |
25 | xmalloc(size_t size) | |
8efc0c15 | 26 | { |
a2e6d17d | 27 | void *ptr; |
28 | ||
29 | if (size == 0) | |
30 | fatal("xmalloc: zero size"); | |
31 | ptr = malloc(size); | |
5260325f | 32 | if (ptr == NULL) |
a2e6d17d | 33 | fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); |
5260325f | 34 | return ptr; |
8efc0c15 | 35 | } |
36 | ||
52e3daed | 37 | void * |
38 | xcalloc(size_t nmemb, size_t size) | |
39 | { | |
40 | void *ptr; | |
41 | ||
52e3daed | 42 | if (size == 0 || nmemb == 0) |
43 | fatal("xcalloc: zero size"); | |
6a0984b4 | 44 | if (SIZE_T_MAX / nmemb < size) |
45 | fatal("xcalloc: nmemb * size > SIZE_T_MAX"); | |
52e3daed | 46 | ptr = calloc(nmemb, size); |
47 | if (ptr == NULL) | |
48 | fatal("xcalloc: out of memory (allocating %lu bytes)", | |
49 | (u_long)(size * nmemb)); | |
50 | return ptr; | |
51 | } | |
52 | ||
5260325f | 53 | void * |
c5d10563 | 54 | xrealloc(void *ptr, size_t nmemb, size_t size) |
8efc0c15 | 55 | { |
5260325f | 56 | void *new_ptr; |
c5d10563 | 57 | size_t new_size = nmemb * size; |
5260325f | 58 | |
a2e6d17d | 59 | if (new_size == 0) |
60 | fatal("xrealloc: zero size"); | |
6a0984b4 | 61 | if (SIZE_T_MAX / nmemb < size) |
62 | fatal("xrealloc: nmemb * size > SIZE_T_MAX"); | |
5260325f | 63 | if (ptr == NULL) |
764291b3 | 64 | new_ptr = malloc(new_size); |
65 | else | |
66 | new_ptr = realloc(ptr, new_size); | |
5260325f | 67 | if (new_ptr == NULL) |
c5d10563 | 68 | fatal("xrealloc: out of memory (new_size %lu bytes)", |
69 | (u_long) new_size); | |
5260325f | 70 | return new_ptr; |
8efc0c15 | 71 | } |
72 | ||
6ae2364d | 73 | void |
5260325f | 74 | xfree(void *ptr) |
8efc0c15 | 75 | { |
5260325f | 76 | if (ptr == NULL) |
77 | fatal("xfree: NULL pointer given as argument"); | |
78 | free(ptr); | |
8efc0c15 | 79 | } |
80 | ||
5260325f | 81 | char * |
82 | xstrdup(const char *str) | |
8efc0c15 | 83 | { |
bac2ef55 | 84 | size_t len; |
a2e6d17d | 85 | char *cp; |
8efc0c15 | 86 | |
bac2ef55 | 87 | len = strlen(str) + 1; |
a2e6d17d | 88 | cp = xmalloc(len); |
5260325f | 89 | strlcpy(cp, str, len); |
90 | return cp; | |
8efc0c15 | 91 | } |
52e3daed | 92 | |
93 | int | |
94 | xasprintf(char **ret, const char *fmt, ...) | |
95 | { | |
96 | va_list ap; | |
97 | int i; | |
98 | ||
99 | va_start(ap, fmt); | |
100 | i = vasprintf(ret, fmt, ap); | |
101 | va_end(ap); | |
102 | ||
103 | if (i < 0 || *ret == NULL) | |
104 | fatal("xasprintf: could not allocate memory"); | |
105 | ||
106 | return (i); | |
107 | } |