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 for Font(fontbuffer=object()) #838

Closed
user202729 opened this issue Jan 17, 2021 · 7 comments
Closed

Segmentation fault for Font(fontbuffer=object()) #838

user202729 opened this issue Jan 17, 2021 · 7 comments
Assignees
Labels

Comments

@user202729
Copy link
Contributor

user202729 commented Jan 17, 2021

Please provide all mandatory information!

Describe the bug (mandatory)

fitz.Font(fontbuffer=object()) causes a segmentation fault.

To Reproduce (mandatory)

See above.

Expected behavior (optional)

It should raise a TypeError or something.

Your configuration (mandatory)

Python 3.9.1, wheel.

Additional context (optional)

This is not how the library is supposed to be used, but mistakes can happen (for example pass a fontTools.TTFont object in), and segmentation faults are annoying in Python (normally it would print a traceback and let the user catch it)

To generalize, perhaps there should be some way to make the functions that take AnyType arguments safe from user errors?

@user202729
Copy link
Contributor Author

user202729 commented Jan 17, 2021

Looks like that normally there's a check, but in this case it's missing.

The straight forward fix would be

--- a/fitz/helper-stext.i
+++ b/fitz/helper-stext.i
@@ -830,6 +830,7 @@ fz_font *JM_get_font(fz_context *ctx,
         // fontbuffer
         have_buffer:;
         res = JM_BufferFromBytes(ctx, fontbuffer);
+        if (!res) THROWMSG(gctx, "bad type: 'fontbuffer'");
         font = fz_new_font_from_buffer(ctx, NULL, res, index, 0);
         goto fertig;

but in this case it raises a warning (also is there a memory leak?)


Searching a bit in the source code, there are some other instances that the result is not checked against null value. (twice in Document.insertImage, once in Tools.insert_contents)

@JorjMcKie
Copy link
Collaborator

Thanks for spotting this.
Yes, this case is a left-over from the time when the Font class was introduced.
I have already corrected it in my local version.

@JorjMcKie
Copy link
Collaborator

I usually use a macro EXISTS, which checks for NULL and Python-True.

@malizheng
Copy link

I also encountered a similar error. When encountering this "Segmentation fault: 11" exception, "try: except:" cannot recognize this exception, and the program will exit abnormally. Is there any other way to identify this kind of exception in python?

@JorjMcKie
Copy link
Collaborator

Is there any other way to identify this kind of exception in python?

No - and that exactly is the problem - the Python interpreter itself is brought down.
For every such incident, please submit an issue and material to reproduce the error.
I am able to test stuff under Windows and Linux (only) - not MAC OSX.

@user202729
Copy link
Contributor Author

user202729 commented Jan 25, 2021

There's faulthandler to print a traceback on segmentation fault -- nevertheless, it's never possible to catch the error.

Also, since this error is not fixed (in the newest version on GitHub/pypi), it might be the same (or related) error.

Searching a bit in the source code, there are some other instances that the result is not checked against null value. (twice in Document.insertImage, once in Tools.insert_contents)

@JorjMcKie
Copy link
Collaborator

Fixed by v1.18.7, currently being uploaded.

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

No branches or pull requests

3 participants