From ae8882b0de5925dbe0a9cc73728f230807e601b2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 15 Jan 2012 21:33:35 +0000 Subject: [PATCH] Context: do not permit access to window or buffer if null --- src/context.hh | 26 ++++++++++++++++++++------ src/main.cc | 26 +++++++++++++------------- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/context.hh b/src/context.hh index 6ad5bf73..53f4ad1b 100644 --- a/src/context.hh +++ b/src/context.hh @@ -8,15 +8,29 @@ namespace Kakoune struct Context { - Window* window; - Buffer* buffer; - Context() - : window(nullptr), buffer(nullptr) {} + : m_window(nullptr), m_buffer(nullptr) {} Context(Window& window) - : window(&window), buffer(&window.buffer()) {} + : m_window(&window), m_buffer(&window.buffer()) {} Context(Buffer& buffer) - : window(nullptr), buffer(&buffer) {} + : m_window(nullptr), m_buffer(&buffer) {} + + Buffer& buffer() const + { + if (not m_buffer) + throw runtime_error("no buffer in context"); + return *m_buffer; + } + Window& window() const + { + if (not m_window) + throw runtime_error("no window in context"); + return *m_window; + } +public: + Window* m_window; + Buffer* m_buffer; + }; } diff --git a/src/main.cc b/src/main.cc index 698fbbe8..1cd671bc 100644 --- a/src/main.cc +++ b/src/main.cc @@ -429,7 +429,7 @@ void write_buffer(const CommandParameters& params, const Context& context) if (params.size() > 1) throw wrong_argument_count(); - Buffer& buffer = context.window->buffer(); + Buffer& buffer = context.window().buffer(); std::string filename = params.empty() ? buffer.name() : params[0]; write_buffer_to_file(buffer, filename); @@ -490,15 +490,15 @@ void add_highlighter(const CommandParameters& params, const Context& context) if (params.size() < 3) throw wrong_argument_count(); - HighlighterGroup& group = context.window->get_highlighter_group(params[1]); + HighlighterGroup& group = context.window().get_highlighter_group(params[1]); HighlighterParameters highlighter_params(params.begin()+3, params.end()); - registry.add_highlighter_to_group(*context.window, group, + registry.add_highlighter_to_group(context.window(), group, params[2], highlighter_params); } else { HighlighterParameters highlighter_params(params.begin()+1, params.end()); - registry.add_highlighter_to_window(*context.window, params[0], + registry.add_highlighter_to_window(context.window(), params[0], highlighter_params); } } @@ -513,7 +513,7 @@ void rm_highlighter(const CommandParameters& params, const Context& context) if (params.size() != 1) throw wrong_argument_count(); - context.window->remove_highlighter(params[0]); + context.window().remove_highlighter(params[0]); } void add_filter(const CommandParameters& params, const Context& context) @@ -525,7 +525,7 @@ void add_filter(const CommandParameters& params, const Context& context) { FilterRegistry& registry = FilterRegistry::instance(); FilterParameters filter_params(params.begin()+1, params.end()); - registry.add_filter_to_window(*context.window, params[0], + registry.add_filter_to_window(context.window(), params[0], filter_params); } catch (runtime_error& err) @@ -539,7 +539,7 @@ void rm_filter(const CommandParameters& params, const Context& context) if (params.size() != 1) throw wrong_argument_count(); - context.window->remove_filter(params[0]); + context.window().remove_filter(params[0]); } void add_hook(const CommandParameters& params, const Context& context) @@ -907,7 +907,7 @@ void exec_string(const CommandParameters& params, { auto it = keymap.find(key); if (it != keymap.end()) - it->second(*context.window, count); + it->second(context.window(), count); count = 0; } } @@ -948,7 +948,7 @@ int main(int argc, char* argv[]) CommandManager::None, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) - { return main_context.window->complete_highlighterid(prefix, cursor_pos); } + { return main_context.window().complete_highlighterid(prefix, cursor_pos); } }); command_manager.register_command(std::vector{ "af", "addfilter" }, add_filter, CommandManager::None, @@ -959,7 +959,7 @@ int main(int argc, char* argv[]) CommandManager::None, PerArgumentCommandCompleter { [&](const std::string& prefix, size_t cursor_pos) - { return main_context.window->complete_filterid(prefix, cursor_pos); } + { return main_context.window().complete_filterid(prefix, cursor_pos); } }); command_manager.register_command(std::vector{ "hook" }, add_hook, CommandManager::IgnoreSemiColons); @@ -989,7 +989,7 @@ int main(int argc, char* argv[]) auto buffer = (argc > 1) ? open_or_create(argv[1]) : new Buffer("*scratch*", Buffer::Type::Scratch); main_context = Context(*buffer->get_or_create_window()); - draw_window(*main_context.window); + draw_window(main_context.window()); int count = 0; while(not quit_requested) { @@ -1018,8 +1018,8 @@ int main(int argc, char* argv[]) auto it = keymap.find(key); if (it != keymap.end()) { - it->second(*main_context.window, count); - draw_window(*main_context.window); + it->second(main_context.window(), count); + draw_window(main_context.window()); } count = 0; }