001
2022-02-16
op
/* $OpenBSD: xmalloc.c,v 1.4 2019/06/28 05:44:09 deraadt Exp $ */
003
2022-02-16
op
* Author: Tatu Ylonen <ylo@cs.hut.fi>
004
2022-02-16
op
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
005
2022-02-16
op
* All rights reserved
006
2022-02-16
op
* Versions of malloc and friends that check their results, and never return
007
2022-02-16
op
* failure (they call fatal if they encounter an error).
009
2022-02-16
op
* As far as I am concerned, the code I have written for this software
010
2022-02-16
op
* can be used freely for any purpose. Any derived versions of this
011
2022-02-16
op
* software must be clearly marked as such, and if the derived work is
012
2022-02-16
op
* incompatible with the protocol description in the RFC file, it must be
013
2022-02-16
op
* called by a name other than "ssh" or "Secure Shell".
016
2022-02-16
op
#include <stdarg.h>
017
2022-02-16
op
#include <stdint.h>
018
2022-02-16
op
#include <stdio.h>
019
2022-02-16
op
#include <stdlib.h>
020
2022-02-16
op
#include <string.h>
021
2022-02-16
op
#include <syslog.h>
023
2022-02-16
op
#include "log.h"
024
2022-02-16
op
#include "xmalloc.h"
027
2022-02-16
op
xmalloc(size_t size)
029
2022-02-16
op
void *ptr;
031
2022-02-16
op
if (size == 0)
032
2022-02-16
op
fatal("xmalloc: zero size");
033
2022-02-16
op
ptr = malloc(size);
034
2022-02-16
op
if (ptr == NULL)
035
2022-02-16
op
fatal("xmalloc: allocating %zu bytes", size);
036
2022-02-16
op
return ptr;
040
2022-02-16
op
xcalloc(size_t nmemb, size_t size)
042
2022-02-16
op
void *ptr;
044
2022-02-16
op
if (size == 0 || nmemb == 0)
045
2022-02-16
op
fatal("xcalloc: zero size");
046
2022-02-16
op
ptr = calloc(nmemb, size);
047
2022-02-16
op
if (ptr == NULL)
048
2022-02-16
op
fatal("xcalloc: allocating %zu * %zu bytes", nmemb, size);
049
2022-02-16
op
return ptr;
053
2022-02-16
op
xreallocarray(void *ptr, size_t nmemb, size_t size)
055
2022-02-16
op
void *new_ptr;
057
2022-02-16
op
new_ptr = reallocarray(ptr, nmemb, size);
058
2022-02-16
op
if (new_ptr == NULL)
059
2022-02-16
op
fatal("xreallocarray: allocating %zu * %zu bytes",
060
2022-02-16
op
nmemb, size);
061
2022-02-16
op
return new_ptr;
065
2022-02-16
op
xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
067
2022-02-16
op
void *new_ptr;
069
2022-02-16
op
new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
070
2022-02-16
op
if (new_ptr == NULL)
071
2022-02-16
op
fatal("xrecallocarray: allocating %zu * %zu bytes",
072
2022-02-16
op
nmemb, size);
073
2022-02-16
op
return new_ptr;
077
2022-02-16
op
xstrdup(const char *str)
079
2022-02-16
op
char *cp;
081
2022-02-16
op
if ((cp = strdup(str)) == NULL)
082
2022-02-16
op
fatal("xstrdup");
083
2022-02-16
op
return cp;
087
2022-02-16
op
xasprintf(char **ret, const char *fmt, ...)
089
2022-02-16
op
va_list ap;
092
2022-02-16
op
va_start(ap, fmt);
093
2022-02-16
op
i = vasprintf(ret, fmt, ap);
094
2022-02-16
op
va_end(ap);
096
2022-02-16
op
if (i == -1)
097
2022-02-16
op
fatal("xasprintf");
099
2022-02-16
op
return i;