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