]> andersk Git - openssh.git/blame - xmalloc.c
- deraadt@cvs.openbsd.org 2006/03/26 01:31:48
[openssh.git] / xmalloc.c
CommitLineData
c1cb7bae 1/* $OpenBSD: xmalloc.c,v 1.20 2006/03/25 13:17:03 djm 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
42f11eb2 18#include "xmalloc.h"
19#include "log.h"
8efc0c15 20
5260325f 21void *
22xmalloc(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
52e3daed 34void *
35xcalloc(size_t nmemb, size_t size)
36{
37 void *ptr;
38
c5d10563 39 if (nmemb && size && SIZE_T_MAX / nmemb < size)
52e3daed 40 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
41 if (size == 0 || nmemb == 0)
42 fatal("xcalloc: zero size");
43 ptr = calloc(nmemb, size);
44 if (ptr == NULL)
45 fatal("xcalloc: out of memory (allocating %lu bytes)",
46 (u_long)(size * nmemb));
47 return ptr;
48}
49
5260325f 50void *
c5d10563 51xrealloc(void *ptr, size_t nmemb, size_t size)
8efc0c15 52{
5260325f 53 void *new_ptr;
c5d10563 54 size_t new_size = nmemb * size;
5260325f 55
c5d10563 56 if (nmemb && size && SIZE_T_MAX / nmemb < size)
57 fatal("xrealloc: nmemb * size > SIZE_T_MAX");
a2e6d17d 58 if (new_size == 0)
59 fatal("xrealloc: zero size");
5260325f 60 if (ptr == NULL)
764291b3 61 new_ptr = malloc(new_size);
62 else
63 new_ptr = realloc(ptr, new_size);
5260325f 64 if (new_ptr == NULL)
c5d10563 65 fatal("xrealloc: out of memory (new_size %lu bytes)",
66 (u_long) new_size);
5260325f 67 return new_ptr;
8efc0c15 68}
69
6ae2364d 70void
5260325f 71xfree(void *ptr)
8efc0c15 72{
5260325f 73 if (ptr == NULL)
74 fatal("xfree: NULL pointer given as argument");
75 free(ptr);
8efc0c15 76}
77
5260325f 78char *
79xstrdup(const char *str)
8efc0c15 80{
bac2ef55 81 size_t len;
a2e6d17d 82 char *cp;
8efc0c15 83
bac2ef55 84 len = strlen(str) + 1;
a2e6d17d 85 cp = xmalloc(len);
5260325f 86 strlcpy(cp, str, len);
87 return cp;
8efc0c15 88}
52e3daed 89
90int
91xasprintf(char **ret, const char *fmt, ...)
92{
93 va_list ap;
94 int i;
95
96 va_start(ap, fmt);
97 i = vasprintf(ret, fmt, ap);
98 va_end(ap);
99
100 if (i < 0 || *ret == NULL)
101 fatal("xasprintf: could not allocate memory");
102
103 return (i);
104}
This page took 0.27064 seconds and 5 git commands to generate.