From 701102d44b813ac5d6b8c6e9673c8b74a4109f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Sat, 28 Jan 2017 23:26:42 +0100 Subject: [PATCH] fix(cairo): Fix font prioritization In the previous implementation, std::find() returned fns.end() several times, which caused an "Address boundary error" in std::sort if the preferred font-index was set to m_fonts.size() + 1. This commit reimplements the font prioritization with a simple swap. Reproduction steps: - Start polybar with the following config: [bar/top] font-0 = NotoSans-Regular:size=8;0 font-1 = MaterialIcons:size=10;0 modules-left = date [module/date] type = internal/date date = %Y-%m-%d label-font = 3 ; invalid index --- include/cairo/context.hpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/include/cairo/context.hpp b/include/cairo/context.hpp index 75ef4636..98623d38 100644 --- a/include/cairo/context.hpp +++ b/include/cairo/context.hpp @@ -145,16 +145,12 @@ namespace cairo { double x, y; position(&x, &y); - // Sort the fontlist so that the - // preferred font gets prioritized + // Prioritize the preferred font vector> fns(m_fonts.begin(), m_fonts.end()); - std::sort(fns.begin(), fns.end(), [&](const shared_ptr& a, const shared_ptr&) { - if (t.font > 0 && std::distance(fns.begin(), std::find(fns.begin(), fns.end(), a)) == t.font - 1) { - return -1; - } else { - return 0; - } - }); + + if (t.font > 0 && t.font <= std::distance(fns.begin(), fns.end())) { + std::iter_swap(fns.begin(), fns.begin() + t.font - 1); + } string utf8 = string(t.contents); utils::unicode_charlist chars;