Fix segfault when adding an invalid default-region highlighter

RegionsHighlighter::create_region() validates the highlighter type argument
but RegionsHighlighter::create_default_region() assumes it is correct,
segfaulting from dereferencing a null pointer if the given type isn't in
the highlighter registry HashMap.

@PJungkamp reported this in https://github.com/mawww/kakoune/issues/4959
with a simple recipe to reproduce:

    :add-highlighter shared/test regions
    :add-highlighter shared/test/ default-region invalid highlighter
    :add-highlighter window/test ref test

Validate the type argument in RegionsHighlighter::create_default_region()
in the same way as RegionsHighlighter::create_region().
main
Chris Webb 2023-08-15 16:22:39 +01:00
parent 6942a4c0c9
commit 491d4d47ae
1 changed files with 7 additions and 1 deletions

View File

@ -2044,7 +2044,13 @@ public:
static const ParameterDesc param_desc{ {}, ParameterDesc::Flags::SwitchesOnlyAtStart, 1 };
ParametersParser parser{params, param_desc};
auto delegate = HighlighterRegistry::instance()[parser[0]].factory(parser.positionals_from(1), nullptr);
const auto& type = parser[0];
auto& registry = HighlighterRegistry::instance();
auto it = registry.find(type);
if (it == registry.end())
throw runtime_error(format("no such highlighter type: '{}'", type));
auto delegate = it->value.factory(parser.positionals_from(1), nullptr);
return std::make_unique<RegionHighlighter>(std::move(delegate));
}