Remove some |new|s in cpdfsdk_formfillenvironment.cpp and cpdfxfa_context.cpp

Use std::vector<uint8_t> as buffer instead.

Change-Id: I710fe87f292b2c0f838410e9c7ff615c27c589a6
Reviewed-on: https://pdfium-review.googlesource.com/4790
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index 6c6e367..e1b34ce 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -114,14 +114,14 @@
   if (nRequiredLen <= 0)
     return CFX_WideString();
 
-  std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
-  memset(pBuff.get(), 0, nRequiredLen);
+  std::vector<uint8_t> pBuff(nRequiredLen);
   const int nActualLen = m_pInfo->m_pJsPlatform->Field_browse(
-      m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
+      m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen);
   if (nActualLen <= 0 || nActualLen > nRequiredLen)
     return CFX_WideString();
 
-  return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
+  pBuff.resize(nActualLen);
+  return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff));
 }
 
 CFX_WideString CPDFSDK_FormFillEnvironment::JS_docGetFilePath() {
@@ -134,14 +134,14 @@
   if (nRequiredLen <= 0)
     return CFX_WideString();
 
-  std::unique_ptr<char[]> pBuff(new char[nRequiredLen]);
-  memset(pBuff.get(), 0, nRequiredLen);
+  std::vector<uint8_t> pBuff(nRequiredLen);
   const int nActualLen = m_pInfo->m_pJsPlatform->Doc_getFilePath(
-      m_pInfo->m_pJsPlatform, pBuff.get(), nRequiredLen);
+      m_pInfo->m_pJsPlatform, pBuff.data(), nRequiredLen);
   if (nActualLen <= 0 || nActualLen > nRequiredLen)
     return CFX_WideString();
 
-  return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff.get(), nActualLen));
+  pBuff.resize(nActualLen);
+  return CFX_WideString::FromLocal(CFX_ByteStringC(pBuff));
 }
 
 void CPDFSDK_FormFillEnvironment::JS_docSubmitForm(void* formData,
@@ -368,25 +368,20 @@
 
 CFX_WideString CPDFSDK_FormFillEnvironment::GetPlatform() {
   if (!m_pInfo || !m_pInfo->FFI_GetPlatform)
-    return L"";
+    return CFX_WideString();
 
   int nRequiredLen = m_pInfo->FFI_GetPlatform(m_pInfo, nullptr, 0);
   if (nRequiredLen <= 0)
-    return L"";
+    return CFX_WideString();
 
-  char* pbuff = new char[nRequiredLen];
-  memset(pbuff, 0, nRequiredLen);
-  int nActualLen = m_pInfo->FFI_GetPlatform(m_pInfo, pbuff, nRequiredLen);
-  if (nActualLen <= 0 || nActualLen > nRequiredLen) {
-    delete[] pbuff;
-    return L"";
-  }
-  CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
-  CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
-      (unsigned short*)bsRet.GetBuffer(bsRet.GetLength()),
-      bsRet.GetLength() / sizeof(unsigned short));
-  delete[] pbuff;
-  return wsRet;
+  std::vector<uint8_t> pBuff(nRequiredLen);
+  int nActualLen =
+      m_pInfo->FFI_GetPlatform(m_pInfo, pBuff.data(), nRequiredLen);
+  if (nActualLen <= 0 || nActualLen > nRequiredLen)
+    return CFX_WideString();
+
+  return CFX_WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
+                                     nActualLen / sizeof(uint16_t));
 }
 
 void CPDFSDK_FormFillEnvironment::GotoURL(FPDF_DOCUMENT document,
@@ -533,25 +528,20 @@
 
 CFX_WideString CPDFSDK_FormFillEnvironment::GetLanguage() {
   if (!m_pInfo || !m_pInfo->FFI_GetLanguage)
-    return L"";
+    return CFX_WideString();
 
   int nRequiredLen = m_pInfo->FFI_GetLanguage(m_pInfo, nullptr, 0);
   if (nRequiredLen <= 0)
-    return L"";
+    return CFX_WideString();
 
-  char* pbuff = new char[nRequiredLen];
-  memset(pbuff, 0, nRequiredLen);
-  int nActualLen = m_pInfo->FFI_GetLanguage(m_pInfo, pbuff, nRequiredLen);
-  if (nActualLen <= 0 || nActualLen > nRequiredLen) {
-    delete[] pbuff;
-    return L"";
-  }
-  CFX_ByteString bsRet = CFX_ByteString(pbuff, nActualLen);
-  CFX_WideString wsRet = CFX_WideString::FromUTF16LE(
-      (FPDF_WIDESTRING)bsRet.GetBuffer(bsRet.GetLength()),
-      bsRet.GetLength() / sizeof(FPDF_WIDESTRING));
-  delete[] pbuff;
-  return wsRet;
+  std::vector<uint8_t> pBuff(nRequiredLen);
+  int nActualLen =
+      m_pInfo->FFI_GetLanguage(m_pInfo, pBuff.data(), nRequiredLen);
+  if (nActualLen <= 0 || nActualLen > nRequiredLen)
+    return CFX_WideString();
+
+  return CFX_WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
+                                     nActualLen / sizeof(uint16_t));
 }
 
 void CPDFSDK_FormFillEnvironment::PageEvent(int iPageCount,
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
index 5773542..bda0128 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
@@ -6,6 +6,7 @@
 
 #include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
 
+#include <algorithm>
 #include <utility>
 
 #include "core/fpdfapi/parser/cpdf_document.h"
@@ -307,25 +308,22 @@
                                          const CFX_WideString& wsTitle,
                                          const CFX_WideString& wsDefaultAnswer,
                                          bool bMark) {
-  CFX_WideString wsAnswer;
   if (!m_pFormFillEnv)
-    return wsAnswer;
+    return CFX_WideString();
 
   int nLength = 2048;
-  char* pBuff = new char[nLength];
+  std::vector<uint8_t> pBuff(nLength);
   nLength = m_pFormFillEnv->JS_appResponse(wsQuestion.c_str(), wsTitle.c_str(),
                                            wsDefaultAnswer.c_str(), nullptr,
-                                           bMark, pBuff, nLength);
-  if (nLength > 0) {
-    nLength = nLength > 2046 ? 2046 : nLength;
-    pBuff[nLength] = 0;
-    pBuff[nLength + 1] = 0;
-    wsAnswer = CFX_WideString::FromUTF16LE(
-        reinterpret_cast<const unsigned short*>(pBuff),
-        nLength / sizeof(unsigned short));
-  }
-  delete[] pBuff;
-  return wsAnswer;
+                                           bMark, pBuff.data(), nLength);
+  if (nLength <= 0)
+    return CFX_WideString();
+
+  nLength = std::min(2046, nLength);
+  pBuff[nLength] = 0;
+  pBuff[nLength + 1] = 0;
+  return CFX_WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
+                                     nLength / sizeof(uint16_t));
 }
 
 CFX_RetainPtr<IFX_SeekableReadStream> CPDFXFA_Context::DownloadURL(