Skip to content

Commit 66775d2

Browse files
committed
Merge branch 'jc/qsort-s-alignment-fix'
Fix a hand-rolled alloca() imitation that may have violated alignment requirement of data being sorted in compatibility implementation of qsort_s() and stable qsort(). * jc/qsort-s-alignment-fix: stable-qsort: avoid using potentially unaligned access compat/qsort_s.c: avoid using potentially unaligned access
2 parents 4bb003d + b80741e commit 66775d2

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

compat/qsort_s.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,15 @@ int git_qsort_s(void *b, size_t n, size_t s,
4949
int (*cmp)(const void *, const void *, void *), void *ctx)
5050
{
5151
const size_t size = st_mult(n, s);
52-
char buf[1024];
52+
char *tmp;
5353

5454
if (!n)
5555
return 0;
5656
if (!b || !cmp)
5757
return -1;
5858

59-
if (size < sizeof(buf)) {
60-
/* The temporary array fits on the small on-stack buffer. */
61-
msort_with_tmp(b, n, s, cmp, buf, ctx);
62-
} else {
63-
/* It's somewhat large, so malloc it. */
64-
char *tmp = xmalloc(size);
65-
msort_with_tmp(b, n, s, cmp, tmp, ctx);
66-
free(tmp);
67-
}
59+
tmp = xmalloc(size);
60+
msort_with_tmp(b, n, s, cmp, tmp, ctx);
61+
free(tmp);
6862
return 0;
6963
}

stable-qsort.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,9 @@ void git_stable_qsort(void *b, size_t n, size_t s,
4848
int (*cmp)(const void *, const void *))
4949
{
5050
const size_t size = st_mult(n, s);
51-
char buf[1024];
52-
53-
if (size < sizeof(buf)) {
54-
/* The temporary array fits on the small on-stack buffer. */
55-
msort_with_tmp(b, n, s, cmp, buf);
56-
} else {
57-
/* It's somewhat large, so malloc it. */
58-
char *tmp = xmalloc(size);
59-
msort_with_tmp(b, n, s, cmp, tmp);
60-
free(tmp);
61-
}
51+
char *tmp;
52+
53+
tmp = xmalloc(size);
54+
msort_with_tmp(b, n, s, cmp, tmp);
55+
free(tmp);
6256
}

0 commit comments

Comments
 (0)