]>
Commit | Line | Data |
---|---|---|
cf851879 | 1 | /* $OpenBSD: xmalloc.c,v 1.26 2006/08/01 23:22:48 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> |
cf851879 | 21 | #include <stdio.h> |
ffa517a8 | 22 | #include <stdlib.h> |
00146caa | 23 | #include <string.h> |
4c72fcfd | 24 | |
42f11eb2 | 25 | #include "xmalloc.h" |
26 | #include "log.h" | |
8efc0c15 | 27 | |
5260325f | 28 | void * |
29 | xmalloc(size_t size) | |
8efc0c15 | 30 | { |
a2e6d17d | 31 | void *ptr; |
32 | ||
33 | if (size == 0) | |
34 | fatal("xmalloc: zero size"); | |
35 | ptr = malloc(size); | |
5260325f | 36 | if (ptr == NULL) |
a2e6d17d | 37 | fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); |
5260325f | 38 | return ptr; |
8efc0c15 | 39 | } |
40 | ||
52e3daed | 41 | void * |
42 | xcalloc(size_t nmemb, size_t size) | |
43 | { | |
44 | void *ptr; | |
45 | ||
52e3daed | 46 | if (size == 0 || nmemb == 0) |
47 | fatal("xcalloc: zero size"); | |
6a0984b4 | 48 | if (SIZE_T_MAX / nmemb < size) |
49 | fatal("xcalloc: nmemb * size > SIZE_T_MAX"); | |
52e3daed | 50 | ptr = calloc(nmemb, size); |
51 | if (ptr == NULL) | |
52 | fatal("xcalloc: out of memory (allocating %lu bytes)", | |
53 | (u_long)(size * nmemb)); | |
54 | return ptr; | |
55 | } | |
56 | ||
5260325f | 57 | void * |
c5d10563 | 58 | xrealloc(void *ptr, size_t nmemb, size_t size) |
8efc0c15 | 59 | { |
5260325f | 60 | void *new_ptr; |
c5d10563 | 61 | size_t new_size = nmemb * size; |
5260325f | 62 | |
a2e6d17d | 63 | if (new_size == 0) |
64 | fatal("xrealloc: zero size"); | |
6a0984b4 | 65 | if (SIZE_T_MAX / nmemb < size) |
66 | fatal("xrealloc: nmemb * size > SIZE_T_MAX"); | |
5260325f | 67 | if (ptr == NULL) |
764291b3 | 68 | new_ptr = malloc(new_size); |
69 | else | |
70 | new_ptr = realloc(ptr, new_size); | |
5260325f | 71 | if (new_ptr == NULL) |
c5d10563 | 72 | fatal("xrealloc: out of memory (new_size %lu bytes)", |
73 | (u_long) new_size); | |
5260325f | 74 | return new_ptr; |
8efc0c15 | 75 | } |
76 | ||
6ae2364d | 77 | void |
5260325f | 78 | xfree(void *ptr) |
8efc0c15 | 79 | { |
5260325f | 80 | if (ptr == NULL) |
81 | fatal("xfree: NULL pointer given as argument"); | |
82 | free(ptr); | |
8efc0c15 | 83 | } |
84 | ||
5260325f | 85 | char * |
86 | xstrdup(const char *str) | |
8efc0c15 | 87 | { |
bac2ef55 | 88 | size_t len; |
a2e6d17d | 89 | char *cp; |
8efc0c15 | 90 | |
bac2ef55 | 91 | len = strlen(str) + 1; |
a2e6d17d | 92 | cp = xmalloc(len); |
5260325f | 93 | strlcpy(cp, str, len); |
94 | return cp; | |
8efc0c15 | 95 | } |
52e3daed | 96 | |
97 | int | |
98 | xasprintf(char **ret, const char *fmt, ...) | |
99 | { | |
100 | va_list ap; | |
101 | int i; | |
102 | ||
103 | va_start(ap, fmt); | |
104 | i = vasprintf(ret, fmt, ap); | |
105 | va_end(ap); | |
106 | ||
107 | if (i < 0 || *ret == NULL) | |
108 | fatal("xasprintf: could not allocate memory"); | |
109 | ||
110 | return (i); | |
111 | } |