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
maint
Junio C Hamano 2022-02-05 09:42:31 -08:00
commit 66775d2109
2 changed files with 8 additions and 20 deletions

View File

@ -49,21 +49,15 @@ int git_qsort_s(void *b, size_t n, size_t s,
int (*cmp)(const void *, const void *, void *), void *ctx) int (*cmp)(const void *, const void *, void *), void *ctx)
{ {
const size_t size = st_mult(n, s); const size_t size = st_mult(n, s);
char buf[1024]; char *tmp;


if (!n) if (!n)
return 0; return 0;
if (!b || !cmp) if (!b || !cmp)
return -1; return -1;


if (size < sizeof(buf)) { tmp = xmalloc(size);
/* The temporary array fits on the small on-stack buffer. */ msort_with_tmp(b, n, s, cmp, tmp, ctx);
msort_with_tmp(b, n, s, cmp, buf, ctx); free(tmp);
} else {
/* It's somewhat large, so malloc it. */
char *tmp = xmalloc(size);
msort_with_tmp(b, n, s, cmp, tmp, ctx);
free(tmp);
}
return 0; return 0;
} }

View File

@ -48,15 +48,9 @@ void git_stable_qsort(void *b, size_t n, size_t s,
int (*cmp)(const void *, const void *)) int (*cmp)(const void *, const void *))
{ {
const size_t size = st_mult(n, s); const size_t size = st_mult(n, s);
char buf[1024]; char *tmp;


if (size < sizeof(buf)) { tmp = xmalloc(size);
/* The temporary array fits on the small on-stack buffer. */ msort_with_tmp(b, n, s, cmp, tmp);
msort_with_tmp(b, n, s, cmp, buf); free(tmp);
} else {
/* It's somewhat large, so malloc it. */
char *tmp = xmalloc(size);
msort_with_tmp(b, n, s, cmp, tmp);
free(tmp);
}
} }