Fix a DCHECK() failure inside FPDF_SaveAsCopy().

FPDF_SaveAsCopy() calls CPDF_Name::WriteTo() for name objects, but names
can be empty. Adjust WriteTo() to avoid writing out an empty name, as it
is effectively a no-op and the IFX_WriteStream implementation has a
DCHECK() to prevent that.

With this fix, the FPDFSaveEmbedderTest.Bug1328389 test case can be
enabled since it no longer crashes. Update the test case's expectations
to reflect what actually happens.

Bug: chromium:1328389
Change-Id: I5fffc93d8811afaa9ba0f0fb501032ebf1165dc8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93930
Reviewed-by: Nigi <nigi@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_name.cpp b/core/fpdfapi/parser/cpdf_name.cpp
index 33ee8aa..c5aee55 100644
--- a/core/fpdfapi/parser/cpdf_name.cpp
+++ b/core/fpdfapi/parser/cpdf_name.cpp
@@ -52,6 +52,9 @@
 
 bool CPDF_Name::WriteTo(IFX_ArchiveStream* archive,
                         const CPDF_Encryptor* encryptor) const {
-  return archive->WriteString("/") &&
-         archive->WriteString(PDF_NameEncode(GetString()).AsStringView());
+  if (!archive->WriteString("/"))
+    return false;
+
+  const ByteString name = PDF_NameEncode(GetString());
+  return name.IsEmpty() || archive->WriteString(name.AsStringView());
 }
diff --git a/fpdfsdk/fpdf_save_embeddertest.cpp b/fpdfsdk/fpdf_save_embeddertest.cpp
index 7bada3b..0aab9fb 100644
--- a/fpdfsdk/fpdf_save_embeddertest.cpp
+++ b/fpdfsdk/fpdf_save_embeddertest.cpp
@@ -180,8 +180,9 @@
 }
 
 // Should not trigger a DCHECK() failure in CFX_FileBufferArchive.
-// TODO(crbug.com/1328389): Fix the bug so the test can be enabled.
-TEST_F(FPDFSaveEmbedderTest, DISABLED_Bug1328389) {
+// Fails because the PDF is malformed.
+TEST_F(FPDFSaveEmbedderTest, Bug1328389) {
   ASSERT_TRUE(OpenDocument("bug_1328389.pdf"));
   EXPECT_TRUE(FPDF_SaveAsCopy(document(), this, 0));
+  EXPECT_THAT(GetString(), testing::HasSubstr("/Foo/"));
 }