From ac9f5419653ea017eead9ad9ffb4770f4939cde9 Mon Sep 17 00:00:00 2001 From: Bakkeby Date: Tue, 5 Jul 2022 15:32:06 +0200 Subject: [PATCH] [st][patch][ligatures] fix potential buffer overflow in shaping code ref. https://git.suckless.org/sites/commit/05a0d9b6bf500a7b2955c4299a1912eb302ce40b.html --- hb.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/hb.c b/hb.c index f222445..7bf3768 100644 --- a/hb.c +++ b/hb.c @@ -1,12 +1,16 @@ #include #include #include -#include #include +#include +#include #include #include + #include "st.h" +#define FEATURE(c1,c2,c3,c4) { .tag = HB_TAG(c1,c2,c3,c4), .value = 1, .start = HB_FEATURE_GLOBAL_START, .end = HB_FEATURE_GLOBAL_END } + void hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoints, int start, int length); hb_font_t *hbfindfont(XftFont *match); @@ -18,6 +22,13 @@ typedef struct { static int hbfontslen = 0; static HbFontMatch *hbfontcache = NULL; +/* + * Poplulate the array with a list of font features, wrapped in FEATURE macro, + * e. g. + * FEATURE('c', 'a', 'l', 't'), FEATURE('d', 'l', 'i', 'g') + */ +hb_feature_t features[] = { }; + void hbunloadfonts() { @@ -59,7 +70,7 @@ void hbtransform(XftGlyphFontSpec *specs, const Glyph *glyphs, size_t len, int x, int y) { int start = 0, length = 1, gstart = 0; - hb_codepoint_t *codepoints = calloc(len, sizeof(hb_codepoint_t)); + hb_codepoint_t *codepoints = calloc((unsigned int)len, sizeof(hb_codepoint_t)); for (int idx = 1, specidx = 1; idx < len; idx++) { if (glyphs[idx].mode & ATTR_WDUMMY) { @@ -127,7 +138,7 @@ hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoin } /* Shape the segment. */ - hb_shape(font, buffer, NULL, 0); + hb_shape(font, buffer, features, sizeof(features)/sizeof(hb_feature_t)); /* Get new glyph info. */ hb_glyph_info_t *info = hb_buffer_get_glyph_infos(buffer, NULL); @@ -140,4 +151,4 @@ hbtransformsegment(XftFont *xfont, const Glyph *string, hb_codepoint_t *codepoin /* Cleanup. */ hb_buffer_destroy(buffer); -} \ No newline at end of file +}