Pass output parameters as pointers in ijs_event_context

It is hard to tell if there is some unsavory stuff going on with
references otherwise. Avoids some const_casts in the process.

Add some UnownedPtrs along the way to check caller's storage duration.

Change-Id: Ic8d85802083f0b27e07993ea25f8f1c15fca1712
Reviewed-on: https://pdfium-review.googlesource.com/35750
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_actionhandler.cpp b/fpdfsdk/cpdfsdk_actionhandler.cpp
index 5b72082..64d82b9 100644
--- a/fpdfsdk/cpdfsdk_actionhandler.cpp
+++ b/fpdfsdk/cpdfsdk_actionhandler.cpp
@@ -422,22 +422,24 @@
             break;
           case CPDF_AAction::GetFocus:
             context->OnField_Focus(data->bModifier, data->bShift, pFormField,
-                                   data->sValue);
+                                   &data->sValue);
             break;
           case CPDF_AAction::LoseFocus:
             context->OnField_Blur(data->bModifier, data->bShift, pFormField,
-                                  data->sValue);
+                                  &data->sValue);
             break;
           case CPDF_AAction::KeyStroke:
             context->OnField_Keystroke(
-                data->sChange, data->sChangeEx, data->bKeyDown, data->bModifier,
-                data->nSelEnd, data->nSelStart, data->bShift, pFormField,
-                data->sValue, data->bWillCommit, data->bFieldFull, data->bRC);
+                &data->sChange, data->sChangeEx, data->bKeyDown,
+                data->bModifier, &data->nSelEnd, &data->nSelStart, data->bShift,
+                pFormField, &data->sValue, data->bWillCommit, data->bFieldFull,
+                &data->bRC);
             break;
           case CPDF_AAction::Validate:
-            context->OnField_Validate(
-                data->sChange, data->sChangeEx, data->bKeyDown, data->bModifier,
-                data->bShift, pFormField, data->sValue, data->bRC);
+            context->OnField_Validate(&data->sChange, data->sChangeEx,
+                                      data->bKeyDown, data->bModifier,
+                                      data->bShift, pFormField, &data->sValue,
+                                      &data->bRC);
             break;
           default:
             NOTREACHED();
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index 751a889..9d646b6 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -290,7 +290,7 @@
     WideString sValue = sOldValue;
     bool bRC = true;
     IJS_Runtime::ScopedEventContext pContext(pRuntime);
-    pContext->OnField_Calculate(pFormField, pField, sValue, bRC);
+    pContext->OnField_Calculate(pFormField, pField, &sValue, &bRC);
 
     Optional<IJS_Runtime::JS_Error> err = pContext->RunScript(csJS);
     if (!err && bRC && sValue.Compare(sOldValue) != 0)
