Coverage Report

Created: 2026-10-28 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/malloc-overflow.h
Line
Count
Source
1
#ifndef MALLOC_OVERFLOW_H
2
#define MALLOC_OVERFLOW_H
3
4
/* MALLOC_*() can be used to calculate memory allocation sizes. If there's an
5
   overflow, it'll cleanly panic instead of causing a potential buffer
6
   overflow.
7
8
   Note that *_malloc(size+1) doesn't need to use MALLOC_ADD(size, 1). It wraps
9
   to size==0 and the *_malloc() calls already panic if size==0. */
10
static inline size_t
11
malloc_multiply_check(size_t a, size_t b, size_t sizeof_a, size_t sizeof_b,
12
          const char *fname, unsigned int linenum)
13
13.8M
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
13.8M
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
20.3k
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
13.8M
  return a * b;
22
13.8M
}
Unexecuted instantiation: fuzz-var-expand-import.c:malloc_multiply_check
expansion-program.c:malloc_multiply_check
Line
Count
Source
13
10.3M
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
10.3M
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
0
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
10.3M
  return a * b;
22
10.3M
}
Unexecuted instantiation: var-expand.c:malloc_multiply_check
Unexecuted instantiation: var-expand-parser.c:malloc_multiply_check
Unexecuted instantiation: var-expand-lexer.c:malloc_multiply_check
Unexecuted instantiation: expansion-parameter.c:malloc_multiply_check
Unexecuted instantiation: expansion-statement.c:malloc_multiply_check
Unexecuted instantiation: expansion-filter.c:malloc_multiply_check
Unexecuted instantiation: expansion-filter-if.c:malloc_multiply_check
Unexecuted instantiation: expansion-filter-crypt.c:malloc_multiply_check
Unexecuted instantiation: fuzzer.c:malloc_multiply_check
Unexecuted instantiation: array.c:malloc_multiply_check
Unexecuted instantiation: base64.c:malloc_multiply_check
buffer.c:malloc_multiply_check
Line
Count
Source
13
3.46M
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
3.46M
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
4.32k
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
3.46M
  return a * b;
22
3.46M
}
Unexecuted instantiation: cpu-count.c:malloc_multiply_check
Unexecuted instantiation: data-stack.c:malloc_multiply_check
event-log.c:malloc_multiply_check
Line
Count
Source
13
1.44k
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
1.44k
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
1.44k
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
1.44k
  return a * b;
22
1.44k
}
Unexecuted instantiation: failures.c:malloc_multiply_check
Unexecuted instantiation: fd-util.c:malloc_multiply_check
Unexecuted instantiation: guid.c:malloc_multiply_check
Unexecuted instantiation: hash.c:malloc_multiply_check
Unexecuted instantiation: hash-method.c:malloc_multiply_check
Unexecuted instantiation: hex-binary.c:malloc_multiply_check
Unexecuted instantiation: hostpid.c:malloc_multiply_check
Unexecuted instantiation: imem.c:malloc_multiply_check
Unexecuted instantiation: iostream-pump.c:malloc_multiply_check
Unexecuted instantiation: iso8601-date.c:malloc_multiply_check
Unexecuted instantiation: istream.c:malloc_multiply_check
Unexecuted instantiation: istream-data.c:malloc_multiply_check
ioloop.c:malloc_multiply_check
Line
Count
Source
13
2.88k
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
2.88k
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
2.88k
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
2.88k
  return a * b;
22
2.88k
}
Unexecuted instantiation: ioloop-notify-inotify.c:malloc_multiply_check
Unexecuted instantiation: ioloop-epoll.c:malloc_multiply_check
lib.c:malloc_multiply_check
Line
Count
Source
13
1.44k
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
1.44k
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
1.44k
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
1.44k
  return a * b;
22
1.44k
}
lib-event.c:malloc_multiply_check
Line
Count
Source
13
10.4k
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
10.4k
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
10.2k
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
10.4k
  return a * b;
22
10.4k
}
Unexecuted instantiation: lib-signals.c:malloc_multiply_check
Unexecuted instantiation: md4.c:malloc_multiply_check
Unexecuted instantiation: md5.c:malloc_multiply_check
Unexecuted instantiation: memarea.c:malloc_multiply_check
Unexecuted instantiation: mempool.c:malloc_multiply_check
Unexecuted instantiation: mempool-allocfree.c:malloc_multiply_check
mempool-alloconly.c:malloc_multiply_check
Line
Count
Source
13
1.60k
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
1.60k
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
0
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
1.60k
  return a * b;
