xmalloc.c revision 65668
157429Smarkm/* 257429Smarkm * Author: Tatu Ylonen <ylo@cs.hut.fi> 357429Smarkm * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 457429Smarkm * All rights reserved 557429Smarkm * Versions of malloc and friends that check their results, and never return 657429Smarkm * failure (they call fatal if they encounter an error). 765668Skris * 865668Skris * As far as I am concerned, the code I have written for this software 965668Skris * can be used freely for any purpose. Any derived versions of this 1065668Skris * software must be clearly marked as such, and if the derived work is 1165668Skris * incompatible with the protocol description in the RFC file, it must be 1265668Skris * called by a name other than "ssh" or "Secure Shell". 1357429Smarkm */ 1457429Smarkm 1557429Smarkm#include "includes.h" 1665668SkrisRCSID("$OpenBSD: xmalloc.c,v 1.8 2000/09/07 20:27:55 deraadt Exp $"); 1757429Smarkm 1857429Smarkm#include "ssh.h" 1957429Smarkm 2057429Smarkmvoid * 2157429Smarkmxmalloc(size_t size) 2257429Smarkm{ 2357429Smarkm void *ptr = malloc(size); 2457429Smarkm if (ptr == NULL) 2557429Smarkm fatal("xmalloc: out of memory (allocating %d bytes)", (int) size); 2657429Smarkm return ptr; 2757429Smarkm} 2857429Smarkm 2957429Smarkmvoid * 3057429Smarkmxrealloc(void *ptr, size_t new_size) 3157429Smarkm{ 3257429Smarkm void *new_ptr; 3357429Smarkm 3457429Smarkm if (ptr == NULL) 3557429Smarkm fatal("xrealloc: NULL pointer given as argument"); 3657429Smarkm new_ptr = realloc(ptr, new_size); 3757429Smarkm if (new_ptr == NULL) 3857429Smarkm fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size); 3957429Smarkm return new_ptr; 4057429Smarkm} 4157429Smarkm 4260573Skrisvoid 4357429Smarkmxfree(void *ptr) 4457429Smarkm{ 4557429Smarkm if (ptr == NULL) 4657429Smarkm fatal("xfree: NULL pointer given as argument"); 4757429Smarkm free(ptr); 4857429Smarkm} 4957429Smarkm 5057429Smarkmchar * 5157429Smarkmxstrdup(const char *str) 5257429Smarkm{ 5357429Smarkm int len = strlen(str) + 1; 5457429Smarkm 5557429Smarkm char *cp = xmalloc(len); 5657429Smarkm strlcpy(cp, str, len); 5757429Smarkm return cp; 5857429Smarkm} 59