@@ -324,7 +324,7 @@
       if (!script.IsEmpty()) {
         WideString Value = sValue;
         IJS_Runtime::ScopedEventContext pContext(pRuntime);
-        pContext->OnField_Format(pFormField, Value, true);
+        pContext->OnField_Format(pFormField, &Value, true);
         Optional<IJS_Runtime::JS_Error> err = pContext->RunScript(script);
         if (!err) {
           sValue = std::move(Value);
diff --git a/fxjs/cjs_event_context.cpp b/fxjs/cjs_event_context.cpp
index 70f0677..3d40c9d 100644
--- a/fxjs/cjs_event_context.cpp
+++ b/fxjs/cjs_event_context.cpp
@@ -136,55 +136,55 @@
 void CJS_EventContext::OnField_Focus(bool bModifier,
                                      bool bShift,
                                      CPDF_FormField* pTarget,
-                                     const WideString& Value) {
+                                     WideString* Value) {
   m_pEventHandler->OnField_Focus(bModifier, bShift, pTarget, Value);
 }
 
 void CJS_EventContext::OnField_Blur(bool bModifier,
                                     bool bShift,
                                     CPDF_FormField* pTarget,
-                                    const WideString& Value) {
+                                    WideString* Value) {
   m_pEventHandler->OnField_Blur(bModifier, bShift, pTarget, Value);
 }
 
 void CJS_EventContext::OnField_Calculate(CPDF_FormField* pSource,
                                          CPDF_FormField* pTarget,
-                                         WideString& Value,
-                                         bool& bRc) {
-  m_pEventHandler->OnField_Calculate(pSource, pTarget, Value, bRc);
+                                         WideString* pValue,
+                                         bool* pRc) {
+  m_pEventHandler->OnField_Calculate(pSource, pTarget, pValue, pRc);
 }
 
 void CJS_EventContext::OnField_Format(CPDF_FormField* pTarget,
-                                      WideString& Value,
+                                      WideString* Value,
                                       bool bWillCommit) {
   m_pEventHandler->OnField_Format(pTarget, Value, bWillCommit);
 }
 
-void CJS_EventContext::OnField_Keystroke(WideString& strChange,
+void CJS_EventContext::OnField_Keystroke(WideString* strChange,
                                          const WideString& strChangeEx,
                                          bool bKeyDown,
                                          bool bModifier,
-                                         int& nSelEnd,
-                                         int& nSelStart,
+                                         int* nSelEnd,
+                                         int* nSelStart,
                                          bool bShift,
                                          CPDF_FormField* pTarget,
-                                         WideString& Value,
+                                         WideString* Value,
                                          bool bWillCommit,
                                          bool bFieldFull,
-                                         bool& bRc) {
+                                         bool* bRc) {
   m_pEventHandler->OnField_Keystroke(
       strChange, strChangeEx, bKeyDown, bModifier, nSelEnd, nSelStart, bShift,
       pTarget, Value, bWillCommit, bFieldFull, bRc);
 }
 
-void CJS_EventContext::OnField_Validate(WideString& strChange,
+void CJS_EventContext::OnField_Validate(WideString* strChange,
                                         const WideString& strChangeEx,
                                         bool bKeyDown,
                                         bool bModifier,
                                         bool bShift,
                                         CPDF_FormField* pTarget,
-                                        WideString& Value,
-                                        bool& bRc) {
+                                        WideString* Value,
+                                        bool* bRc) {
   m_pEventHandler->OnField_Validate(strChange, strChangeEx, bKeyDown, bModifier,
                                     bShift, pTarget, Value, bRc);
 }
diff --git a/fxjs/cjs_event_context.h b/fxjs/cjs_event_context.h
index 3cfc6da..29bbd5a 100644
--- a/fxjs/cjs_event_context.h
+++ b/fxjs/cjs_event_context.h
@@ -52,38 +52,38 @@
   void OnField_Focus(bool bModifier,
                      bool bShift,
                      CPDF_FormField* pTarget,
-                     const WideString& Value) override;
+                     WideString* Value) override;
   void OnField_Blur(bool bModifier,
                     bool bShift,
                     CPDF_FormField* pTarget,
-                    const WideString& Value) override;
+                    WideString* Value) override;
   void OnField_Calculate(CPDF_FormField* pSource,
                          CPDF_FormField* pTarget,
-                         WideString& Value,
-                         bool& bRc) override;
+                         WideString* pValue,
+                         bool* pRc) override;
   void OnField_Format(CPDF_FormField* pTarget,
-                      WideString& Value,
+                      WideString* Value,
                       bool bWillCommit) override;
-  void OnField_Keystroke(WideString& strChange,
+  void OnField_Keystroke(WideString* strChange,
                          const WideString& strChangeEx,
                          bool bKeyDown,
                          bool bModifier,
-                         int& nSelEnd,
-                         int& nSelStart,
+                         int* nSelEnd,
+                         int* nSelStart,
                          bool bShift,
                          CPDF_FormField* pTarget,
-                         WideString& Value,
+                         WideString* Value,
                          bool bWillCommit,
                          bool bFieldFull,
-                         bool& bRc) override;
-  void OnField_Validate(WideString& strChange,
+                         bool* bRc) override;
+  void OnField_Validate(WideString* strChange,
                         const WideString& strChangeEx,
                         bool bKeyDown,
                         bool bModifier,
                         bool bShift,
                         CPDF_FormField* pTarget,
-                        WideString& Value,
-                        bool& bRc) override;
+                        WideString* Value,
+                        bool* bRc) override;
   void OnScreen_Focus(bool bModifier,
                       bool bShift,
                       CPDFSDK_Annot* pScreen) override;
diff --git a/fxjs/cjs_event_context_stub.h b/fxjs/cjs_event_context_stub.h
index c8c5e33..341b995 100644
--- a/fxjs/cjs_event_context_stub.h
+++ b/fxjs/cjs_event_context_stub.h
@@ -44,38 +44,38 @@
   void OnField_Focus(bool bModifier,
                      bool bShift,
                      CPDF_FormField* pTarget,
-                     const WideString& Value) override {}
+                     WideString* Value) override {}
   void OnField_Blur(bool bModifier,
                     bool bShift,
                     CPDF_FormField* pTarget,
-                    const WideString& Value) override {}
+                    WideString* Value) override {}
   void OnField_Calculate(CPDF_FormField* pSource,
                          CPDF_FormField* pTarget,
-                         WideString& Value,
-                         bool& bRc) override {}
+                         WideString* pValue,
+                         bool* pRc) override {}
   void OnField_Format(CPDF_FormField* pTarget,
-                      WideString& Value,
+                      WideString* Value,
                       bool bWillCommit) override {}
