Branch data Line data Source code
1 : : /* $OpenBSD: xmalloc.c,v 1.29 2014/01/04 17:50:55 tedu Exp $ */
2 : : /*
3 : : * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 : : * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 : : * All rights reserved
6 : : * Versions of malloc and friends that check their results, and never return
7 : : * failure (they call fatal if they encounter an error).
8 : : *
9 : : * As far as I am concerned, the code I have written for this software
10 : : * can be used freely for any purpose. Any derived versions of this
11 : : * software must be clearly marked as such, and if the derived work is
12 : : * incompatible with the protocol description in the RFC file, it must be
13 : : * called by a name other than "ssh" or "Secure Shell".
14 : : */
15 : :
16 : : #include "includes.h"
17 : :
18 : : #include <sys/param.h>
19 : : #include <stdarg.h>
20 : : #include <stdio.h>
21 : : #include <stdlib.h>
22 : : #include <string.h>
23 : :
24 : : #include "xmalloc.h"
25 : : #include "log.h"
26 : :
27 : : void *
28 : 2250867 : xmalloc(size_t size)
29 : : {
30 : : void *ptr;
31 : :
32 [ - + ]: 2250867 : if (size == 0)
33 : 0 : fatal("xmalloc: zero size");
34 : 2250867 : ptr = malloc(size);
35 [ - + ]: 2250867 : if (ptr == NULL)
36 : 0 : fatal("xmalloc: out of memory (allocating %zu bytes)", size);
37 : 2250867 : return ptr;
38 : : }
39 : :
40 : : void *
41 : 1554713 : xcalloc(size_t nmemb, size_t size)
42 : : {
43 : : void *ptr;
44 : :
45 [ - + ]: 1554713 : if (size == 0 || nmemb == 0)
46 : 0 : fatal("xcalloc: zero size");
47 [ - + ]: 1554713 : if (SIZE_T_MAX / nmemb < size)
48 : 0 : fatal("xcalloc: nmemb * size > SIZE_T_MAX");
49 : 1554713 : ptr = calloc(nmemb, size);
50 [ - + ]: 1554713 : if (ptr == NULL)
51 : 0 : fatal("xcalloc: out of memory (allocating %zu bytes)",
52 : : size * nmemb);
53 : 1554713 : return ptr;
54 : : }
55 : :
56 : : void *
57 : 30295 : xrealloc(void *ptr, size_t nmemb, size_t size)
58 : : {
59 : : void *new_ptr;
60 : 30295 : size_t new_size = nmemb * size;
61 : :
62 [ - + ]: 30295 : if (new_size == 0)
63 : 0 : fatal("xrealloc: zero size");
64 [ - + ]: 30295 : if (SIZE_T_MAX / nmemb < size)
65 : 0 : fatal("xrealloc: nmemb * size > SIZE_T_MAX");
66 [ + + ]: 30295 : if (ptr == NULL)
67 : 8134 : new_ptr = malloc(new_size);
68 : : else
69 : 22161 : new_ptr = realloc(ptr, new_size);
70 [ - + ]: 30295 : if (new_ptr == NULL)
71 : 0 : fatal("xrealloc: out of memory (new_size %zu bytes)",
72 : : new_size);
73 : 30295 : return new_ptr;
74 : : }
75 : :
76 : : char *
77 : 209320 : xstrdup(const char *str)
78 : : {
79 : : size_t len;
80 : : char *cp;
81 : :
82 : 209320 : len = strlen(str) + 1;
83 : 209320 : cp = xmalloc(len);
84 : 209320 : strlcpy(cp, str, len);
85 : 209320 : return cp;
86 : : }
87 : :
88 : : int
89 : 8478 : xasprintf(char **ret, const char *fmt, ...)
90 : : {
91 : : va_list ap;
92 : : int i;
93 : :
94 : 8478 : va_start(ap, fmt);
95 : 8478 : i = vasprintf(ret, fmt, ap);
96 : 8478 : va_end(ap);
97 : :
98 [ + - ][ - + ]: 8478 : if (i < 0 || *ret == NULL)
99 : 0 : fatal("xasprintf: could not allocate memory");
100 : :
101 : 8478 : return (i);
102 : : }
|