Use FixedZeroedDataVector in JS_appResponse() callers.
Use FixedZeroedDataVector instead of DataVector since the vectors are a
fixed length. Also make the FixedZeroedDataVectors of type uint16_t, to
avoid having to cast when converting to WideStrings. Instead, use
pdfium::as_writable_bytes() when passing a span to JS_appResponse().
Change-Id: Ia6d04dfee02ea52f7ff56c1559ac8c1b1cebeb51
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98452
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 4927a8a..137da39 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_context.cpp
@@ -16,7 +16,7 @@
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_seekablemultistream.h"
#include "core/fxcrt/autonuller.h"
-#include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fixed_zeroed_data_vector.h"
#include "core/fxcrt/stl_util.h"
#include "core/fxcrt/xml/cfx_xmldocument.h"
#include "core/fxcrt/xml/cfx_xmlparser.h"
@@ -343,18 +343,19 @@
if (!m_pFormFillEnv)
return WideString();
- int nLength = 2048;
- DataVector<uint8_t> pBuff(nLength);
- nLength = m_pFormFillEnv->JS_appResponse(wsQuestion, wsTitle, wsDefaultAnswer,
- WideString(), bMark, pBuff);
- if (nLength <= 0)
+ int byte_length = 2048;
+ FixedZeroedDataVector<uint16_t> buffer(byte_length / sizeof(uint16_t));
+ pdfium::span<uint16_t> buffer_span = buffer.writable_span();
+ byte_length = m_pFormFillEnv->JS_appResponse(
+ wsQuestion, wsTitle, wsDefaultAnswer, WideString(), bMark,
+ pdfium::as_writable_bytes(buffer_span));
+ if (byte_length <= 0)
return WideString();
- nLength = std::min(2046, nLength);
- pBuff[nLength] = 0;
- pBuff[nLength + 1] = 0;
- return WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
- nLength / sizeof(uint16_t));
+ 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));
}
RetainPtr<IFX_SeekableReadStream> CPDFXFA_Context::DownloadURL(
diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp
index e7787c7..b417106 100644
--- a/fxjs/cjs_app.cpp
+++ b/fxjs/cjs_app.cpp
@@ -10,7 +10,7 @@
#include <utility>
-#include "core/fxcrt/data_vector.h"
+#include "core/fxcrt/fixed_zeroed_data_vector.h"
#include "core/fxcrt/stl_util.h"
#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
#include "fpdfsdk/cpdfsdk_interactiveform.h"
@@ -549,18 +549,20 @@
if (IsExpandedParamKnown(newParams[4]))
swLabel = pRuntime->ToWideString(newParams[4]);
- const int MAX_INPUT_BYTES = 2048;
- DataVector<uint8_t> pBuff(MAX_INPUT_BYTES + 2);
- int nLengthBytes = pRuntime->GetFormFillEnv()->JS_appResponse(
+ constexpr int kMaxBytes = 2048;
+ constexpr int kMaxWideChars = kMaxBytes / 2;
+ // Add 1 char for a NUL terminator.
+ FixedZeroedDataVector<uint16_t> buffer(kMaxWideChars + 1);
+ int byte_length = pRuntime->GetFormFillEnv()->JS_appResponse(
swQuestion, swTitle, swDefault, swLabel, bPassword,
- pdfium::make_span(pBuff).first(MAX_INPUT_BYTES));
+ pdfium::as_writable_bytes(buffer.writable_span().first(kMaxWideChars)));
- if (nLengthBytes < 0 || nLengthBytes > MAX_INPUT_BYTES)
+ if (byte_length < 0 || byte_length > kMaxBytes)
return CJS_Result::Failure(JSMessage::kParamTooLongError);
return CJS_Result::Success(pRuntime->NewString(
- WideString::FromUTF16LE(reinterpret_cast<uint16_t*>(pBuff.data()),
- nLengthBytes / sizeof(uint16_t))
+ WideString::FromUTF16LE(buffer.span().data(),
+ byte_length / sizeof(uint16_t))
.AsStringView()));
}