Annotation of src/usr.bin/ssh/xmalloc.c, Revision 1.3
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.3 ! deraadt 18: RCSID("$Id: xmalloc.c,v 1.2 1999/09/29 21:14:16 deraadt Exp $");
1.1 deraadt 19:
20: #include "ssh.h"
21:
22: void *xmalloc(size_t size)
23: {
24: void *ptr = malloc(size);
25: if (ptr == NULL)
26: fatal("xmalloc: out of memory (allocating %d bytes)", (int)size);
27: return ptr;
28: }
29:
30: void *xrealloc(void *ptr, size_t new_size)
31: {
32: void *new_ptr;
33:
34: if (ptr == NULL)
35: fatal("xrealloc: NULL pointer given as argument");
36: new_ptr = realloc(ptr, new_size);
37: if (new_ptr == NULL)
38: fatal("xrealloc: out of memory (new_size %d bytes)", (int)new_size);
39: return new_ptr;
40: }
41:
42: void xfree(void *ptr)
43: {
44: if (ptr == NULL)
45: fatal("xfree: NULL pointer given as argument");
46: free(ptr);
47: }
48:
49: char *xstrdup(const char *str)
50: {
1.2 deraadt 51: int len = strlen(str) + 1;
52:
53: char *cp = xmalloc(len);
54: strlcpy(cp, str, len);
1.1 deraadt 55: return cp;
56: }