xref: /onnv-gate/usr/src/cmd/ssh/libssh/common/xmalloc.c (revision 3946:0cc6f51aa203)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate  *                    All rights reserved
50Sstevel@tonic-gate  * Versions of malloc and friends that check their results, and never return
60Sstevel@tonic-gate  * failure (they call fatal if they encounter an error).
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
90Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
100Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
110Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
120Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #include "includes.h"
160Sstevel@tonic-gate RCSID("$OpenBSD: xmalloc.c,v 1.16 2001/07/23 18:21:46 stevesk Exp $");
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
190Sstevel@tonic-gate 
200Sstevel@tonic-gate #include "xmalloc.h"
210Sstevel@tonic-gate #include "log.h"
220Sstevel@tonic-gate 
230Sstevel@tonic-gate void *
xmalloc(size_t size)240Sstevel@tonic-gate xmalloc(size_t size)
250Sstevel@tonic-gate {
260Sstevel@tonic-gate 	void *ptr;
270Sstevel@tonic-gate 
280Sstevel@tonic-gate 	if (size == 0)
290Sstevel@tonic-gate 		fatal("xmalloc: zero size");
300Sstevel@tonic-gate 	ptr = malloc(size);
310Sstevel@tonic-gate 	if (ptr == NULL)
320Sstevel@tonic-gate 		fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
330Sstevel@tonic-gate 	return ptr;
340Sstevel@tonic-gate }
350Sstevel@tonic-gate 
360Sstevel@tonic-gate void *
xcalloc(size_t nmemb,size_t size)37*3946Sjp161948 xcalloc(size_t nmemb, size_t size)
38*3946Sjp161948 {
39*3946Sjp161948 	void *ptr;
40*3946Sjp161948 
41*3946Sjp161948 	if (size == 0 || nmemb == 0)
42*3946Sjp161948 		fatal("xcalloc: zero size");
43*3946Sjp161948 	if (SIZE_T_MAX / nmemb < size)
44*3946Sjp161948 		fatal("xcalloc: nmemb * size > SIZE_T_MAX");
45*3946Sjp161948 	ptr = calloc(nmemb, size);
46*3946Sjp161948 	if (ptr == NULL)
47*3946Sjp161948 		fatal("xcalloc: out of memory (allocating %lu bytes)",
48*3946Sjp161948 		    (u_long)(size * nmemb));
49*3946Sjp161948 	return ptr;
50*3946Sjp161948 }
51*3946Sjp161948 
52*3946Sjp161948 void *
xrealloc(void * ptr,size_t new_size)530Sstevel@tonic-gate xrealloc(void *ptr, size_t new_size)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	void *new_ptr;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	if (new_size == 0)
580Sstevel@tonic-gate 		fatal("xrealloc: zero size");
590Sstevel@tonic-gate 	if (ptr == NULL)
600Sstevel@tonic-gate 		new_ptr = malloc(new_size);
610Sstevel@tonic-gate 	else
620Sstevel@tonic-gate 		new_ptr = realloc(ptr, new_size);
630Sstevel@tonic-gate 	if (new_ptr == NULL)
640Sstevel@tonic-gate 		fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size);
650Sstevel@tonic-gate 	return new_ptr;
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate void
xfree(void * ptr)690Sstevel@tonic-gate xfree(void *ptr)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	if (ptr == NULL)
720Sstevel@tonic-gate 		fatal("xfree: NULL pointer given as argument");
730Sstevel@tonic-gate 	free(ptr);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate char *
xstrdup(const char * str)770Sstevel@tonic-gate xstrdup(const char *str)
780Sstevel@tonic-gate {
790Sstevel@tonic-gate 	size_t len;
800Sstevel@tonic-gate 	char *cp;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	len = strlen(str) + 1;
830Sstevel@tonic-gate 	cp = xmalloc(len);
840Sstevel@tonic-gate 	strlcpy(cp, str, len);
850Sstevel@tonic-gate 	return cp;
860Sstevel@tonic-gate }
87