Fix conf_handle_char_p zero-sized dest and remove unused conf_handle_unsigned

This commit is contained in:
Slobodan Predolac 2026-03-27 12:27:00 -07:00
parent 3ac9f96158
commit b507644cb0
3 changed files with 27 additions and 93 deletions

View file

@ -9,19 +9,15 @@ void malloc_abort_invalid_conf(void);
#ifdef JEMALLOC_JET
extern bool had_conf_error;
bool conf_next(char const **opts_p, char const **k_p, size_t *klen_p,
char const **v_p, size_t *vlen_p);
void conf_error(const char *msg, const char *k, size_t klen,
const char *v, size_t vlen);
void conf_error(
const char *msg, const char *k, size_t klen, const char *v, size_t vlen);
bool conf_handle_bool(const char *v, size_t vlen, bool *result);
bool conf_handle_unsigned(const char *v, size_t vlen,
uintmax_t min, uintmax_t max, bool check_min, bool check_max,
bool clip, uintmax_t *result);
bool conf_handle_signed(const char *v, size_t vlen,
intmax_t min, intmax_t max, bool check_min, bool check_max,
bool clip, intmax_t *result);
bool conf_handle_char_p(const char *v, size_t vlen,
char *dest, size_t dest_sz);
bool conf_handle_signed(const char *v, size_t vlen, intmax_t min, intmax_t max,
bool check_min, bool check_max, bool clip, intmax_t *result);
bool conf_handle_char_p(const char *v, size_t vlen, char *dest, size_t dest_sz);
#endif
#endif /* JEMALLOC_INTERNAL_CONF_H */

View file

@ -254,36 +254,8 @@ JEMALLOC_DIAGNOSTIC_PUSH
JEMALLOC_DIAGNOSTIC_IGNORE("-Wunused-function")
JET_EXTERN bool
conf_handle_unsigned(const char *v, size_t vlen,
uintmax_t min, uintmax_t max, bool check_min, bool check_max,
bool clip, uintmax_t *result) {
char *end;
set_errno(0);
uintmax_t mv = (uintmax_t)malloc_strtoumax(v, &end, 0);
if (get_errno() != 0 || (uintptr_t)end - (uintptr_t)v != vlen) {
return true;
}
if (clip) {
if (check_min && mv < min) {
*result = min;
} else if (check_max && mv > max) {
*result = max;
} else {
*result = mv;
}
} else {
if ((check_min && mv < min) || (check_max && mv > max)) {
return true;
}
*result = mv;
}
return false;
}
JET_EXTERN bool
conf_handle_signed(const char *v, size_t vlen,
intmax_t min, intmax_t max, bool check_min, bool check_max,
bool clip, intmax_t *result) {
conf_handle_signed(const char *v, size_t vlen, intmax_t min, intmax_t max,
bool check_min, bool check_max, bool clip, intmax_t *result) {
char *end;
set_errno(0);
intmax_t mv = (intmax_t)malloc_strtoumax(v, &end, 0);
@ -309,6 +281,9 @@ conf_handle_signed(const char *v, size_t vlen,
JET_EXTERN bool
conf_handle_char_p(const char *v, size_t vlen, char *dest, size_t dest_sz) {
if (dest_sz == 0) {
return false;
}
size_t cpylen = (vlen <= dest_sz - 1) ? vlen : dest_sz - 1;
strncpy(dest, v, cpylen);
dest[cpylen] = '\0';
@ -473,11 +448,11 @@ malloc_conf_init_helper(sc_data_t *sc_data, unsigned bin_shard_sizes[SC_NBINS],
continue;
}
while (*opts != '\0'
&& !conf_next(&opts, &k, &klen, &v, &vlen)) {
while (
*opts != '\0' && !conf_next(&opts, &k, &klen, &v, &vlen)) {
#define CONF_ERROR(msg, k, klen, v, vlen) \
if (!initial_call) { \
conf_error(msg, k, klen, v, vlen); \
conf_error(msg, k, klen, v, vlen); \
cur_opt_valid = false; \
}
#define CONF_CONTINUE \

View file

@ -25,50 +25,6 @@ TEST_BEGIN(test_conf_handle_bool_invalid) {
}
TEST_END
TEST_BEGIN(test_conf_handle_unsigned_in_range) {
uintmax_t result = 0;
bool err = conf_handle_unsigned(
"100", sizeof("100") - 1, 1, 2048, true, true, true, &result);
expect_false(err, "Should succeed for in-range value");
expect_u64_eq((uint64_t)result, 100, "result should be 100");
}
TEST_END
TEST_BEGIN(test_conf_handle_unsigned_clip_max) {
uintmax_t result = 0;
bool err = conf_handle_unsigned(
"9999", sizeof("9999") - 1, 1, 2048, true, true, true, &result);
expect_false(err, "Should succeed with clipping");
expect_u64_eq(
(uint64_t)result, 2048, "result should be clipped to max 2048");
}
TEST_END
TEST_BEGIN(test_conf_handle_unsigned_clip_min) {
uintmax_t result = 0;
bool err = conf_handle_unsigned(
"0", sizeof("0") - 1, 1, 2048, true, true, true, &result);
expect_false(err, "Should succeed with clipping");
expect_u64_eq((uint64_t)result, 1, "result should be clipped to min 1");
}
TEST_END
TEST_BEGIN(test_conf_handle_unsigned_no_clip_reject) {
uintmax_t result = 0;
bool err = conf_handle_unsigned(
"9999", sizeof("9999") - 1, 1, 2048, true, true, false, &result);
expect_true(err, "Should fail for out-of-range value without clip");
}
TEST_END
TEST_BEGIN(test_conf_handle_unsigned_invalid) {
uintmax_t result = 0;
bool err = conf_handle_unsigned(
"abc", sizeof("abc") - 1, 1, 2048, true, true, true, &result);
expect_true(err, "Should fail for non-numeric input");
}
TEST_END
TEST_BEGIN(test_conf_handle_signed_valid) {
intmax_t result = 0;
bool err = conf_handle_signed("5000", sizeof("5000") - 1, -1,
@ -113,14 +69,21 @@ TEST_BEGIN(test_conf_handle_char_p) {
}
TEST_END
TEST_BEGIN(test_conf_handle_char_p_zero_dest_sz) {
char buf[4] = {'X', 'Y', 'Z', '\0'};
bool err;
err = conf_handle_char_p("abc", sizeof("abc") - 1, buf, 0);
expect_false(err, "Should succeed for zero-sized destination");
expect_c_eq(buf[0], 'X', "Zero-sized destination must not be modified");
}
TEST_END
int
main(void) {
return test(test_conf_handle_bool_true, test_conf_handle_bool_false,
test_conf_handle_bool_invalid, test_conf_handle_unsigned_in_range,
test_conf_handle_unsigned_clip_max,
test_conf_handle_unsigned_clip_min,
test_conf_handle_unsigned_no_clip_reject,
test_conf_handle_unsigned_invalid, test_conf_handle_signed_valid,
test_conf_handle_bool_invalid, test_conf_handle_signed_valid,
test_conf_handle_signed_negative,
test_conf_handle_signed_out_of_range, test_conf_handle_char_p);
test_conf_handle_signed_out_of_range, test_conf_handle_char_p,
test_conf_handle_char_p_zero_dest_sz);
}