fix(bar): use std::string for mode names

`string_view` leads to UAF when reading custom mode definitions from the
configuration.
This commit is contained in:
Aleksei Bavshin 2024-02-19 01:50:40 -08:00
parent 3cb587945a
commit 4420447e74
No known key found for this signature in database
GPG Key ID: 4F071603387A382A
2 changed files with 8 additions and 8 deletions

View File

@ -71,16 +71,16 @@ class BarSurface {
class Bar { class Bar {
public: public:
using bar_mode_map = std::map<std::string_view, struct bar_mode>; using bar_mode_map = std::map<std::string, struct bar_mode>;
static const bar_mode_map PRESET_MODES; static const bar_mode_map PRESET_MODES;
static const std::string_view MODE_DEFAULT; static const std::string MODE_DEFAULT;
static const std::string_view MODE_INVISIBLE; static const std::string MODE_INVISIBLE;
Bar(struct waybar_output *w_output, const Json::Value &); Bar(struct waybar_output *w_output, const Json::Value &);
Bar(const Bar &) = delete; Bar(const Bar &) = delete;
~Bar(); ~Bar();
void setMode(const std::string_view &); void setMode(const std::string &mode);
void setVisible(bool visible); void setVisible(bool visible);
void toggle(); void toggle();
void handleSignal(int); void handleSignal(int);

View File

@ -54,8 +54,8 @@ const Bar::bar_mode_map Bar::PRESET_MODES = { //
.passthrough = true, .passthrough = true,
.visible = true}}}; .visible = true}}};
const std::string_view Bar::MODE_DEFAULT = "default"; const std::string Bar::MODE_DEFAULT = "default";
const std::string_view Bar::MODE_INVISIBLE = "invisible"; const std::string Bar::MODE_INVISIBLE = "invisible";
const std::string_view DEFAULT_BAR_ID = "bar-0"; const std::string_view DEFAULT_BAR_ID = "bar-0";
/* Deserializer for enum bar_layer */ /* Deserializer for enum bar_layer */
@ -117,7 +117,7 @@ Glib::ustring to_string(Gtk::PositionType pos) {
* Assumes that all the values in the object are deserializable to the same type. * Assumes that all the values in the object are deserializable to the same type.
*/ */
template <typename Key, typename Value, template <typename Key, typename Value,
typename = std::enable_if_t<std::is_convertible<std::string_view, Key>::value>> typename = std::enable_if_t<std::is_convertible<std::string, Key>::value>>
void from_json(const Json::Value& j, std::map<Key, Value>& m) { void from_json(const Json::Value& j, std::map<Key, Value>& m) {
if (j.isObject()) { if (j.isObject()) {
for (auto it = j.begin(); it != j.end(); ++it) { for (auto it = j.begin(); it != j.end(); ++it) {
@ -409,7 +409,7 @@ waybar::Bar::Bar(struct waybar_output* w_output, const Json::Value& w_config)
/* Need to define it here because of forward declared members */ /* Need to define it here because of forward declared members */
waybar::Bar::~Bar() = default; waybar::Bar::~Bar() = default;
void waybar::Bar::setMode(const std::string_view& mode) { void waybar::Bar::setMode(const std::string& mode) {
using namespace std::literals::string_literals; using namespace std::literals::string_literals;
auto style = window.get_style_context(); auto style = window.get_style_context();