[vte] widget: Fix crash using nullptr for word char exceptions



commit 18d9763db20dbcadd1fd825552ee5655d0f42fa5
Author: Christian Persch <chpe src gnome org>
Date:   Tue Nov 19 18:30:44 2019 +0100

    widget: Fix crash using nullptr for word char exceptions
    
    Constructing a std::string_view with nullptr works here, but it is
    undefined behaviour and does crash on other distros.

 src/vte.cc         | 7 ++-----
 src/vtegtk.cc      | 3 ++-
 src/vteinternal.hh | 2 +-
 src/widget.cc      | 8 ++++----
 src/widget.hh      | 7 ++++---
 5 files changed, 13 insertions(+), 14 deletions(-)
---
diff --git a/src/vte.cc b/src/vte.cc
index b577e517..db8371ce 100644
--- a/src/vte.cc
+++ b/src/vte.cc
@@ -10948,9 +10948,6 @@ Terminal::set_input_enabled (bool enabled)
 std::optional<std::vector<char32_t>>
 Terminal::process_word_char_exceptions(std::string_view str_view) const noexcept
 {
-        if (str_view.empty())
-                str_view = WORD_CHAR_EXCEPTIONS_DEFAULT;
-
         auto str = str_view.data();
 
         auto array = std::vector<char32_t>{};
@@ -11017,9 +11014,9 @@ Terminal::process_word_char_exceptions(std::string_view str_view) const noexcept
  * Returns: %true if the word char exceptions changed
  */
 bool
-Terminal::set_word_char_exceptions(std::string_view str)
+Terminal::set_word_char_exceptions(std::optional<std::string_view> stropt)
 {
-        if (auto array = process_word_char_exceptions(str)) {
+        if (auto array = process_word_char_exceptions(stropt ? stropt.value() : 
WORD_CHAR_EXCEPTIONS_DEFAULT)) {
                 m_word_char_exceptions = *array;
                 return true;
         }
diff --git a/src/vtegtk.cc b/src/vtegtk.cc
index 82f715ee..ccb8d0f0 100644
--- a/src/vtegtk.cc
+++ b/src/vtegtk.cc
@@ -4570,7 +4570,8 @@ vte_terminal_set_word_char_exceptions(VteTerminal *terminal,
 {
         g_return_if_fail(VTE_IS_TERMINAL(terminal));
 
-        if (WIDGET(terminal)->set_word_char_exceptions(exceptions))
+        auto stropt = exceptions ? std::make_optional<std::string_view>(exceptions) : std::nullopt;
+        if (WIDGET(terminal)->set_word_char_exceptions(stropt))
                 g_object_notify_by_pspec(G_OBJECT(terminal), pspecs[PROP_WORD_CHAR_EXCEPTIONS]);
 }
 
diff --git a/src/vteinternal.hh b/src/vteinternal.hh
index 6a17efa9..d8656785 100644
--- a/src/vteinternal.hh
+++ b/src/vteinternal.hh
@@ -1302,7 +1302,7 @@ public:
         bool set_scrollback_lines(long lines);
         bool set_scroll_on_keystroke(bool scroll);
         bool set_scroll_on_output(bool scroll);
-        bool set_word_char_exceptions(std::string_view str_view);
+        bool set_word_char_exceptions(std::optional<std::string_view> stropt);
         void set_clear_background(bool setting);
 
         bool write_contents_sync (GOutputStream *stream,
diff --git a/src/widget.cc b/src/widget.cc
index afaace7b..423c2336 100644
--- a/src/widget.cc
+++ b/src/widget.cc
@@ -385,13 +385,13 @@ Widget::set_pty(VtePty* pty_obj) noexcept
 }
 
 bool
-Widget::set_word_char_exceptions(std::string_view str)
+Widget::set_word_char_exceptions(std::optional<std::string_view> stropt)
 {
-        if (m_word_char_exceptions == str)
+        if (m_word_char_exceptions == stropt)
                 return false;
 
-        if (terminal()->set_word_char_exceptions(str)) {
-                m_word_char_exceptions = str;
+        if (terminal()->set_word_char_exceptions(stropt)) {
+                m_word_char_exceptions = stropt;
                 return true;
         }
 
diff --git a/src/widget.hh b/src/widget.hh
index 023e5e36..03c048fb 100644
--- a/src/widget.hh
+++ b/src/widget.hh
@@ -18,6 +18,7 @@
 #pragma once
 
 #include <memory>
+#include <optional>
 #include <string>
 #include <variant>
 
@@ -120,8 +121,8 @@ public:
         bool set_text_blink_mode(VteTextBlinkMode mode) { return 
terminal()->set_text_blink_mode(vte::terminal::Terminal::TextBlinkMode(mode)); }
         auto text_blink_mode() const noexcept { return VteTextBlinkMode(terminal()->text_blink_mode()); }
 
-        bool set_word_char_exceptions(std::string_view str);
-        auto word_char_exceptions() const noexcept { return m_word_char_exceptions.empty() ? nullptr : 
m_word_char_exceptions.c_str(); }
+        bool set_word_char_exceptions(std::optional<std::string_view> stropt);
+        auto word_char_exceptions() const noexcept { return m_word_char_exceptions ? 
m_word_char_exceptions.value().c_str() : nullptr; }
 
         char const* encoding() const noexcept { return m_terminal->encoding(); }
 
@@ -192,7 +193,7 @@ private:
         vte::glib::RefPtr<VtePty> m_pty;
 
         /* Misc */
-        std::string m_word_char_exceptions;
+        std::optional<std::string> m_word_char_exceptions{};
 
         vte::glib::RefPtr<GtkAdjustment> m_hadjustment{};
         uint32_t m_hscroll_policy : 1;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]