FSX / misaka

A Python binding for Hoedown.
http://misaka.61924.nl
MIT License
420 stars 65 forks source link

Custom renderer segfaults on empty codespan #67

Closed mlinhard closed 5 years ago

mlinhard commented 5 years ago

If I create a custom renderer (e.g. one that converts codespans to simple brackets) and call it on input with empty codespan it segfaults:

from misaka.api import Markdown, BaseRenderer

class MyRenderer(BaseRenderer):
    def __init__(self):
        BaseRenderer.__init__(self)

    def codespan(self, text):
        return "(" + text + ")"

    def normal_text(self, text):
        return text

    def paragraph(self, content):
        return content

md = Markdown(MyRenderer())
print(md("` `"))

The problem is here:

@ffi.callback('int(hoedown_buffer *ob, const hoedown_buffer *text, '
              '    const hoedown_renderer_data *data)')
def cb_codespan(ob, text, data):
    renderer = ffi.from_handle(lib.misaka_get_renderer(data))
    text = ffi.string(text.data, text.size).decode('utf-8')
    result = renderer.codespan(text)
    if result:
        lib.hoedown_buffer_puts(ob, result.encode('utf-8'))
        return 1
    return 0

The text will be a NULL CData object and dereferencing text.data attribute will segfault.

I worked around it with ffi.NULL check

mlinhard commented 5 years ago

Workaround fix in callbacks.py: Replace line

    text = ffi.string(text.data, text.size).decode('utf-8')

with

    text = " " if text == ffi.NULL else ffi.string(text.data, text.size).decode('utf-8')
kingclowndean commented 5 years ago

Bonjour souple

Le 29 nov. 2018 6:30 AM, "Michal Linhard" notifications@github.com a écrit :

Workaround fix in callbacks.py: Replace line

text = ffi.string(text.data, text.size).decode('utf-8')

with

text = " " if text == ffi.NULL else ffi.string(text.data, text.size).decode('utf-8')

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/FSX/misaka/issues/67#issuecomment-442833983, or mute the thread https://github.com/notifications/unsubscribe-auth/ACpx6A0aNEtPeMUCFi2iADZlVRzWkXVAks5uz-F_gaJpZM4Y5qEN .

FSX commented 5 years ago

Thanks!

Fixed: 25ad527a266fd7df221ba8a0a3bdde7dd75edaf3

lepture commented 5 years ago

@FSX is there a bug fix release?

FSX commented 5 years ago

@lepture Just now.