GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/xmalloc.c Lines: 0 40 0.0 %
Date: 2016-12-06 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: xmalloc.c,v 1.10 2016/04/04 16:19:43 nicm Exp $ */
2
3
/*
4
 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5
 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6
 *                    All rights reserved
7
 * Versions of malloc and friends that check their results, and never return
8
 * failure (they call fatalx if they encounter an error).
9
 *
10
 * As far as I am concerned, the code I have written for this software
11
 * can be used freely for any purpose.  Any derived versions of this
12
 * software must be clearly marked as such, and if the derived work is
13
 * incompatible with the protocol description in the RFC file, it must be
14
 * called by a name other than "ssh" or "Secure Shell".
15
 */
16
17
#include <errno.h>
18
#include <limits.h>
19
#include <stdint.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
#include "tmux.h"
25
26
void *
27
xmalloc(size_t size)
28
{
29
	void *ptr;
30
31
	if (size == 0)
32
		fatalx("xmalloc: zero size");
33
	ptr = malloc(size);
34
	if (ptr == NULL)
35
		fatalx("xmalloc: allocating %zu bytes: %s",
36
		    size, strerror(errno));
37
	return ptr;
38
}
39
40
void *
41
xcalloc(size_t nmemb, size_t size)
42
{
43
	void *ptr;
44
45
	if (size == 0 || nmemb == 0)
46
		fatalx("xcalloc: zero size");
47
	ptr = calloc(nmemb, size);
48
	if (ptr == NULL)
49
		fatalx("xcalloc: allocating %zu * %zu bytes: %s",
50
		    nmemb, size, strerror(errno));
51
	return ptr;
52
}
53
54
void *
55
xrealloc(void *ptr, size_t size)
56
{
57
	return xreallocarray(ptr, 1, size);
58
}
59
60
void *
61
xreallocarray(void *ptr, size_t nmemb, size_t size)
62
{
63
	void *new_ptr;
64
65
	if (nmemb == 0 || size == 0)
66
		fatalx("xreallocarray: zero size");
67
	new_ptr = reallocarray(ptr, nmemb, size);
68
	if (new_ptr == NULL)
69
		fatalx("xreallocarray: allocating %zu * %zu bytes: %s",
70
		    nmemb, size, strerror(errno));
71
	return new_ptr;
72
}
73
74
char *
75
xstrdup(const char *str)
76
{
77
	char *cp;
78
79
	if ((cp = strdup(str)) == NULL)
80
		fatalx("xstrdup: %s", strerror(errno));
81
	return cp;
82
}
83
84
int
85
xasprintf(char **ret, const char *fmt, ...)
86
{
87
	va_list ap;
88
	int i;
89
90
	va_start(ap, fmt);
91
	i = xvasprintf(ret, fmt, ap);
92
	va_end(ap);
93
94
	return i;
95
}
96
97
int
98
xvasprintf(char **ret, const char *fmt, va_list ap)
99
{
100
	int i;
101
102
	i = vasprintf(ret, fmt, ap);
103
104
	if (i < 0 || *ret == NULL)
105
		fatalx("xasprintf: %s", strerror(errno));
106
107
	return i;
108
}
109
110
int
111
xsnprintf(char *str, size_t len, const char *fmt, ...)
112
{
113
	va_list ap;
114
	int i;
115
116
	va_start(ap, fmt);
117
	i = xvsnprintf(str, len, fmt, ap);
118
	va_end(ap);
119
120
	return i;
121
}
122
123
int
124
xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
125
{
126
	int i;
127
128
	if (len > INT_MAX)
129
		fatalx("xsnprintf: len > INT_MAX");
130
131
	i = vsnprintf(str, len, fmt, ap);
132
133
	if (i < 0 || i >= (int)len)
134
		fatalx("xsnprintf: overflow");
135
136
	return i;
137
}