22
1.60k
}
Unexecuted instantiation: mempool-datastack.c:malloc_multiply_check
Unexecuted instantiation: mempool-system.c:malloc_multiply_check
Unexecuted instantiation: mempool-unsafe-datastack.c:malloc_multiply_check
Unexecuted instantiation: module-dir.c:malloc_multiply_check
Unexecuted instantiation: net.c:malloc_multiply_check
Unexecuted instantiation: ostream.c:malloc_multiply_check
Unexecuted instantiation: ostream-file.c:malloc_multiply_check
Unexecuted instantiation: path-util.c:malloc_multiply_check
Unexecuted instantiation: primes.c:malloc_multiply_check
Unexecuted instantiation: printf-format-fix.c:malloc_multiply_check
Unexecuted instantiation: process-title.c:malloc_multiply_check
Unexecuted instantiation: priorityq.c:malloc_multiply_check
Unexecuted instantiation: randgen.c:malloc_multiply_check
Unexecuted instantiation: rand.c:malloc_multiply_check
Unexecuted instantiation: restrict-access.c:malloc_multiply_check
Unexecuted instantiation: safe-memset.c:malloc_multiply_check
Unexecuted instantiation: sendfile-util.c:malloc_multiply_check
Unexecuted instantiation: sha1.c:malloc_multiply_check
Unexecuted instantiation: sha2.c:malloc_multiply_check
Unexecuted instantiation: sha3.c:malloc_multiply_check
Unexecuted instantiation: xxh64.c:malloc_multiply_check
Unexecuted instantiation: sleep.c:malloc_multiply_check
Unexecuted instantiation: str.c:malloc_multiply_check
Unexecuted instantiation: str-sanitize.c:malloc_multiply_check
Unexecuted instantiation: strescape.c:malloc_multiply_check
strfuncs.c:malloc_multiply_check
Line
Count
Source
13
13.3k
{
14
  /* the first sizeof-checks are intended to optimize away this entire
15
     if-check for types that are small enough to never wrap size_t. */
16
13.3k
  if ((sizeof_a * 2 > sizeof(size_t) || sizeof_b * 2 > sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
17
0
      b != 0 && (a > SIZE_MAX / b)) {
18
0
    i_panic("file %s: line %d: memory allocation overflow: %zu * %zu",
19
0
      fname, linenum, a, b);
20
0
  }
21
13.3k
  return a * b;
22
13.3k
}
Unexecuted instantiation: strnum.c:malloc_multiply_check
Unexecuted instantiation: time-util.c:malloc_multiply_check
Unexecuted instantiation: unichar.c:malloc_multiply_check
Unexecuted instantiation: unicode-break.c:malloc_multiply_check
Unexecuted instantiation: unicode-data-tables.c:malloc_multiply_check
Unexecuted instantiation: unicode-transform.c:malloc_multiply_check
Unexecuted instantiation: utc-offset.c:malloc_multiply_check
Unexecuted instantiation: utc-mktime.c:malloc_multiply_check
Unexecuted instantiation: wildcard-match.c:malloc_multiply_check
Unexecuted instantiation: write-full.c:malloc_multiply_check
Unexecuted instantiation: backtrace-string.c:malloc_multiply_check
Unexecuted instantiation: bits.c:malloc_multiply_check
Unexecuted instantiation: env-util.c:malloc_multiply_check
Unexecuted instantiation: event-filter.c:malloc_multiply_check
Unexecuted instantiation: event-filter-lexer.c:malloc_multiply_check
Unexecuted instantiation: event-filter-parser.c:malloc_multiply_check
Unexecuted instantiation: hmac.c:malloc_multiply_check
Unexecuted instantiation: ipwd.c:malloc_multiply_check
Unexecuted instantiation: iostream.c:malloc_multiply_check
Unexecuted instantiation: istream-file.c:malloc_multiply_check
Unexecuted instantiation: ioloop-iolist.c:malloc_multiply_check
Unexecuted instantiation: ioloop-notify-fd.c:malloc_multiply_check
Unexecuted instantiation: str-parse.c:malloc_multiply_check
23
#ifndef STATIC_CHECKER
24
#  define MALLOC_MULTIPLY(a, b) \
25
13.8M
  malloc_multiply_check(a, b, sizeof(a), sizeof(b), __FILE__, __LINE__) // NOLINT(bugprone-sizeof-expression)
26
#else
27
/* avoid warning every time about sizeof(b) when b contains any arithmetic */
28
#  define MALLOC_MULTIPLY(a, b) \
29
  malloc_multiply_check(a, b, sizeof(a), sizeof(size_t), __FILE__, __LINE__)  // NOLINT(bugprone-sizeof-expression)
30
#endif
31
32
static inline size_t
33
malloc_add_check(size_t a, size_t b, size_t sizeof_a, size_t sizeof_b,
34
     const char *fname, unsigned int linenum)
35
1.81k
{
36
  /* the first sizeof-checks are intended to optimize away this entire
37
     if-check for types that are small enough to never wrap size_t. */
38
1.81k
  if ((sizeof_a >= sizeof(size_t) || sizeof_b >= sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
39
1.81k
      SIZE_MAX - a < b) {
40
0
    i_panic("file %s: line %d: memory allocation overflow: %zu + %zu",
41
0
      fname, linenum, a, b);
42
0
  }
43
1.81k
  return a + b;
44
1.81k
}
Unexecuted instantiation: fuzz-var-expand-import.c:malloc_add_check
Unexecuted instantiation: expansion-program.c:malloc_add_check
Unexecuted instantiation: var-expand.c:malloc_add_check
Unexecuted instantiation: var-expand-parser.c:malloc_add_check
Unexecuted instantiation: var-expand-lexer.c:malloc_add_check
Unexecuted instantiation: expansion-parameter.c:malloc_add_check
Unexecuted instantiation: expansion-statement.c:malloc_add_check
Unexecuted instantiation: expansion-filter.c:malloc_add_check
Unexecuted instantiation: expansion-filter-if.c:malloc_add_check
Unexecuted instantiation: expansion-filter-crypt.c:malloc_add_check
Unexecuted instantiation: fuzzer.c:malloc_add_check
Unexecuted instantiation: array.c:malloc_add_check
Unexecuted instantiation: base64.c:malloc_add_check
Unexecuted instantiation: buffer.c:malloc_add_check
Unexecuted instantiation: cpu-count.c:malloc_add_check
data-stack.c:malloc_add_check
Line
Count
Source
35
1.81k
{
36
  /* the first sizeof-checks are intended to optimize away this entire
37
     if-check for types that are small enough to never wrap size_t. */
38
1.81k
  if ((sizeof_a >= sizeof(size_t) || sizeof_b >= sizeof(size_t)) && // NOLINT(bugprone-sizeof-expression)
39
1.81k
      SIZE_MAX - a < b) {
40
0
    i_panic("file %s: line %d: memory allocation overflow: %zu + %zu",
41
0
      fname, linenum, a, b);
42
0
  }
43
1.81k
  return a + b;
44
1.81k
}
Unexecuted instantiation: event-log.c:malloc_add_check
Unexecuted instantiation: failures.c:malloc_add_check
Unexecuted instantiation: fd-util.c:malloc_add_check
Unexecuted instantiation: guid.c:malloc_add_check
Unexecuted instantiation: hash.c:malloc_add_check
Unexecuted instantiation: hash-method.c:malloc_add_check
Unexecuted instantiation: hex-binary.c:malloc_add_check
Unexecuted instantiation: hostpid.c:malloc_add_check
Unexecuted instantiation: imem.c:malloc_add_check
Unexecuted instantiation: iostream-pump.c:malloc_add_check
Unexecuted instantiation: iso8601-date.c:malloc_add_check
Unexecuted instantiation: istream.c:malloc_add_check
Unexecuted instantiation: istream-data.c:malloc_add_check
Unexecuted instantiation: ioloop.c:malloc_add_check
Unexecuted instantiation: ioloop-notify-inotify.c:malloc_add_check
Unexecuted instantiation: ioloop-epoll.c:malloc_add_check
Unexecuted instantiation: lib.c:malloc_add_check
Unexecuted instantiation: lib-event.c:malloc_add_check
Unexecuted instantiation: lib-signals.c:malloc_add_check
Unexecuted instantiation: md4.c:malloc_add_check
Unexecuted instantiation: md5.c:malloc_add_check
Unexecuted instantiation: memarea.c:malloc_add_check
Unexecuted instantiation: mempool.c:malloc_add_check
Unexecuted instantiation: mempool-allocfree.c:malloc_add_check
Unexecuted instantiation: mempool-alloconly.c:malloc_add_check
Unexecuted instantiation: mempool-datastack.c:malloc_add_check
Unexecuted instantiation: mempool-system.c:malloc_add_check
Unexecuted instantiation: mempool-unsafe-datastack.c:malloc_add_check
Unexecuted instantiation: module-dir.c:malloc_add_check
Unexecuted instantiation: net.c:malloc_add_check
Unexecuted instantiation: ostream.c:malloc_add_check
Unexecuted instantiation: ostream-file.c:malloc_add_check
Unexecuted instantiation: path-util.c:malloc_add_check
Unexecuted instantiation: primes.c:malloc_add_check
Unexecuted instantiation: printf-format-fix.c:malloc_add_check
Unexecuted instantiation: process-title.c:malloc_add_check
Unexecuted instantiation: priorityq.c:malloc_add_check
Unexecuted instantiation: randgen.c:malloc_add_check
Unexecuted instantiation: rand.c:malloc_add_check
Unexecuted instantiation: restrict-access.c:malloc_add_check
Unexecuted instantiation: safe-memset.c:malloc_add_check
Unexecuted instantiation: sendfile-util.c:malloc_add_check
Unexecuted instantiation: sha1.c:malloc_add_check
Unexecuted instantiation: sha2.c:malloc_add_check
Unexecuted instantiation: sha3.c:malloc_add_check
Unexecuted instantiation: xxh64.c:malloc_add_check
Unexecuted instantiation: sleep.c:malloc_add_check
Unexecuted instantiation: str.c:malloc_add_check
Unexecuted instantiation: str-sanitize.c:malloc_add_check
Unexecuted instantiation: strescape.c:malloc_add_check
Unexecuted instantiation: strfuncs.c:malloc_add_check
Unexecuted instantiation: strnum.c:malloc_add_check
Unexecuted instantiation: time-util.c:malloc_add_check
Unexecuted instantiation: unichar.c:malloc_add_check
Unexecuted instantiation: unicode-break.c:malloc_add_check
Unexecuted instantiation: unicode-data-tables.c:malloc_add_check
Unexecuted instantiation: unicode-transform.c:malloc_add_check
Unexecuted instantiation: utc-offset.c:malloc_add_check
Unexecuted instantiation: utc-mktime.c:malloc_add_check
Unexecuted instantiation: wildcard-match.c:malloc_add_check
Unexecuted instantiation: write-full.c:malloc_add_check
Unexecuted instantiation: backtrace-string.c:malloc_add_check
Unexecuted instantiation: bits.c:malloc_add_check
Unexecuted instantiation: env-util.c:malloc_add_check
Unexecuted instantiation: event-filter.c:malloc_add_check
Unexecuted instantiation: event-filter-lexer.c:malloc_add_check
Unexecuted instantiation: event-filter-parser.c:malloc_add_check
Unexecuted instantiation: hmac.c:malloc_add_check
Unexecuted instantiation: ipwd.c:malloc_add_check
Unexecuted instantiation: iostream.c:malloc_add_check
Unexecuted instantiation: istream-file.c:malloc_add_check
Unexecuted instantiation: ioloop-iolist.c:malloc_add_check
Unexecuted instantiation: ioloop-notify-fd.c:malloc_add_check
Unexecuted instantiation: str-parse.c:malloc_add_check
45
#ifndef STATIC_CHECKER
46
#  define MALLOC_ADD(a, b) \
47
1.81k
  malloc_add_check(a, b, sizeof(a), sizeof(b), __FILE__, __LINE__) // NOLINT(bugprone-sizeof-expression)
48
#else
49
/* avoid warning every time about sizeof(b) when b contains any arithmetic */
50
#  define MALLOC_ADD(a, b) \
51
  malloc_add_check(a, b, sizeof(a), sizeof(size_t), __FILE__, __LINE__) // NOLINT(bugprone-sizeof-expression)
52
#endif
53
54
/* Minimal convenience wrapper for common allocation size arithmetic.
55
   Built on MALLOC_ADD() to keep overflow handling centralized. */
56
#define MALLOC_ADD3(a, b, c) \
57
0
  MALLOC_ADD(MALLOC_ADD((a), (b)), (c))
58
59
#endif