Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Custom renderer segfaults on empty codespan #67

Closed
mlinhard opened this issue Nov 29, 2018 · 5 comments
Closed

Custom renderer segfaults on empty codespan #67

mlinhard opened this issue Nov 29, 2018 · 5 comments

Comments

@mlinhard
Copy link

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
Copy link
Author

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
Copy link

kingclowndean commented Nov 29, 2018 via email

@FSX
Copy link
Owner

FSX commented Dec 1, 2018

Thanks!

Fixed: 25ad527

@FSX FSX closed this as completed Dec 1, 2018
@lepture
Copy link
Contributor

lepture commented Dec 2, 2018

@FSX is there a bug fix release?

@FSX
Copy link
Owner

FSX commented Dec 2, 2018

@lepture Just now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants