Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation Fault with Empty Markdown Heading #38

Closed
AstraBert opened this issue Jan 2, 2025 · 3 comments · Fixed by #42
Closed

Segmentation Fault with Empty Markdown Heading #38

AstraBert opened this issue Jan 2, 2025 · 3 comments · Fixed by #42

Comments

@AstraBert
Copy link

Description:
Hello!

First, thank you for this fantastic tool 🥰.

I encountered a segmentation fault when using my tool, which is built on top of markdown-pdf. The issue arises when converting a Markdown file containing an empty heading (line 59). I’ve attached the file for reference.

While I understand that this edge case is rare, I wanted my tool to handle such situations seamlessly, so I investigated further and identified the root cause in your code.


Steps to Reproduce:

  1. Use the attached Markdown file: line.md.
  2. Attempt to convert it using markdown-pdf.

Observed Behavior:
A segmentation fault occurs when processing the file.


Analysis:
The issue is triggered at the following line during the second iteration of the while loop in the add_section method:

story.element_positions(self._recorder, {"toc": section.toc, "pdfile": self})

Here's the relevant code snippet for context:

def add_section(self, section: Section, user_css: typing.Optional[str] = None) -> None:
    """Add markdown section to pdf."""
    rect = fitz.paper_rect(section.paper_size)
    print("Rect ok!")
    where = rect + section.borders
    print("Where ok!")
    story = fitz.Story(html=self.m_d.render(section.text), archive=section.root, user_css=user_css)
    print("Story ok!")
    more = 1
    while more:  # loop outputting the story
        self.page += 1
        device = self.writer.begin_page(rect)
        print("Device ok!")
        more, _ = story.place(where)  # layout into allowed rectangle
        print("Place ok!")
        >>>story.element_positions(self._recorder, {"toc": section.toc, "pdfile": self})
        print("Positions ok!")
        story.draw(device)
        print("Device ok!")
        self.writer.end_page()
        print("End page ok!")

(Note: The print statements were added for debugging purposes.)


Root Cause:
The segmentation fault seems to be related to the handling of empty headings in Markdown.


Proposed Solution:
Upgrading the PyMuPDF dependency resolves the issue. Specifically:


Suggestion:
Please consider updating your PyMuPDF dependency to version >= 1.24.6.


Thank you for your time, and let me know if I can provide further details! 😊

@vb64
Copy link
Owner

vb64 commented Jan 8, 2025

Thank you for your report. You are an excellent QA :)

@vb64 vb64 closed this as completed in #42 Jan 8, 2025
@vb64
Copy link
Owner

vb64 commented Jan 8, 2025

Fixed in version 1.3.3. Please, update your installation.

@AstraBert
Copy link
Author

Thank you so much!

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

Successfully merging a pull request may close this issue.

2 participants