1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi> 3*0Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4*0Sstevel@tonic-gate * All rights reserved 5*0Sstevel@tonic-gate * Versions of malloc and friends that check their results, and never return 6*0Sstevel@tonic-gate * failure (they call fatal if they encounter an error). 7*0Sstevel@tonic-gate * 8*0Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software 9*0Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this 10*0Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is 11*0Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be 12*0Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell". 13*0Sstevel@tonic-gate */ 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate #include "includes.h" 16*0Sstevel@tonic-gate RCSID("$OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp $"); 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gate #include "xmalloc.h" 21*0Sstevel@tonic-gate #include "log.h" 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate void * 24*0Sstevel@tonic-gate xmalloc(size_t size) 25*0Sstevel@tonic-gate { 26*0Sstevel@tonic-gate void *ptr; 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate if (size == 0) 29*0Sstevel@tonic-gate fatal("xmalloc: zero size"); 30*0Sstevel@tonic-gate ptr = malloc(size); 31*0Sstevel@tonic-gate if (ptr == NULL) 32*0Sstevel@tonic-gate fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); 33*0Sstevel@tonic-gate return ptr; 34*0Sstevel@tonic-gate } 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate void * 37*0Sstevel@tonic-gate xrealloc(void *ptr, size_t new_size) 38*0Sstevel@tonic-gate { 39*0Sstevel@tonic-gate void *new_ptr; 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate if (new_size == 0) 42*0Sstevel@tonic-gate fatal("xrealloc: zero size"); 43*0Sstevel@tonic-gate if (ptr == NULL) 44*0Sstevel@tonic-gate new_ptr = malloc(new_size); 45*0Sstevel@tonic-gate else 46*0Sstevel@tonic-gate new_ptr = realloc(ptr, new_size); 47*0Sstevel@tonic-gate if (new_ptr == NULL) 48*0Sstevel@tonic-gate fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); 49*0Sstevel@tonic-gate return new_ptr; 50*0Sstevel@tonic-gate } 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate void 53*0Sstevel@tonic-gate xfree(void *ptr) 54*0Sstevel@tonic-gate { 55*0Sstevel@tonic-gate if (ptr == NULL) 56*0Sstevel@tonic-gate fatal("xfree: NULL pointer given as argument"); 57*0Sstevel@tonic-gate free(ptr); 58*0Sstevel@tonic-gate } 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate char * 61*0Sstevel@tonic-gate xstrdup(const char *str) 62*0Sstevel@tonic-gate { 63*0Sstevel@tonic-gate size_t len; 64*0Sstevel@tonic-gate char *cp; 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate len = strlen(str) + 1; 67*0Sstevel@tonic-gate cp = xmalloc(len); 68*0Sstevel@tonic-gate strlcpy(cp, str, len); 69*0Sstevel@tonic-gate return cp; 70*0Sstevel@tonic-gate } 71