-
Notifications
You must be signed in to change notification settings - Fork 162
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
Fix #63: do not panic into C #91
Conversation
src/engines/mod.rs
Outdated
_ => { | ||
tt_warning!(es.status, "Unexpected \"whence\" parameter to fseek() wrapper: {}", | ||
whence); | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make more sense to change input_seek
to return a signed value. fseek
returns -1
and set errno
to EINVAL
when there is an invalid whence
provided. I think it would make sense to return -EINVAL
(e.g., ssize_t
is signed). A negative value so that the caller knows there is an error and its value set to an error to know the reason if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion. Unfortunately, as far as can tell, the semantics of input_seek
are not clear. It is not really a wrapper around fseek
, so that message is a bit misleading. Looking at the history, it was used to replace various methods, mostly rewind
, which does not report errors. In fact, the code does not check the return value, except at two places in dpx-pdfobj.c
like this inefficient looking search for the beginning of the line. There it means the current position. I agree that it would be definitely better to abort the engine and report a proper error. I think that it will need a more serious refactoring of the bridge code. This commit at least fixes the undefined behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, maybe add a TODO or an issue to keep track of that? We should handle normal errors from seek and also the case of an unknown whence
parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I have also moved the error conversion from ExecutionState::input_seek
to input_seek
. I believe it is better to keep the conversion "Rust error" -> "C error" all in one place in the bridge function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, thanks! Besides the small comments I had, can you change the tt_warning!
macros to tt_error!
calls?
src/engines/mod.rs
Outdated
@@ -383,7 +377,9 @@ impl<'a, I: 'a + IoProvider> ExecutionState<'a, I> { | |||
} | |||
} | |||
|
|||
panic!("unexpected handle {:?}", handle); | |||
tt_warning!(self.status, "input close failed: unexpected handle {:?}", handle); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add statement making it clearer that this is an internal bug? Something like "serious internal bug: unexpected handle in input close".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want me to add "serious internal bug" only to this particular error, or to others as well?
src/engines/mod.rs
Outdated
_ => panic!("Unexpected \"whence\" parameter to fseek() wrapper: {}", whence), | ||
_ => { | ||
// TODO: Handle the error better. This indicates a bug in the engine. | ||
tt_warning!(es.status, "Unexpected \"whence\" parameter to fseek() wrapper: {}", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor, but while we're here, I'm trying to be more consistent about these sorts of messages not starting with capital letters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is also an internal error, maybe also add "serious internal error"?
Thanks for the review. Should I change just the It might be a good idea to unify the format of all the internal errors in this file when I am already doing the edits. |
I'll trust your judgment on both questions. For things like the "whence" problem, I think it is good to somehow try to convey to users that the program has some crazy problem and, importantly, that they (the users) will almost certainly not be able to fix it themselves. In fact, it might be smart to add some infrastructure for "internal bug" type problems that automatically produces a message to this effect, telling the user to visit the issue tracker. As for |
I see, thanks! I'll try to do something reasonable. |
Hi @rekka, Sorry for being so inactive on this. I'd like to get this PR actually merged! If you have a chance to make the final changes I suggested that would be great. Otherwise do you mind if I extend your PR with a couple of commits of my own that make the changes? |
Hi @pkgw, Sorry about not making progress on this. OK, I will try to have a look in the next few days and finish the pull request. |
Hi, just to give an update, I haven't yet been able to spend much time on this because of a few deadlines that started coming up. If you prefer to get this merged soon, feel free to edit the PR. Sorry about that... |
Instead of panicking on failure, add a warning and return an error value (if possible). Closes tectonic-typesetting#63. - Move error conversion to the `input_seek` wrapper. - Add TODOs in `input_seek` to handle the serious internal errors properly. Optimally, tectonic should stop when an internal error is encountered in seek.
Codecov Report
@@ Coverage Diff @@
## master #91 +/- ##
==========================================
- Coverage 37.88% 37.88% -0.01%
==========================================
Files 131 131
Lines 61500 61523 +23
==========================================
+ Hits 23298 23306 +8
- Misses 38202 38217 +15
Continue to review full report at Codecov.
|
Sorry about the delay! I think it's time to fix this possible UB. I added some crude handling of the internal bug when calling I also checked the other conditions that generate I noticed that all the functions in |
Yes, based on what I can find, it sounds like it is correct to add edit: link for posterity |
I have added the |
#188 is a panic across FFI boundary as well, triggering undefined behavior. Surprisingly, the stack trace works even for the C functions... We either should be careful not to panic in any Rust functions called by the C code, or wrap everything in |
Sorry for taking so long to merge this! I think I read somewhere that |
Thank for merging. And thanks for the comment about
|
remove unused `mfgets`
Instead of panicking on failure, add a warning and return an error value
(if possible).
Closes #63.
However, these kinds of problems should probably result in a failure since they indicate an internal engine error.