Coverage Report

Created: 2025-11-19 06:37

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