Coverage Report

Created: 2025-10-13 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssh/xmalloc.c
Line
Count
Source
1
/* $OpenBSD: xmalloc.c,v 1.38 2025/05/23 00:40:45 deraadt 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 <stdarg.h>
19
#include <stdint.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23
24
#include "xmalloc.h"
25
#include "log.h"
26
27
#if defined(__OpenBSD__)
28
const char * const malloc_options = "S";
29
#endif /* __OpenBSD__ */
30
31
void *
32
xmalloc(size_t size)
33
3.99M
{
34
3.99M
  void *ptr;
35
36
3.99M
  if (size == 0)
37
0
    fatal("xmalloc: zero size");
38
3.99M
  ptr = malloc(size);
39
3.99M
  if (ptr == NULL)
40
0
    fatal("xmalloc: out of memory (allocating %zu bytes)", size);
41
3.99M
  return ptr;
42
3.99M
}
43
44
void *
45
xcalloc(size_t nmemb, size_t size)
46
7.08k
{
47
7.08k
  void *ptr;
48
49
7.08k
  if (size == 0 || nmemb == 0)
50
0
    fatal("xcalloc: zero size");
51
7.08k
  if (SIZE_MAX / nmemb < size)
52
0
    fatal("xcalloc: nmemb * size > SIZE_MAX");
53
7.08k
  ptr = calloc(nmemb, size);
54
7.08k
  if (ptr == NULL)
55
0
    fatal("xcalloc: out of memory (allocating %zu bytes)",
56
7.08k
        size * nmemb);
57
7.08k
  return ptr;
58
7.08k
}
59
60
void *
61
xreallocarray(void *ptr, size_t nmemb, size_t size)
62
0
{
63
0
  void *new_ptr;
64
65
0
  new_ptr = reallocarray(ptr, nmemb, size);
66
0
  if (new_ptr == NULL)
67
0
    fatal("xreallocarray: out of memory (%zu elements of %zu bytes)",
68
0
        nmemb, size);
69
0
  return new_ptr;
70
0
}
71
72
void *
73
xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size)
74
6
{
75
6
  void *new_ptr;
76
77
6
  new_ptr = recallocarray(ptr, onmemb, nmemb, size);
78
6
  if (new_ptr == NULL)
79
0
    fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
80
6
        nmemb, size);
81
6
  return new_ptr;
82
6
}
83
84
char *
85
xstrdup(const char *str)
86
3.99M
{
87
3.99M
  size_t len;
88
3.99M
  char *cp;
89
90
3.99M
  len = strlen(str) + 1;
91
3.99M
  cp = xmalloc(len);
92
3.99M
  return memcpy(cp, str, len);
93
3.99M
}
94
95
int
96
xvasprintf(char **ret, const char *fmt, va_list ap)
97
2.35k
{
98
2.35k
  int i;
99
100
2.35k
  i = vasprintf(ret, fmt, ap);
101
2.35k
  if (i < 0 || *ret == NULL)
102
0
    fatal("xvasprintf: could not allocate memory");
103
2.35k
  return i;
104
2.35k
}
105
106
int
107
xasprintf(char **ret, const char *fmt, ...)
108
1.11k
{
109
1.11k
  va_list ap;
110
1.11k
  int i;
111
112
1.11k
  va_start(ap, fmt);
113
1.11k
  i = xvasprintf(ret, fmt, ap);
114
  va_end(ap);
115
1.11k
  return i;
116
1.11k
}