Coverage Report

Created: 2025-10-10 06:38

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
0
{
34
0
  void *ptr;
35
36
0
  if (size == 0)
37
0
    fatal("xmalloc: zero size");
38
0
  ptr = malloc(size);
39
0
  if (ptr == NULL)
40
0
    fatal("xmalloc: out of memory (allocating %zu bytes)", size);
41
0
  return ptr;
42
0
}
43
44
void *
45
xcalloc(size_t nmemb, size_t size)
46
0
{
47
0
  void *ptr;
48
49
0
  if (size == 0 || nmemb == 0)
50
0
    fatal("xcalloc: zero size");
51
0
  if (SIZE_MAX / nmemb < size)
52
0
    fatal("xcalloc: nmemb * size > SIZE_MAX");
53
0
  ptr = calloc(nmemb, size);
54
0
  if (ptr == NULL)
55
0
    fatal("xcalloc: out of memory (allocating %zu bytes)",
56
0
        size * nmemb);
57
0
  return ptr;
58
0
}
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
0
{
75
0
  void *new_ptr;
76
77
0
  new_ptr = recallocarray(ptr, onmemb, nmemb, size);
78
0
  if (new_ptr == NULL)
79
0
    fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
80
0
        nmemb, size);
81
0
  return new_ptr;
82
0
}
83
84
char *
85
xstrdup(const char *str)
86
0
{
87
0
  size_t len;
88
0
  char *cp;
89
90
0
  len = strlen(str) + 1;
91
0
  cp = xmalloc(len);
92
0
  return memcpy(cp, str, len);
93
0
}
94
95
int
96
xvasprintf(char **ret, const char *fmt, va_list ap)
97
0
{
98
0
  int i;
99
100
0
  i = vasprintf(ret, fmt, ap);
101
0
  if (i < 0 || *ret == NULL)
102
0
    fatal("xvasprintf: could not allocate memory");
103
0
  return i;
104
0
}
105
106
int
107
xasprintf(char **ret, const char *fmt, ...)
108
0
{
109
0
  va_list ap;
110
0
  int i;
111
112
0
  va_start(ap, fmt);
113
0
  i = xvasprintf(ret, fmt, ap);
114
  va_end(ap);
115
0
  return i;
116
0
}