-  void OnField_Keystroke(WideString& strChange,
+  void OnField_Keystroke(WideString* strChange,
                          const WideString& strChangeEx,
                          bool KeyDown,
                          bool bModifier,
-                         int& nSelEnd,
-                         int& nSelStart,
+                         int* nSelEnd,
+                         int* nSelStart,
                          bool bShift,
                          CPDF_FormField* pTarget,
-                         WideString& Value,
+                         WideString* Value,
                          bool bWillCommit,
                          bool bFieldFull,
-                         bool& bRc) override {}
-  void OnField_Validate(WideString& strChange,
+                         bool* bRc) override {}
+  void OnField_Validate(WideString* strChange,
                         const WideString& strChangeEx,
                         bool bKeyDown,
                         bool bModifier,
                         bool bShift,
                         CPDF_FormField* pTarget,
-                        WideString& Value,
-                        bool& bRc) override {}
+                        WideString* Value,
+                        bool* bRc) override {}
   void OnScreen_Focus(bool bModifier,
                       bool bShift,
                       CPDFSDK_Annot* pScreen) override {}
diff --git a/fxjs/cjs_eventhandler.cpp b/fxjs/cjs_eventhandler.cpp
index 43605d9..884651a 100644
--- a/fxjs/cjs_eventhandler.cpp
+++ b/fxjs/cjs_eventhandler.cpp
@@ -127,97 +127,103 @@
 void CJS_EventHandler::OnField_Focus(bool bModifier,
                                      bool bShift,
                                      CPDF_FormField* pTarget,
-                                     const WideString& Value) {
+                                     WideString* pValue) {
+  ASSERT(pValue);
   Initialize(JET_FIELD_FOCUS);
 
   m_bModifier = bModifier;
   m_bShift = bShift;
   m_strTargetName = pTarget->GetFullName();
-  m_pValue = const_cast<WideString*>(&Value);
+  m_pValue = pValue;
 }
 
 void CJS_EventHandler::OnField_Blur(bool bModifier,
                                     bool bShift,
                                     CPDF_FormField* pTarget,
-                                    const WideString& Value) {
+                                    WideString* pValue) {
+  ASSERT(pValue);
   Initialize(JET_FIELD_BLUR);
 
   m_bModifier = bModifier;
   m_bShift = bShift;
   m_strTargetName = pTarget->GetFullName();
-  m_pValue = const_cast<WideString*>(&Value);
+  m_pValue = pValue;
 }
 
