Fix overflow in prng_range().

Add jemalloc_ffs64() and use it instead of jemalloc_ffsl() in
prng_range(), since long is not guaranteed to be a 64-bit type.
This commit is contained in:
Jason Evans
2016-02-20 23:41:33 -08:00
parent aac93f414e
commit ecae12323d
5 changed files with 40 additions and 6 deletions

View File

@@ -189,9 +189,10 @@
#undef JEMALLOC_TLS
/*
* ffs()/ffsl() functions to use for bitmapping. Don't use these directly;
* instead, use jemalloc_ffs() or jemalloc_ffsl() from util.h.
* ffs*() functions to use for bitmapping. Don't use these directly; instead,
* use jemalloc_ffs*() from util.h.
*/
#undef JEMALLOC_INTERNAL_FFSLL
#undef JEMALLOC_INTERNAL_FFSL
#undef JEMALLOC_INTERNAL_FFS
@@ -241,6 +242,9 @@
/* sizeof(long) == 2^LG_SIZEOF_LONG. */
#undef LG_SIZEOF_LONG
/* sizeof(long long) == 2^LG_SIZEOF_LONG_LONG. */
#undef LG_SIZEOF_LONG_LONG
/* sizeof(intmax_t) == 2^LG_SIZEOF_INTMAX_T. */
#undef LG_SIZEOF_INTMAX_T

View File

@@ -292,6 +292,9 @@ isqalloc
isthreaded
ivsalloc
ixalloc
jemalloc_ffs
jemalloc_ffs64
jemalloc_ffsl
jemalloc_postfork_child
jemalloc_postfork_parent
jemalloc_prefork

View File

@@ -64,7 +64,7 @@ prng_range(uint64_t *state, uint64_t range)
assert(range > 1);
/* Compute the ceiling of lg(range). */
lg_range = jemalloc_ffsl(pow2_ceil_u64(range)) - 1;
lg_range = jemalloc_ffs64(pow2_ceil_u64(range)) - 1;
/* Generate a result in [0..range) via repeated trial. */
do {

View File

@@ -121,6 +121,7 @@ void malloc_printf(const char *format, ...) JEMALLOC_FORMAT_PRINTF(1, 2);
#ifdef JEMALLOC_H_INLINES
#ifndef JEMALLOC_ENABLE_INLINE
int jemalloc_ffs64(uint64_t bitmap);
int jemalloc_ffsl(long bitmap);
int jemalloc_ffs(int bitmap);
uint64_t pow2_ceil_u64(uint64_t x);
@@ -134,10 +135,24 @@ int get_errno(void);
#if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_UTIL_C_))
/* Sanity check. */
#if !defined(JEMALLOC_INTERNAL_FFSL) || !defined(JEMALLOC_INTERNAL_FFS)
# error Both JEMALLOC_INTERNAL_FFSL && JEMALLOC_INTERNAL_FFS should have been defined by configure
#if !defined(JEMALLOC_INTERNAL_FFSLL) || !defined(JEMALLOC_INTERNAL_FFSL) \
|| !defined(JEMALLOC_INTERNAL_FFS)
# error JEMALLOC_INTERNAL_FFS{,L,LL} should have been defined by configure
#endif
JEMALLOC_ALWAYS_INLINE int
jemalloc_ffs64(uint64_t bitmap)
{
#if LG_SIZEOF_LONG == 3
return (JEMALLOC_INTERNAL_FFSL(bitmap));
#elif LG_SIZEOF_LONG_LONG == 3
return (JEMALLOC_INTERNAL_FFSLL(bitmap));
#else
#error No implementation for 64-bit ffs()
#endif
}
JEMALLOC_ALWAYS_INLINE int
jemalloc_ffsl(long bitmap)
{