fix(PDFWriter): prevent segfault on double EndPDF calls#328
Open
SergeySD wants to merge 1 commit into
Open
Conversation
Add mIsStarted flag to track PDF lifecycle state, preventing crashes when EndPDF or EndPDFForStream are called multiple times. Includes comprehensive test coverage for double-end scenarios and edge cases.
|
i would have done it the other way around like "isEnded" instead of "isStarted" but this is the correct fix for double calls to end keeping a state. |
Owner
|
i don't really see it as the library role to make sure you call endpdf just once. just don't. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mIsStartedflag toPDFWriterclass to track PDF lifecycle stateEndPDF()orEndPDFForStream()are called multiple timeseFailuregracefully instead of causing undefined behavior/segfaultProblem
Calling
EndPDF()twice on the samePDFWriterinstance causes a segmentation fault because:EndPDF()callsmDocumentContext.FinalizeNewPDF()which operates on already cleaned-up resourcesEndPDF()callsmOutputFile.CloseFile()on already closed file handlesThis affects wrapper code (Node.js, Python, etc.) where:
Solution
Added
bool mIsStartedmember variable:falsein constructor andReset()trueinStartPDF(),StartPDFForStream(),ModifyPDF(),ModifyPDFForStream(),ContinuePDF(),ContinuePDFForStream()EndPDF()andEndPDFForStream()- returnseFailureif not startedTest Plan
DoubleEndPDFTest.cppwith 4 test cases:EndPDF()on file output returnseFailureEndPDFForStream()on stream output returnseFailureReset()works correctlyEndPDF()withoutStartPDF()returnseFailure