Coverage Report

Created: 2026-01-10 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntpsec/libntp/emalloc.c
Line
Count
Source
1
/*
2
 * emalloc - return new memory obtained from the system.  Belch if none.
3
 */
4
#include "config.h"
5
#include "ntp_types.h"
6
#include "ntp_malloc.h"
7
#include "ntp_syslog.h"
8
#include "ntp_stdlib.h"
9
10
11
/*
12
 * When using the debug MS CRT allocator, each allocation stores the
13
 * callsite __FILE__ and __LINE__, which is then displayed at process
14
 * termination, to track down leaks.  We don't want all of our
15
 * allocations to show up as coming from emalloc.c, so we preserve the
16
 * original callsite's source file and line using macros which pass
17
 * __FILE__ and __LINE__ as parameters to these routines.
18
 * Other debug malloc implementations can be used by defining
19
 * EREALLOC_IMPL() as ports/winnt/include/config.h does.
20
 */
21
22
void *
23
ereallocz(
24
  void *  ptr,
25
  size_t  newsz,
26
  size_t  priorsz,
27
  int zero_init
28
#ifdef EREALLOC_CALLSITE    /* ntp_malloc.h */
29
       ,
30
  const char *  file,
31
  int   line
32
#endif
33
  )
34
0
{
35
0
  char *  mem;
36
0
  size_t  allocsz;
37
38
0
  if (0 == newsz)
39
0
    allocsz = 1;
40
0
  else
41
0
    allocsz = newsz;
42
43
0
  mem = EREALLOC_IMPL(ptr, allocsz, file, line);
44
0
  if (NULL == mem) {
45
0
    termlogit = true;
46
0
#ifndef EREALLOC_CALLSITE
47
0
    msyslog(LOG_ERR, "ERR: fatal out of memory (%lu bytes)",
48
0
      (unsigned long)newsz);
49
#else
50
    msyslog(LOG_ERR,
51
      "ERR: fatal out of memory %s line %d (%lu bytes)",
52
      file, line, (unsigned long)newsz);
53
#endif
54
0
    exit(1);
55
0
  }
56
57
0
  if (zero_init && newsz > priorsz)
58
0
    memset(mem + priorsz, '\0', newsz - priorsz);
59
60
0
  return mem;
61
0
}
62
63
/* oreallocarray.c is licensed under the following:
64
 * Copyright Otto Moerbeek <otto@drijf.net>
65
 *
66
 * Permission to use, copy, modify, and distribute this software for any
67
 * purpose with or without fee is hereby granted, provided that the above
68
 * copyright notice and this permission notice appear in all copies.
69
 *
70
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
71
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
72
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
73
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
74
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
75
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
76
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
77
 */
78
79
#include <stdint.h>
80
81
/*
82
 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
83
 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
84
 */
85
0
#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4))
86
87
void *
88
oreallocarray(
89
  void *optr,
90
  size_t nmemb,
91
  size_t size
92
#ifdef EREALLOC_CALLSITE    /* ntp_malloc.h */
93
  ,
94
  const char *  file,
95
  int   line
96
#endif
97
  )
98
0
{
99
0
  if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
100
0
      nmemb > 0 && SIZE_MAX / nmemb < size) {
101
0
#ifndef EREALLOC_CALLSITE
102
0
    msyslog(LOG_ERR, "ERR: fatal allocation size overflow");
103
#else
104
    msyslog(LOG_ERR,
105
      "ERR: fatal allocation size overflow %s line %d",
106
      file, line);
107
#endif
108
0
    exit(1);
109
0
  }
110
0
#ifndef EREALLOC_CALLSITE
111
0
  return ereallocz(optr, (size * nmemb), 0, false);
112
#else
113
  return ereallocz(optr, (size * nmemb), 0, false, file, line);
114
#endif
115
0
}
116
117
char *
118
estrdup_impl(
119
  const char *  str
120
#ifdef EREALLOC_CALLSITE
121
         ,
122
  const char *  file,
123
  int   line
124
#endif
125
  )
126
0
{
127
0
  char *  copy;
128
0
  size_t  bytes;
129
130
0
  bytes = strlen(str) + 1;
131
0
  copy = ereallocz(NULL, bytes, 0, false
132
#ifdef EREALLOC_CALLSITE
133
       , file, line
134
#endif
135
0
       );
136
0
  memcpy(copy, str, bytes);
137
138
0
  return copy;
139
0
}
140