diff options
Diffstat (limited to 'other/ssharp/xmalloc.c')
| -rw-r--r-- | other/ssharp/xmalloc.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/other/ssharp/xmalloc.c b/other/ssharp/xmalloc.c new file mode 100644 index 0000000..5046627 --- /dev/null +++ b/other/ssharp/xmalloc.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | /* | ||
| 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | ||
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | ||
| 4 | * All rights reserved | ||
| 5 | * Versions of malloc and friends that check their results, and never return | ||
| 6 | * failure (they call fatal if they encounter an error). | ||
| 7 | * | ||
| 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". | ||
| 13 | */ | ||
| 14 | |||
| 15 | #include "includes.h" | ||
| 16 | RCSID("$OpenBSD: xmalloc.c,v 1.15 2001/04/16 08:05:34 deraadt Exp $"); | ||
| 17 | |||
| 18 | #include "xmalloc.h" | ||
| 19 | #include "log.h" | ||
| 20 | |||
| 21 | void * | ||
| 22 | xmalloc(size_t size) | ||
| 23 | { | ||
| 24 | void *ptr; | ||
| 25 | |||
| 26 | if (size == 0) | ||
| 27 | fatal("xmalloc: zero size"); | ||
| 28 | ptr = malloc(size); | ||
| 29 | if (ptr == NULL) | ||
| 30 | fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); | ||
| 31 | return ptr; | ||
| 32 | } | ||
| 33 | |||
| 34 | void * | ||
| 35 | xrealloc(void *ptr, size_t new_size) | ||
| 36 | { | ||
| 37 | void *new_ptr; | ||
| 38 | |||
| 39 | if (new_size == 0) | ||
| 40 | fatal("xrealloc: zero size"); | ||
| 41 | if (ptr == NULL) | ||
| 42 | new_ptr = malloc(new_size); | ||
| 43 | else | ||
| 44 | new_ptr = realloc(ptr, new_size); | ||
| 45 | if (new_ptr == NULL) | ||
| 46 | fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); | ||
| 47 | return new_ptr; | ||
| 48 | } | ||
| 49 | |||
| 50 | void | ||
| 51 | xfree(void *ptr) | ||
| 52 | { | ||
| 53 | if (ptr == NULL) | ||
| 54 | fatal("xfree: NULL pointer given as argument"); | ||
| 55 | free(ptr); | ||
| 56 | } | ||
| 57 | |||
| 58 | char * | ||
| 59 | xstrdup(const char *str) | ||
| 60 | { | ||
| 61 | size_t len = strlen(str) + 1; | ||
| 62 | char *cp; | ||
| 63 | |||
| 64 | if (len == 0) | ||
| 65 | fatal("xstrdup: zero size"); | ||
| 66 | cp = xmalloc(len); | ||
| 67 | strlcpy(cp, str, len); | ||
| 68 | return cp; | ||
| 69 | } | ||