-void CJS_EventHandler::OnField_Keystroke(WideString& strChange,
+void CJS_EventHandler::OnField_Keystroke(WideString* strChange,
                                          const WideString& strChangeEx,
                                          bool KeyDown,
                                          bool bModifier,
-                                         int& nSelEnd,
-                                         int& nSelStart,
+                                         int* pSelEnd,
+                                         int* pSelStart,
                                          bool bShift,
                                          CPDF_FormField* pTarget,
-                                         WideString& Value,
+                                         WideString* pValue,
                                          bool bWillCommit,
                                          bool bFieldFull,
-                                         bool& bRc) {
+                                         bool* pbRc) {
+  ASSERT(pValue && pbRc && pSelEnd && pSelStart);
   Initialize(JET_FIELD_KEYSTROKE);
 
   m_nCommitKey = 0;
-  m_pWideStrChange = &strChange;
+  m_pWideStrChange = strChange;
   m_WideStrChangeEx = strChangeEx;
   m_bKeyDown = KeyDown;
   m_bModifier = bModifier;
-  m_pISelEnd = &nSelEnd;
-  m_pISelStart = &nSelStart;
+  m_pISelEnd = pSelEnd;
+  m_pISelStart = pSelStart;
   m_bShift = bShift;
   m_strTargetName = pTarget->GetFullName();
-  m_pValue = &Value;
+  m_pValue = pValue;
   m_bWillCommit = bWillCommit;
-  m_pbRc = &bRc;
+  m_pbRc = pbRc;
   m_bFieldFull = bFieldFull;
 }
 
-void CJS_EventHandler::OnField_Validate(WideString& strChange,
+void CJS_EventHandler::OnField_Validate(WideString* strChange,
                                         const WideString& strChangeEx,
                                         bool bKeyDown,
                                         bool bModifier,
                                         bool bShift,
                                         CPDF_FormField* pTarget,
-                                        WideString& Value,
-                                        bool& bRc) {
+                                        WideString* pValue,
+                                        bool* pbRc) {
+  ASSERT(pValue && pbRc);
   Initialize(JET_FIELD_VALIDATE);
 
-  m_pWideStrChange = &strChange;
+  m_pWideStrChange = strChange;
   m_WideStrChangeEx = strChangeEx;
   m_bKeyDown = bKeyDown;
   m_bModifier = bModifier;
   m_bShift = bShift;
   m_strTargetName = pTarget->GetFullName();
-  m_pValue = &Value;
-  m_pbRc = &bRc;
+  m_pValue = pValue;
+  m_pbRc = pbRc;
 }
 
 void CJS_EventHandler::OnField_Calculate(CPDF_FormField* pSource,
                                          CPDF_FormField* pTarget,
-                                         WideString& Value,
-                                         bool& bRc) {
+                                         WideString* pValue,
+                                         bool* pRc) {
+  ASSERT(pValue && pRc);
   Initialize(JET_FIELD_CALCULATE);
 
   if (pSource)
     m_strSourceName = pSource->GetFullName();
   m_strTargetName = pTarget->GetFullName();
-  m_pValue = &Value;
-  m_pbRc = &bRc;
+  m_pValue = pValue;
+  m_pbRc = pRc;
 }
 
 void CJS_EventHandler::OnField_Format(CPDF_FormField* pTarget,
-                                      WideString& Value,
+                                      WideString* pValue,
                                       bool bWillCommit) {
+  ASSERT(pValue);
   Initialize(JET_FIELD_FORMAT);
 
   m_nCommitKey = 0;
   m_strTargetName = pTarget->GetFullName();
-  m_pValue = &Value;
+  m_pValue = pValue;
   m_bWillCommit = bWillCommit;
 }
 
diff --git a/fxjs/cjs_eventhandler.h b/fxjs/cjs_eventhandler.h
index 7c33ed7..b38d3ff 100644
--- a/fxjs/cjs_eventhandler.h
+++ b/fxjs/cjs_eventhandler.h
@@ -81,32 +81,31 @@
 
   void OnField_Calculate(CPDF_FormField* pSource,
                          CPDF_FormField* pTarget,
-                         WideString& Value,
-                         bool& bRc);
+                         WideString* Value,
+                         bool* pbRc);
   void OnField_Format(CPDF_FormField* pTarget,
-                      WideString& Value,
+                      WideString* Value,
                       bool bWillCommit);
-  void OnField_Keystroke(WideString& strChange,
+  void OnField_Keystroke(WideString* strChange,
                          const WideString& strChangeEx,
                          bool KeyDown,
                          bool bModifier,
-                         int& nSelEnd,
-                         int& nSelStart,
+                         int* nSelEnd,
+                         int* nSelStart,
                          bool bShift,
                          CPDF_FormField* pTarget,
-                         WideString& Value,
+                         WideString* Value,
                          bool bWillCommit,
                          bool bFieldFull,
-                         bool& bRc);
-  void OnField_Validate(WideString& strChange,
+                         bool* bRc);
+  void OnField_Validate(WideString* strChange,
                         const WideString& strChangeEx,
                         bool bKeyDown,
                         bool bModifier,
                         bool bShift,
                         CPDF_FormField* pTarget,
-                        WideString& Value,
-                        bool& bRc);
-
+                        WideString* Value,
+                        bool* bRc);
   void OnField_MouseDown(bool bModifier, bool bShift, CPDF_FormField* pTarget);
   void OnField_MouseEnter(bool bModifier, bool bShift, CPDF_FormField* pTarget);
   void OnField_MouseExit(bool bModifier, bool bShift, CPDF_FormField* pTarget);
@@ -114,11 +113,11 @@
   void OnField_Blur(bool bModifier,
                     bool bShift,
                     CPDF_FormField* pTarget,
-                    const WideString& Value);
+                    WideString* Value);
   void OnField_Focus(bool bModifier,
                      bool bShift,
                      CPDF_FormField* pTarget,
-                     const WideString& Value);
+                     WideString* Value);
 
   void OnScreen_Focus(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen);
   void OnScreen_Blur(bool bModifier, bool bShift, CPDFSDK_Annot* pScreen);
