From 5d92b5f6d3ecc34fcf4810d75837e0c0e1c6f64d Mon Sep 17 00:00:00 2001 From: Frank Richter Date: Sun, 19 Dec 2021 15:46:46 +0100 Subject: [PATCH 1/2] Avoid a possible infinite recursion in options parsing. See microsoft/mimalloc#502, second issue, for a scenario where this occurs. --- src/options.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/options.c b/src/options.c index 43f8fb4b..df9f3b3c 100644 --- a/src/options.c +++ b/src/options.c @@ -532,8 +532,11 @@ static void mi_option_init(mi_option_desc_t* desc) { desc->init = INITIALIZED; } else { - _mi_warning_message("environment option mimalloc_%s has an invalid value: %s\n", desc->name, buf); + /* _mi_warning_message() will itself call mi_option_get() for some options, + * so to avoid a possible infinite recursion it's important to mark the option as + * "initialized" first */ desc->init = DEFAULTED; + _mi_warning_message("environment option mimalloc_%s has an invalid value: %s\n", desc->name, buf); } } mi_assert_internal(desc->init != UNINIT); From f7c821fe7927b92212ae3d2c149c0a8b77d3845d Mon Sep 17 00:00:00 2001 From: Frank Richter Date: Sun, 19 Dec 2021 15:56:26 +0100 Subject: [PATCH 2/2] options: Always print a warning if the 'verbose' option value is bogus --- src/options.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/options.c b/src/options.c index df9f3b3c..f9e3984a 100644 --- a/src/options.c +++ b/src/options.c @@ -536,7 +536,15 @@ static void mi_option_init(mi_option_desc_t* desc) { * so to avoid a possible infinite recursion it's important to mark the option as * "initialized" first */ desc->init = DEFAULTED; + if (desc->option == mi_option_verbose) { + /* Special case: if the 'mimalloc_verbose' env var has a bogus value we'd never know + * (since the value default to 'off') - so in that one case briefly set the option to 'on' */ + desc->value = 1; + } _mi_warning_message("environment option mimalloc_%s has an invalid value: %s\n", desc->name, buf); + if (desc->option == mi_option_verbose) { + desc->value = 0; + } } } mi_assert_internal(desc->init != UNINIT);