diff --git a/kitty/core_text.m b/kitty/core_text.m index 12ea6f489..8990a4086 100644 --- a/kitty/core_text.m +++ b/kitty/core_text.m @@ -422,7 +422,7 @@ static struct { char *buf; size_t capacity; } ft_buffer; static CFStringRef lc_as_fallback(const ListOfChars *lc) { ensure_space_for((&ft_buffer), buf, ft_buffer.buf[0], lc->count * 4 + 128, capacity, 256, false); - cell_as_utf8_for_fallback(lc, ft_buffer.buf); + cell_as_utf8_for_fallback(lc, ft_buffer.buf, ft_buffer.capacity); return CFStringCreateWithCString(NULL, ft_buffer.buf, kCFStringEncodingUTF8); } diff --git a/kitty/line.c b/kitty/line.c index ec127bd43..4875005ef 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -414,13 +414,13 @@ cell_as_unicode_for_fallback(const ListOfChars *lc, Py_UCS4 *buf, size_t sz) { } size_t -cell_as_utf8_for_fallback(const ListOfChars *lc, char *buf) { +cell_as_utf8_for_fallback(const ListOfChars *lc, char *buf, size_t sz) { char_type ch = lc->chars[0] ? lc->chars[0] : ' '; bool include_cc = true; if (ch == '\t') { ch = ' '; include_cc = false; } size_t n = encode_utf8(ch, buf); if (include_cc) { - for (unsigned i = 1; i < lc->count; i++) { + for (unsigned i = 1; i < lc->count && sz > n + 4; i++) { char_type ch = lc->chars[i]; if (ch != VS15 && ch != VS16) n += encode_utf8(ch, buf + n); } diff --git a/kitty/lineops.h b/kitty/lineops.h index 70b362e8e..f917fdb5b 100644 --- a/kitty/lineops.h +++ b/kitty/lineops.h @@ -79,7 +79,7 @@ index_type prev_char_pos(const Line *self, index_type x, index_type num); bool line_as_ansi(Line *self, ANSILineState *s, index_type start_at, index_type stop_before, char_type prefix_char, bool skip_multiline_non_zero_lines) __attribute__((nonnull)); unsigned int line_length(Line *self); size_t cell_as_unicode_for_fallback(const ListOfChars *lc, Py_UCS4 *buf, size_t sz); -size_t cell_as_utf8_for_fallback(const ListOfChars *lc, char *buf); +size_t cell_as_utf8_for_fallback(const ListOfChars *lc, char *buf, size_t sz); bool unicode_in_range(const Line *self, const index_type start, const index_type limit, const bool include_cc, const bool add_trailing_newline, const bool skip_zero_cells, bool skip_multiline_non_zero_lines, ANSIBuf*); PyObject* line_as_unicode(Line *, bool, ANSIBuf*);