17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate * All rights reserved
57c478bd9Sstevel@tonic-gate * Versions of malloc and friends that check their results, and never return
67c478bd9Sstevel@tonic-gate * failure (they call fatal if they encounter an error).
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
97c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
107c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
117c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
127c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include "includes.h"
167c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp $");
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
197c478bd9Sstevel@tonic-gate
207c478bd9Sstevel@tonic-gate #include "xmalloc.h"
217c478bd9Sstevel@tonic-gate #include "log.h"
227c478bd9Sstevel@tonic-gate
237c478bd9Sstevel@tonic-gate void *
xmalloc(size_t size)247c478bd9Sstevel@tonic-gate xmalloc(size_t size)
257c478bd9Sstevel@tonic-gate {
267c478bd9Sstevel@tonic-gate void *ptr;
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate if (size == 0)
297c478bd9Sstevel@tonic-gate fatal("xmalloc: zero size");
307c478bd9Sstevel@tonic-gate ptr = malloc(size);
317c478bd9Sstevel@tonic-gate if (ptr == NULL)
327c478bd9Sstevel@tonic-gate fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
337c478bd9Sstevel@tonic-gate return ptr;
347c478bd9Sstevel@tonic-gate }
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate void *
xcalloc(size_t nmemb,size_t size)37*47b374ffSjp161948 xcalloc(size_t nmemb, size_t size)
38*47b374ffSjp161948 {
39*47b374ffSjp161948 void *ptr;
40*47b374ffSjp161948
41*47b374ffSjp161948 if (size == 0 || nmemb == 0)
42*47b374ffSjp161948 fatal("xcalloc: zero size");
43*47b374ffSjp161948 if (SIZE_T_MAX / nmemb < size)
44*47b374ffSjp161948 fatal("xcalloc: nmemb * size > SIZE_T_MAX");
45*47b374ffSjp161948 ptr = calloc(nmemb, size);
46*47b374ffSjp161948 if (ptr == NULL)
47*47b374ffSjp161948 fatal("xcalloc: out of memory (allocating %lu bytes)",
48*47b374ffSjp161948 (u_long)(size * nmemb));
49*47b374ffSjp161948 return ptr;
50*47b374ffSjp161948 }
51*47b374ffSjp161948
52*47b374ffSjp161948 void *
xrealloc(void * ptr,size_t new_size)537c478bd9Sstevel@tonic-gate xrealloc(void *ptr, size_t new_size)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate void *new_ptr;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate if (new_size == 0)
587c478bd9Sstevel@tonic-gate fatal("xrealloc: zero size");
597c478bd9Sstevel@tonic-gate if (ptr == NULL)
607c478bd9Sstevel@tonic-gate new_ptr = malloc(new_size);
617c478bd9Sstevel@tonic-gate else
627c478bd9Sstevel@tonic-gate new_ptr = realloc(ptr, new_size);
637c478bd9Sstevel@tonic-gate if (new_ptr == NULL)
647c478bd9Sstevel@tonic-gate fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size);
657c478bd9Sstevel@tonic-gate return new_ptr;
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate void
xfree(void * ptr)697c478bd9Sstevel@tonic-gate xfree(void *ptr)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate if (ptr == NULL)
727c478bd9Sstevel@tonic-gate fatal("xfree: NULL pointer given as argument");
737c478bd9Sstevel@tonic-gate free(ptr);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate char *
xstrdup(const char * str)777c478bd9Sstevel@tonic-gate xstrdup(const char *str)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate size_t len;
807c478bd9Sstevel@tonic-gate char *cp;
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate len = strlen(str) + 1;
837c478bd9Sstevel@tonic-gate cp = xmalloc(len);
847c478bd9Sstevel@tonic-gate strlcpy(cp, str, len);
857c478bd9Sstevel@tonic-gate return cp;
867c478bd9Sstevel@tonic-gate }
87