From f9d421130fc4b8daaae437e97127310f341d78f2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 5 Mar 2019 20:31:40 +1100 Subject: [PATCH] Fix use of invalidated iterators in highlight_range Fixes #2755 --- src/highlighters.cc | 14 +++++++------- .../2755-segfault-on-double-wrap-highlighters/cmd | 1 + .../2755-segfault-on-double-wrap-highlighters/in | 2 ++ .../2755-segfault-on-double-wrap-highlighters/rc | 5 +++++ 4 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 test/regression/2755-segfault-on-double-wrap-highlighters/cmd create mode 100644 test/regression/2755-segfault-on-double-wrap-highlighters/in create mode 100644 test/regression/2755-segfault-on-double-wrap-highlighters/rc diff --git a/src/highlighters.cc b/src/highlighters.cc index 05bc10e7..6a6fb8e8 100644 --- a/src/highlighters.cc +++ b/src/highlighters.cc @@ -133,7 +133,7 @@ void apply_highlighter(HighlightContext context, using LineIterator = DisplayBuffer::LineList::iterator; LineIterator first_line; - Vector insert_pos; + Vector insert_idx; auto line_end = display_buffer.lines().end(); DisplayBuffer region_display; @@ -147,8 +147,6 @@ void apply_highlighter(HighlightContext context, if (region_lines.empty()) first_line = line_it; - region_lines.emplace_back(); - insert_pos.emplace_back(); if (range.begin < begin or range.end > end) { @@ -184,14 +182,16 @@ void apply_highlighter(HighlightContext context, } } } + region_lines.emplace_back(); std::move(line.begin() + beg_idx, line.begin() + end_idx, std::back_inserter(region_lines.back())); - insert_pos.back() = line.erase(line.begin() + beg_idx, line.begin() + end_idx); + auto it = line.erase(line.begin() + beg_idx, line.begin() + end_idx); + insert_idx.push_back(it - line.begin()); } else { - region_lines.back() = std::move(line); - insert_pos.back() = line.begin(); + insert_idx.push_back(0); + region_lines.push_back(std::move(line)); } } @@ -204,7 +204,7 @@ void apply_highlighter(HighlightContext context, for (size_t i = 0; i < region_lines.size(); ++i) { auto& line = *(first_line + i); - auto pos = insert_pos[i]; + auto pos = line.begin() + insert_idx[i]; for (auto& atom : region_lines[i]) pos = ++line.insert(pos, std::move(atom)); } diff --git a/test/regression/2755-segfault-on-double-wrap-highlighters/cmd b/test/regression/2755-segfault-on-double-wrap-highlighters/cmd new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/regression/2755-segfault-on-double-wrap-highlighters/cmd @@ -0,0 +1 @@ + diff --git a/test/regression/2755-segfault-on-double-wrap-highlighters/in b/test/regression/2755-segfault-on-double-wrap-highlighters/in new file mode 100644 index 00000000..7f5107d4 --- /dev/null +++ b/test/regression/2755-segfault-on-double-wrap-highlighters/in @@ -0,0 +1,2 @@ +If you're not reporting a bug (e.g. requesting a feature or asking a general question), feel free to remove this template. +Otherwise, please edit the following sections with the relevant information (). diff --git a/test/regression/2755-segfault-on-double-wrap-highlighters/rc b/test/regression/2755-segfault-on-double-wrap-highlighters/rc new file mode 100644 index 00000000..837158ff --- /dev/null +++ b/test/regression/2755-segfault-on-double-wrap-highlighters/rc @@ -0,0 +1,5 @@ +add-highlighter buffer/regions regions +add-highlighter buffer/regions/inline default-region regions + +add-highlighter buffer/ wrap +add-highlighter buffer/ wrap -indent