Add a way to undo a previous FPDF_SetSystemFontInfo() call
Modify FPDF_SetSystemFontInfo() to be able to take a nullptr without
crashing. Handle the nullptr case by releasing PDFium's hold on the
previously passed in pointer. Since no existing embedder could have
passed in a nullptr before without crashing, this is a safe enhancement
to an existing stable API.
This gives FPDF_SetSystemFontInfo() callers that need to also call
FPDF_FreeDefaultSystemFontInfo() a way to safely release PDFium's
pointer to the default system font info, before freeing the font info.
Use this where appropriate to balance out prior FPDF_SetSystemFontInfo()
calls in existing code.
Update pdfium_test.cc to make the call at the appropriate time, before
the FPDF_DestroyLibrary() call, by adjusting std::unique_ptr lifetimes.
In a follow-up CL, PartitionAllocator class instances will stop living
forever. Thus after FPDF_DestroyLibrary(), it is too late to call
FPDF_FreeDefaultSystemFontInfo(), as the font info was allocated inside
a destroyed partition.
Bug: pdfium:1876
Change-Id: Ic3835bae5f95604f89afb35bfaf8c0d568c6bc4b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/116771
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Thomas Sepez <tsepez@google.com>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_sysfontinfo_embeddertest.cpp b/fpdfsdk/fpdf_sysfontinfo_embeddertest.cpp
index 24df525..589cf71 100644
--- a/fpdfsdk/fpdf_sysfontinfo_embeddertest.cpp
+++ b/fpdfsdk/fpdf_sysfontinfo_embeddertest.cpp
@@ -77,9 +77,10 @@
}
void TearDown() override {
+ FPDF_SetSystemFontInfo(nullptr);
EmbedderTest::TearDown();
- // Bouncing the library is the only reliable way to undo the
+ // Bouncing the library is the only reliable way to fully undo the initial
// FPDF_SetSystemFontInfo() call at the moment.
EmbedderTestEnvironment::GetInstance()->TearDown();
EmbedderTestEnvironment::GetInstance()->SetUp();
@@ -103,13 +104,14 @@
void TearDown() override {
EmbedderTest::TearDown();
- // Bouncing the library is the only reliable way to undo the
+ // After releasing `font_info_` from PDFium, it is safe to free it.
+ FPDF_SetSystemFontInfo(nullptr);
+ FPDF_FreeDefaultSystemFontInfo(font_info_);
+
+ // Bouncing the library is the only reliable way to fully undo the initial
// FPDF_SetSystemFontInfo() call at the moment.
EmbedderTestEnvironment::GetInstance()->TearDown();
- // After shutdown, it is safe to release the font info.
- FPDF_FreeDefaultSystemFontInfo(font_info_);
-
EmbedderTestEnvironment::GetInstance()->SetUp();
}