Rework CPDFSDK_FormFillEnvironment::JS_appResponse() callers.

Since JS_appResponse() returns how much of the buffer is actually used,
and the function that handles the returned buffer also takes a size,
there is no need to pad the buffer with NULL characters. Remove the code
that assumes this is the case.

Change-Id: Ia83105679a82bf14c388c6e638438b5d86b3503a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98571
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
index 137da39..6420273 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
@@ -343,19 +343,18 @@
   if (!m_pFormFillEnv)
     return WideString();
 
-  int byte_length = 2048;
-  FixedZeroedDataVector<uint16_t> buffer(byte_length / sizeof(uint16_t));
+  constexpr int kMaxWideChars = 1024;
+  FixedZeroedDataVector<uint16_t> buffer(kMaxWideChars);
   pdfium::span<uint16_t> buffer_span = buffer.writable_span();
-  byte_length = m_pFormFillEnv->JS_appResponse(
+  int byte_length = m_pFormFillEnv->JS_appResponse(
       wsQuestion, wsTitle, wsDefaultAnswer, WideString(), bMark,
       pdfium::as_writable_bytes(buffer_span));
   if (byte_length <= 0)
     return WideString();
 
-  byte_length = std::min(2046, byte_length);
-  buffer_span[byte_length / sizeof(uint16_t)] = 0;
-  return WideString::FromUTF16LE(buffer_span.data(),
-                                 byte_length / sizeof(uint16_t));
+  buffer_span = buffer_span.first(
+      std::min<size_t>(kMaxWideChars, byte_length / sizeof(uint16_t)));
+  return WideString::FromUTF16LE(buffer_span.data(), buffer_span.size());
 }
 
 RetainPtr<IFX_SeekableReadStream> CPDFXFA_Context::DownloadURL(
diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp
index b417106..e7c23e7 100644
--- a/fxjs/cjs_app.cpp
+++ b/fxjs/cjs_app.cpp
@@ -8,6 +8,7 @@
 
 #include <stdint.h>
 
+#include <algorithm>
 #include <utility>
 
 #include "core/fxcrt/fixed_zeroed_data_vector.h"
@@ -549,20 +550,20 @@
   if (IsExpandedParamKnown(newParams[4]))
     swLabel = pRuntime->ToWideString(newParams[4]);
 
-  constexpr int kMaxBytes = 2048;
-  constexpr int kMaxWideChars = kMaxBytes / 2;
-  // Add 1 char for a NUL terminator.
-  FixedZeroedDataVector<uint16_t> buffer(kMaxWideChars + 1);
+  constexpr int kMaxWideChars = 1024;
+  constexpr int kMaxBytes = kMaxWideChars * sizeof(uint16_t);
+  FixedZeroedDataVector<uint16_t> buffer(kMaxWideChars);
+  pdfium::span<uint16_t> buffer_span = buffer.writable_span();
   int byte_length = pRuntime->GetFormFillEnv()->JS_appResponse(
       swQuestion, swTitle, swDefault, swLabel, bPassword,
-      pdfium::as_writable_bytes(buffer.writable_span().first(kMaxWideChars)));
-
+      pdfium::as_writable_bytes(buffer_span));
   if (byte_length < 0 || byte_length > kMaxBytes)
     return CJS_Result::Failure(JSMessage::kParamTooLongError);
 
+  buffer_span = buffer_span.first(
+      std::min<size_t>(kMaxWideChars, byte_length / sizeof(uint16_t)));
   return CJS_Result::Success(pRuntime->NewString(
-      WideString::FromUTF16LE(buffer.span().data(),
-                              byte_length / sizeof(uint16_t))
+      WideString::FromUTF16LE(buffer_span.data(), buffer_span.size())
           .AsStringView()));
 }