@@ -190,13 +189,13 @@
   bool m_bKeyDown = false;
   bool m_bModifier = false;
   bool m_bShift = false;
-  int* m_pISelEnd = nullptr;
+  UnownedPtr<int> m_pISelEnd;
   int m_nSelEndDu = 0;
-  int* m_pISelStart = nullptr;
+  UnownedPtr<int> m_pISelStart;
   int m_nSelStartDu = 0;
   bool m_bWillCommit = false;
   bool m_bFieldFull = false;
-  bool* m_pbRc = nullptr;
+  UnownedPtr<bool> m_pbRc;
   bool m_bRcDu = false;
 
   UnownedPtr<CPDF_Bookmark> m_pTargetBookMark;
diff --git a/fxjs/ijs_event_context.h b/fxjs/ijs_event_context.h
index 8317bc2..a81f9fb 100644
--- a/fxjs/ijs_event_context.h
+++ b/fxjs/ijs_event_context.h
@@ -57,39 +57,38 @@
   virtual void OnField_Focus(bool bModifier,
                              bool bShift,
                              CPDF_FormField* pTarget,
-                             const WideString& Value) = 0;
+                             WideString* Value) = 0;
   virtual void OnField_Blur(bool bModifier,
                             bool bShift,
                             CPDF_FormField* pTarget,
-                            const WideString& Value) = 0;
-
+                            WideString* Value) = 0;
   virtual void OnField_Calculate(CPDF_FormField* pSource,
                                  CPDF_FormField* pTarget,
-                                 WideString& Value,
-                                 bool& bRc) = 0;
+                                 WideString* Value,
+                                 bool* bRc) = 0;
   virtual void OnField_Format(CPDF_FormField* pTarget,
-                              WideString& Value,
+                              WideString* Value,
                               bool bWillCommit) = 0;
-  virtual void OnField_Keystroke(WideString& strChange,
+  virtual void OnField_Keystroke(WideString* strChange,
                                  const WideString& strChangeEx,
                                  bool KeyDown,
                                  bool bModifier,
-                                 int& nSelEnd,
-                                 int& nSelStart,
+                                 int* nSelEnd,
+                                 int* nSelStart,
                                  bool bShift,
                                  CPDF_FormField* pTarget,
-                                 WideString& Value,
+                                 WideString* Value,
                                  bool bWillCommit,
                                  bool bFieldFull,
-                                 bool& bRc) = 0;
-  virtual void OnField_Validate(WideString& strChange,
+                                 bool* bRc) = 0;
+  virtual void OnField_Validate(WideString* strChange,
                                 const WideString& strChangeEx,
                                 bool bKeyDown,
                                 bool bModifier,
                                 bool bShift,
                                 CPDF_FormField* pTarget,
-                                WideString& Value,
-                                bool& bRc) = 0;
+                                WideString* Value,
+                                bool* bRc) = 0;
 
   virtual void OnScreen_Focus(bool bModifier,
                               bool bShift,