]>
Commit | Line | Data |
---|---|---|
536c14e8 | 1 | /* $OpenBSD: xmalloc.c,v 1.24 2006/07/26 02:35:17 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 | |
536c14e8 | 18 | #include <sys/param.h> |
19 | ||
4c72fcfd | 20 | #include <stdarg.h> |
00146caa | 21 | #include <string.h> |
4c72fcfd | 22 | |
42f11eb2 | 23 | #include "xmalloc.h" |
24 | #include "log.h" | |
8efc0c15 | 25 | |
5260325f | 26 | void * |
27 | xmalloc(size_t size) | |
8efc0c15 | 28 | { |
a2e6d17d | 29 | void *ptr; |
30 | ||
31 | if (size == 0) | |
32 | fatal("xmalloc: zero size"); | |
33 | ptr = malloc(size); | |
5260325f | 34 | if (ptr == NULL) |
a2e6d17d | 35 | fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); |
5260325f | 36 | return ptr; |
8efc0c15 | 37 | } |
38 | ||
52e3daed | 39 | void * |
40 | xcalloc(size_t nmemb, size_t size) | |
41 | { | |
42 | void *ptr; | |
43 | ||
52e3daed | 44 | if (size == 0 || nmemb == 0) |
45 | fatal("xcalloc: zero size"); | |
6a0984b4 | 46 | if (SIZE_T_MAX / nmemb < size) |
47 | fatal("xcalloc: nmemb * size > SIZE_T_MAX"); | |
52e3daed | 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 | ||
5260325f | 55 | void * |
c5d10563 | 56 | xrealloc(void *ptr, size_t nmemb, size_t size) |
8efc0c15 | 57 | { |
5260325f | 58 | void *new_ptr; |
c5d10563 | 59 | size_t new_size = nmemb * size; |
5260325f | 60 | |
a2e6d17d | 61 | if (new_size == 0) |
62 | fatal("xrealloc: zero size"); | |
6a0984b4 | 63 | if (SIZE_T_MAX / nmemb < size) |
64 | fatal("xrealloc: nmemb * size > SIZE_T_MAX"); | |
5260325f | 65 | if (ptr == NULL) |
764291b3 | 66 | new_ptr = malloc(new_size); |
67 | else | |
68 | new_ptr = realloc(ptr, new_size); | |
5260325f | 69 | if (new_ptr == NULL) |
c5d10563 | 70 | fatal("xrealloc: out of memory (new_size %lu bytes)", |
71 | (u_long) new_size); | |
5260325f | 72 | return new_ptr; |
8efc0c15 | 73 | } |
74 | ||
6ae2364d | 75 | void |
5260325f | 76 | xfree(void *ptr) |
8efc0c15 | 77 | { |
5260325f | 78 | if (ptr == NULL) |
79 | fatal("xfree: NULL pointer given as argument"); | |
80 | free(ptr); | |
8efc0c15 | 81 | } |
82 | ||
5260325f | 83 | char * |
84 | xstrdup(const char *str) | |
8efc0c15 | 85 | { |
bac2ef55 | 86 | size_t len; |
a2e6d17d | 87 | char *cp; |
8efc0c15 | 88 | |
bac2ef55 | 89 | len = strlen(str) + 1; |
a2e6d17d | 90 | cp = xmalloc(len); |
5260325f | 91 | strlcpy(cp, str, len); |
92 | return cp; | |
8efc0c15 | 93 | } |
52e3daed | 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); | |
109 | } |