Coverage Report

Created: 2026-04-27 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/xmalloc.c
Line
Count
Source
1
/* $OpenBSD$ */
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
54.0k
{
29
54.0k
  void *ptr;
30
31
54.0k
  if (size == 0)
32
0
    fatalx("xmalloc: zero size");
33
54.0k
  ptr = malloc(size);
34
54.0k
  if (ptr == NULL)
35
0
    fatalx("xmalloc: allocating %zu bytes: %s",
36
0
        size, strerror(errno));
37
54.0k
  return ptr;
38
54.0k
}
39
40
void *
41
xcalloc(size_t nmemb, size_t size)
42
145k
{
43
145k
  void *ptr;
44
45
145k
  if (size == 0 || nmemb == 0)
46
0
    fatalx("xcalloc: zero size");
47
145k
  ptr = calloc(nmemb, size);
48
145k
  if (ptr == NULL)
49
0
    fatalx("xcalloc: allocating %zu * %zu bytes: %s",
50
0
        nmemb, size, strerror(errno));
51
145k
  return ptr;
52
145k
}
53
54
void *
55
xrealloc(void *ptr, size_t size)
56
26.6k
{
57
26.6k
  return xreallocarray(ptr, 1, size);
58
26.6k
}
59
60
void *
61
xreallocarray(void *ptr, size_t nmemb, size_t size)
62
150k
{
63
150k
  void *new_ptr;
64
65
150k
  if (nmemb == 0 || size == 0)
66
0
    fatalx("xreallocarray: zero size");
67
150k
  new_ptr = reallocarray(ptr, nmemb, size);
68
150k
  if (new_ptr == NULL)
69
0
    fatalx("xreallocarray: allocating %zu * %zu bytes: %s",
70
0
        nmemb, size, strerror(errno));
71
150k
  return new_ptr;
72
150k
}
73
74
void *
75
xrecallocarray(void *ptr, size_t oldnmemb, size_t nmemb, size_t size)
76
6
{
77
6
  void *new_ptr;
78
79
6
  if (nmemb == 0 || size == 0)
80
0
    fatalx("xrecallocarray: zero size");
81
6
  new_ptr = recallocarray(ptr, oldnmemb, nmemb, size);
82
6
  if (new_ptr == NULL)
83
0
    fatalx("xrecallocarray: allocating %zu * %zu bytes: %s",
84
0
        nmemb, size, strerror(errno));
85
6
  return new_ptr;
86
6
}
87
88
char *
89
xstrdup(const char *str)
90
158k
{
91
158k
  char *cp;
92
93
158k
  if ((cp = strdup(str)) == NULL)
94
0
    fatalx("xstrdup: %s", strerror(errno));
95
158k
  return cp;
96
158k
}
97
98
char *
99
xstrndup(const char *str, size_t maxlen)
100
4.79k
{
101
4.79k
  char *cp;
102
103
4.79k
  if ((cp = strndup(str, maxlen)) == NULL)
104
0
    fatalx("xstrndup: %s", strerror(errno));
105
4.79k
  return cp;
106
4.79k
}
107
108
int
109
xasprintf(char **ret, const char *fmt, ...)
110
17.7k
{
111
17.7k
  va_list ap;
112
17.7k
  int i;
113
114
17.7k
  va_start(ap, fmt);
115
17.7k
  i = xvasprintf(ret, fmt, ap);
116
17.7k
  va_end(ap);
117
118
17.7k
  return i;
119
17.7k
}
120
121
int
122
xvasprintf(char **ret, const char *fmt, va_list ap)
123
51.3k
{
124
51.3k
  int i;
125
126
51.3k
  i = vasprintf(ret, fmt, ap);
127
128
51.3k
  if (i == -1)
129
0
    fatalx("xasprintf: %s", strerror(errno));
130
131
51.3k
  return i;
132
51.3k
}
133
134
int
135
xsnprintf(char *str, size_t len, const char *fmt, ...)
136
6.30k
{
137
6.30k
  va_list ap;
138
6.30k
  int i;
139
140
6.30k
  va_start(ap, fmt);
141
6.30k
  i = xvsnprintf(str, len, fmt, ap);
142
6.30k
  va_end(ap);
143
144
6.30k
  return i;
145
6.30k
}
146
147
int
148
xvsnprintf(char *str, size_t len, const char *fmt, va_list ap)
149
6.30k
{
150
6.30k
  int i;
151
152
6.30k
  if (len > INT_MAX)
153
0
    fatalx("xsnprintf: len > INT_MAX");
154
155
6.30k
  i = vsnprintf(str, len, fmt, ap);
156
157
6.30k
  if (i < 0 || i >= (int)len)
158
0
    fatalx("xsnprintf: overflow");
159
160
6.30k
  return i;
161
6.30k
}