Coverage Report

Created: 2025-12-31 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/compat/qsort_s.c
Line
Count
Source
1
#include "../git-compat-util.h"
2
3
/*
4
 * A merge sort implementation, simplified from the qsort implementation
5
 * by Mike Haertel, which is a part of the GNU C Library.
6
 * Added context pointer, safety checks and return value.
7
 */
8
9
static void msort_with_tmp(void *b, size_t n, size_t s,
10
         int (*cmp)(const void *, const void *, void *),
11
         char *t, void *ctx)
12
0
{
13
0
  char *tmp;
14
0
  char *b1, *b2;
15
0
  size_t n1, n2;
16
17
0
  if (n <= 1)
18
0
    return;
19
20
0
  n1 = n / 2;
21
0
  n2 = n - n1;
22
0
  b1 = b;
23
0
  b2 = (char *)b + (n1 * s);
24
25
0
  msort_with_tmp(b1, n1, s, cmp, t, ctx);
26
0
  msort_with_tmp(b2, n2, s, cmp, t, ctx);
27
28
0
  tmp = t;
29
30
0
  while (n1 > 0 && n2 > 0) {
31
0
    if (cmp(b1, b2, ctx) <= 0) {
32
0
      memcpy(tmp, b1, s);
33
0
      tmp += s;
34
0
      b1 += s;
35
0
      --n1;
36
0
    } else {
37
0
      memcpy(tmp, b2, s);
38
0
      tmp += s;
39
0
      b2 += s;
40
0
      --n2;
41
0
    }
42
0
  }
43
0
  if (n1 > 0)
44
0
    memcpy(tmp, b1, n1 * s);
45
0
  memcpy(b, t, (n - n2) * s);
46
0
}
47
48
int git_qsort_s(void *b, size_t n, size_t s,
49
    int (*cmp)(const void *, const void *, void *), void *ctx)
50
0
{
51
0
  const size_t size = st_mult(n, s);
52
0
  char *tmp;
53
54
0
  if (!n)
55
0
    return 0;
56
0
  if (!b || !cmp)
57
0
    return -1;
58
59
0
  tmp = xmalloc(size);
60
0
  msort_with_tmp(b, n, s, cmp, tmp, ctx);
61
0
  free(tmp);
62
0
  return 0;
63
0
}