M101: Retain original dictionary in CPDFSDK_AppStream::Write().

Bug: chromium:1314658
Change-Id: Iad1db4fc2285492e31b8bb535a0b078b07fb73ef
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/92432
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
(cherry picked from commit 45333887dc93ce27ae3c54a2c541ae1c8c987824)
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/92550
Reviewed-by: Nigi <nigi@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_appstream.cpp b/fpdfsdk/cpdfsdk_appstream.cpp
index 529f860..a006aef 100644
--- a/fpdfsdk/cpdfsdk_appstream.cpp
+++ b/fpdfsdk/cpdfsdk_appstream.cpp
@@ -1858,10 +1858,11 @@
     key = sAPState;
   }
 
+  RetainPtr<CPDF_Dictionary> pOrigStreamDict;
+
   // If `pStream` is created by CreateModifiedAPStream(), then it is safe to
   // edit, as it is not shared.
   CPDF_Stream* pStream = pParentDict->GetStreamFor(key);
-  CPDF_Dictionary* pOrigStreamDict = nullptr;
   CPDF_Document* doc = widget_->GetPageView()->GetPDFDocument();
   if (!doc->IsModifiedAPStream(pStream)) {
     if (pStream)
diff --git a/testing/resources/javascript/bug_1314658.in b/testing/resources/javascript/bug_1314658.in
new file mode 100644
index 0000000..f05b3ed
--- /dev/null
+++ b/testing/resources/javascript/bug_1314658.in
@@ -0,0 +1,29 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Kids [3 0 R]
+>>
+endobj
+{{object 3 0}} <<
+  /]
+  /Subtype /Widget
+  /T (0)
+  /FT /Ch
+  /AA <<
+    /F 5 0 R
+  >>
+  /Annots 4 0 R
+4 0 obj [3 0 R /Ff 393216]
+  /AP<</N<<>>
+stream
+{{object 5 0}} <<
+  /JS (app.alert('done'))
+>>
+endobj
+{{trailer}}
+%%EOF
diff --git a/testing/resources/javascript/bug_1314658_expected.txt b/testing/resources/javascript/bug_1314658_expected.txt
new file mode 100644
index 0000000..daa1eca
--- /dev/null
+++ b/testing/resources/javascript/bug_1314658_expected.txt
@@ -0,0 +1 @@
+Alert: done