Commit 53f1096b authored by IdiocyInAction's avatar IdiocyInAction

Tried fixing memory leak

parent e980ab3b
"""Functions/constants related to markdown handling."""
from ctypes import CDLL, c_char_p, c_long
from ctypes import CDLL, c_char_p, c_long, c_void_p, cast
import re
from typing import (
Callable,
......@@ -30,8 +30,11 @@ from tildes.schemas.user import is_valid_username
# set up the commonmark function to call into libcmark-gfm
CMARK_DLL = CDLL('/usr/local/lib/libcmark-gfm.so')
commonmark = CMARK_DLL.cmark_markdown_to_html # pylint: disable=invalid-name
commonmark.restype = c_char_p
commonmark.restype = c_void_p
commonmark.argtypes = [c_char_p, c_long, c_long]
LIBC_DLL = CDLL('libc.so.6')
free = LIBC_DLL.free
free.argtypes = [c_void_p]
HTML_TAG_WHITELIST = (
......@@ -118,13 +121,20 @@ def convert_markdown_to_safe_html(markdown: str) -> str:
html_bytes = commonmark(markdown_bytes, len(markdown_bytes), cmark_options)
html = html_bytes.decode('utf8')
html = cast(html_bytes, c_char_p).value
if html:
htmlDecoded = html.decode('utf8')
# apply custom post-processing to HTML
html = postprocess_markdown_html(html)
# apply custom post-processing to HTML
htmlDecoded = postprocess_markdown_html(htmlDecoded)
# sanitize the final HTML before returning it
return sanitize_html(html)
# Buffer shouldn't be needed anymore
free(html_bytes)
# sanitize the final HTML before returning it
return sanitize_html(htmlDecoded)
else:
return ""
def preprocess_markdown(markdown: str) -> str:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment