Coverage Report

Created: 2025-11-01 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/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
15.6k
{
34
15.6k
  void *ptr;
35
36
15.6k
  if (size == 0)
37
0
    fatal("xmalloc: zero size");
38
15.6k
  ptr = malloc(size);
39
15.6k
  if (ptr == NULL)
40
0
    fatal("xmalloc: out of memory (allocating %zu bytes)", size);
41
15.6k
  return ptr;
42
15.6k
}
43
44
void *
45
xcalloc(size_t nmemb, size_t size)
46
9.07k
{
47
9.07k
  void *ptr;
48
49
9.07k
  if (size == 0 || nmemb == 0)
50
0
    fatal("xcalloc: zero size");
51
9.07k
  if (SIZE_MAX / nmemb < size)
52
0
    fatal("xcalloc: nmemb * size > SIZE_MAX");
53
9.07k
  ptr = calloc(nmemb, size);
54
9.07k
  if (ptr == NULL)
55
0
    fatal("xcalloc: out of memory (allocating %zu bytes)",
56
9.07k
        size * nmemb);
57
9.07k
  return ptr;
58
9.07k
}
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
825
{
75
825
  void *new_ptr;
76
77
825
  new_ptr = recallocarray(ptr, onmemb, nmemb, size);
78
825
  if (new_ptr == NULL)
79
0
    fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)",
80
825
        nmemb, size);
81
825
  return new_ptr;
82
825
}
83
84
char *
85
xstrdup(const char *str)
86
15.6k
{
87
15.6k
  size_t len;
88
15.6k
  char *cp;
89
90
15.6k
  len = strlen(str) + 1;
91
15.6k
  cp = xmalloc(len);
92
15.6k
  return memcpy(cp, str, len);
93
15.6k
}
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
}