Annotation of src/usr.bin/ssh/xmalloc.c, Revision 1.4
1.1 deraadt 1: /*
2:
3: xmalloc.c
4:
5: Author: Tatu Ylonen <ylo@cs.hut.fi>
6:
7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: All rights reserved
9:
10: Created: Mon Mar 20 21:23:10 1995 ylo
11:
12: Versions of malloc and friends that check their results, and never return
13: failure (they call fatal if they encounter an error).
14:
15: */
16:
17: #include "includes.h"
1.4 ! markus 18: RCSID("$Id: xmalloc.c,v 1.3 1999/09/30 04:30:03 deraadt Exp $");
1.1 deraadt 19:
20: #include "ssh.h"
21:
1.4 ! markus 22: void *
! 23: xmalloc(size_t size)
1.1 deraadt 24: {
1.4 ! markus 25: void *ptr = malloc(size);
! 26: if (ptr == NULL)
! 27: fatal("xmalloc: out of memory (allocating %d bytes)", (int) size);
! 28: return ptr;
1.1 deraadt 29: }
30:
1.4 ! markus 31: void *
! 32: xrealloc(void *ptr, size_t new_size)
1.1 deraadt 33: {
1.4 ! markus 34: void *new_ptr;
1.1 deraadt 35:
1.4 ! markus 36: if (ptr == NULL)
! 37: fatal("xrealloc: NULL pointer given as argument");
! 38: new_ptr = realloc(ptr, new_size);
! 39: if (new_ptr == NULL)
! 40: fatal("xrealloc: out of memory (new_size %d bytes)", (int) new_size);
! 41: return new_ptr;
1.1 deraadt 42: }
43:
1.4 ! markus 44: void
! 45: xfree(void *ptr)
1.1 deraadt 46: {
1.4 ! markus 47: if (ptr == NULL)
! 48: fatal("xfree: NULL pointer given as argument");
! 49: free(ptr);
1.1 deraadt 50: }
51:
1.4 ! markus 52: char *
! 53: xstrdup(const char *str)
1.1 deraadt 54: {
1.4 ! markus 55: int len = strlen(str) + 1;
1.2 deraadt 56:
1.4 ! markus 57: char *cp = xmalloc(len);
! 58: strlcpy(cp, str, len);
! 59: return cp;
1.1 deraadt 60: }