1262566Sdes/* $OpenBSD: xmalloc.c,v 1.29 2014/01/04 17:50:55 tedu Exp $ */ 257429Smarkm/* 357429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi> 457429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 557429Smarkm * All rights reserved 657429Smarkm * Versions of malloc and friends that check their results, and never return 757429Smarkm * failure (they call fatal if they encounter an error). 876259Sgreen * 965668Skris * As far as I am concerned, the code I have written for this software 1065668Skris * can be used freely for any purpose. Any derived versions of this 1165668Skris * software must be clearly marked as such, and if the derived work is 1265668Skris * incompatible with the protocol description in the RFC file, it must be 1365668Skris * called by a name other than "ssh" or "Secure Shell". 1457429Smarkm */ 1557429Smarkm 1657429Smarkm#include "includes.h" 1757429Smarkm 18162852Sdes#include <sys/param.h> 19162852Sdes#include <stdarg.h> 20162852Sdes#include <stdio.h> 21162852Sdes#include <stdlib.h> 22162852Sdes#include <string.h> 23162852Sdes 2476259Sgreen#include "xmalloc.h" 2576259Sgreen#include "log.h" 2657429Smarkm 2757429Smarkmvoid * 2857429Smarkmxmalloc(size_t size) 2957429Smarkm{ 3076259Sgreen void *ptr; 3176259Sgreen 3276259Sgreen if (size == 0) 3376259Sgreen fatal("xmalloc: zero size"); 3476259Sgreen ptr = malloc(size); 3557429Smarkm if (ptr == NULL) 36262566Sdes fatal("xmalloc: out of memory (allocating %zu bytes)", size); 3757429Smarkm return ptr; 3857429Smarkm} 3957429Smarkm 4057429Smarkmvoid * 41162852Sdesxcalloc(size_t nmemb, size_t size) 4257429Smarkm{ 43162852Sdes void *ptr; 44162852Sdes 45162852Sdes if (size == 0 || nmemb == 0) 46162852Sdes fatal("xcalloc: zero size"); 47162852Sdes if (SIZE_T_MAX / nmemb < size) 48162852Sdes fatal("xcalloc: nmemb * size > SIZE_T_MAX"); 49162852Sdes ptr = calloc(nmemb, size); 50162852Sdes if (ptr == NULL) 51262566Sdes fatal("xcalloc: out of memory (allocating %zu bytes)", 52262566Sdes size * nmemb); 53162852Sdes return ptr; 54162852Sdes} 55162852Sdes 56162852Sdesvoid * 57162852Sdesxrealloc(void *ptr, size_t nmemb, size_t size) 58162852Sdes{ 5957429Smarkm void *new_ptr; 60162852Sdes size_t new_size = nmemb * size; 6157429Smarkm 6276259Sgreen if (new_size == 0) 6376259Sgreen fatal("xrealloc: zero size"); 64162852Sdes if (SIZE_T_MAX / nmemb < size) 65162852Sdes fatal("xrealloc: nmemb * size > SIZE_T_MAX"); 6657429Smarkm if (ptr == NULL) 6776259Sgreen new_ptr = malloc(new_size); 6876259Sgreen else 6976259Sgreen new_ptr = realloc(ptr, new_size); 7057429Smarkm if (new_ptr == NULL) 71262566Sdes fatal("xrealloc: out of memory (new_size %zu bytes)", 72262566Sdes new_size); 7357429Smarkm return new_ptr; 7457429Smarkm} 7557429Smarkm 7657429Smarkmchar * 7757429Smarkmxstrdup(const char *str) 7857429Smarkm{ 7992555Sdes size_t len; 8076259Sgreen char *cp; 8157429Smarkm 8292555Sdes len = strlen(str) + 1; 8376259Sgreen cp = xmalloc(len); 8457429Smarkm strlcpy(cp, str, len); 8557429Smarkm return cp; 8657429Smarkm} 87162852Sdes 88162852Sdesint 89162852Sdesxasprintf(char **ret, const char *fmt, ...) 90162852Sdes{ 91162852Sdes va_list ap; 92162852Sdes int i; 93162852Sdes 94162852Sdes va_start(ap, fmt); 95162852Sdes i = vasprintf(ret, fmt, ap); 96162852Sdes va_end(ap); 97162852Sdes 98162852Sdes if (i < 0 || *ret == NULL) 99162852Sdes fatal("xasprintf: could not allocate memory"); 100162852Sdes 101162852Sdes return (i); 102162852Sdes} 103