Skip to content

Commit

Permalink
Fix crash in message API (#39)
Browse files Browse the repository at this point in the history
* if a method was used for the message callback, uharfbuzz would crash, as it didn't keep a reference to the callback.
  • Loading branch information
justvanrossum authored Apr 1, 2020
1 parent 71a35aa commit 2c8fc5c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/uharfbuzz/_harfbuzz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ cdef class GlyphPosition:

cdef class Buffer:
cdef hb_buffer_t* _hb_buffer
cdef object _message_callback

def __cinit__(self):
self._hb_buffer = hb_buffer_create()
if not hb_buffer_allocation_successful(self._hb_buffer):
raise MemoryError()
self._message_callback = None

def __dealloc__(self):
if self._hb_buffer is not NULL:
Expand Down Expand Up @@ -238,6 +240,7 @@ cdef class Buffer:
hb_buffer_guess_segment_properties(self._hb_buffer)

def set_message_func(self, callback) -> None:
self._message_callback = callback
hb_buffer_set_message_func(self._hb_buffer, msgcallback, <void*>callback, NULL)


Expand Down
15 changes: 15 additions & 0 deletions tests/test_uharfbuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ def message(msg):
assert advances_trace == [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
[0, 0, 0, 0, 0], [0, 0, 0, 100, 0]]

def test_message_func_crash(self, blankfont):
string = "edcba"
buf = hb.Buffer()
buf.add_str(string)
buf.guess_segment_properties()
message_collector = MessageCollector()
buf.set_message_func(message_collector.message)
hb.shape(blankfont, buf)


class MessageCollector:
def message(self, message):
pass


class TestGetBaseline:
# The test font contains a BASE table with some test values
def test_ot_layout_get_baseline_invalid_tag(self, blankfont):
Expand Down

0 comments on commit 2c8fc5c

Please sign in to comment.