Fix posix_memalign with input size 0.

Return a valid pointer instead of failed assertion.
This commit is contained in:
Qi Wang 2019-07-16 14:35:53 -07:00 committed by Qi Wang
parent a2a693e722
commit f32f23d6cc
2 changed files with 20 additions and 7 deletions

View File

@ -1816,6 +1816,11 @@ struct static_opts_s {
/* Whether or not allocation size may overflow. */ /* Whether or not allocation size may overflow. */
bool may_overflow; bool may_overflow;
/*
* Whether or not allocations (with alignment) of size 0 should be
* treated as size 1.
*/
bool bump_empty_aligned_alloc;
/* /*
* Whether to assert that allocations are not of size 0 (after any * Whether to assert that allocations are not of size 0 (after any
* bumping). * bumping).
@ -1857,6 +1862,7 @@ struct static_opts_s {
JEMALLOC_ALWAYS_INLINE void JEMALLOC_ALWAYS_INLINE void
static_opts_init(static_opts_t *static_opts) { static_opts_init(static_opts_t *static_opts) {
static_opts->may_overflow = false; static_opts->may_overflow = false;
static_opts->bump_empty_aligned_alloc = false;
static_opts->assert_nonempty_alloc = false; static_opts->assert_nonempty_alloc = false;
static_opts->null_out_result_on_error = false; static_opts->null_out_result_on_error = false;
static_opts->set_errno_on_error = false; static_opts->set_errno_on_error = false;
@ -2044,11 +2050,6 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
goto label_oom; goto label_oom;
} }
/* Validate the user input. */
if (sopts->assert_nonempty_alloc) {
assert (size != 0);
}
if (unlikely(dopts->alignment < sopts->min_alignment if (unlikely(dopts->alignment < sopts->min_alignment
|| (dopts->alignment & (dopts->alignment - 1)) != 0)) { || (dopts->alignment & (dopts->alignment - 1)) != 0)) {
goto label_invalid_alignment; goto label_invalid_alignment;
@ -2068,6 +2069,11 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
<= SC_LARGE_MAXCLASS); <= SC_LARGE_MAXCLASS);
} }
} else { } else {
if (sopts->bump_empty_aligned_alloc) {
if (unlikely(size == 0)) {
size = 1;
}
}
usize = sz_sa2u(size, dopts->alignment); usize = sz_sa2u(size, dopts->alignment);
dopts->usize = usize; dopts->usize = usize;
if (unlikely(usize == 0 if (unlikely(usize == 0
@ -2075,6 +2081,10 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
goto label_oom; goto label_oom;
} }
} }
/* Validate the user input. */
if (sopts->assert_nonempty_alloc) {
assert (size != 0);
}
check_entry_exit_locking(tsd_tsdn(tsd)); check_entry_exit_locking(tsd_tsdn(tsd));
@ -2390,6 +2400,7 @@ je_posix_memalign(void **memptr, size_t alignment, size_t size) {
static_opts_init(&sopts); static_opts_init(&sopts);
dynamic_opts_init(&dopts); dynamic_opts_init(&dopts);
sopts.bump_empty_aligned_alloc = true;
sopts.min_alignment = sizeof(void *); sopts.min_alignment = sizeof(void *);
sopts.oom_string = sopts.oom_string =
"<jemalloc>: Error allocating aligned memory: out of memory\n"; "<jemalloc>: Error allocating aligned memory: out of memory\n";
@ -2430,6 +2441,7 @@ je_aligned_alloc(size_t alignment, size_t size) {
static_opts_init(&sopts); static_opts_init(&sopts);
dynamic_opts_init(&dopts); dynamic_opts_init(&dopts);
sopts.bump_empty_aligned_alloc = true;
sopts.null_out_result_on_error = true; sopts.null_out_result_on_error = true;
sopts.set_errno_on_error = true; sopts.set_errno_on_error = true;
sopts.min_alignment = 1; sopts.min_alignment = 1;

View File

@ -85,9 +85,10 @@ TEST_BEGIN(test_alignment_and_size) {
alignment <= MAXALIGN; alignment <= MAXALIGN;
alignment <<= 1) { alignment <<= 1) {
total = 0; total = 0;
for (size = 1; for (size = 0;
size < 3 * alignment && size < (1U << 31); size < 3 * alignment && size < (1U << 31);
size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) { size += ((size == 0) ? 1 :
(alignment >> (LG_SIZEOF_PTR-1)) - 1)) {
for (i = 0; i < NITER; i++) { for (i = 0; i < NITER; i++) {
err = posix_memalign(&ps[i], err = posix_memalign(&ps[i],
alignment, size); alignment, size);