]>
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" | |
764291b3 | 16 | RCSID("$OpenBSD: xmalloc.c,v 1.15 2001/04/16 08:05:34 deraadt Exp $"); |
8efc0c15 | 17 | |
42f11eb2 | 18 | #include "xmalloc.h" |
19 | #include "log.h" | |
8efc0c15 | 20 | |
5260325f | 21 | void * |
22 | xmalloc(size_t size) | |
8efc0c15 | 23 | { |
a2e6d17d | 24 | void *ptr; |
25 | ||
26 | if (size == 0) | |
27 | fatal("xmalloc: zero size"); | |
28 | ptr = malloc(size); | |
5260325f | 29 | if (ptr == NULL) |
a2e6d17d | 30 | fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); |
5260325f | 31 | return ptr; |
8efc0c15 | 32 | } |
33 | ||
5260325f | 34 | void * |
35 | xrealloc(void *ptr, size_t new_size) | |
8efc0c15 | 36 | { |
5260325f | 37 | void *new_ptr; |
38 | ||
a2e6d17d | 39 | if (new_size == 0) |
40 | fatal("xrealloc: zero size"); | |
5260325f | 41 | if (ptr == NULL) |
764291b3 | 42 | new_ptr = malloc(new_size); |
43 | else | |
44 | new_ptr = realloc(ptr, new_size); | |
5260325f | 45 | if (new_ptr == NULL) |
a2e6d17d | 46 | fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); |
5260325f | 47 | return new_ptr; |
8efc0c15 | 48 | } |
49 | ||
6ae2364d | 50 | void |
5260325f | 51 | xfree(void *ptr) |
8efc0c15 | 52 | { |
5260325f | 53 | if (ptr == NULL) |
54 | fatal("xfree: NULL pointer given as argument"); | |
55 | free(ptr); | |
8efc0c15 | 56 | } |
57 | ||
5260325f | 58 | char * |
59 | xstrdup(const char *str) | |
8efc0c15 | 60 | { |
95f4ccfb | 61 | size_t len = strlen(str) + 1; |
a2e6d17d | 62 | char *cp; |
8efc0c15 | 63 | |
a2e6d17d | 64 | if (len == 0) |
65 | fatal("xstrdup: zero size"); | |
66 | cp = xmalloc(len); | |
5260325f | 67 | strlcpy(cp, str, len); |
68 | return cp; | |
8efc0c15 | 69 | } |