Use size_t for sizes in fxv8.cpp -- perform checked conversions at the API boundary. -- re-order header to match order of functions in .cpp file. Change-Id: Id672988fa455bc4b9547e227996ea44637b47738 Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/87013 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fxjs/fxv8.cpp b/fxjs/fxv8.cpp index 00caf27..f066b3b 100644 --- a/fxjs/fxv8.cpp +++ b/fxjs/fxv8.cpp
@@ -6,6 +6,7 @@ #include "fxjs/fxv8.h" +#include "third_party/base/numerics/safe_conversions.h" #include "v8/include/v8-container.h" #include "v8/include/v8-date.h" #include "v8/include/v8-exception.h" @@ -289,31 +290,36 @@ bool ReentrantPutArrayElementHelper(v8::Isolate* pIsolate, v8::Local<v8::Array> pArray, - unsigned index, + size_t index, v8::Local<v8::Value> pValue) { if (pArray.IsEmpty()) return false; v8::TryCatch squash_exceptions(pIsolate); v8::Maybe<bool> result = - pArray->Set(pIsolate->GetCurrentContext(), index, pValue); + pArray->Set(pIsolate->GetCurrentContext(), + pdfium::base::checked_cast<uint32_t>(index), pValue); return result.IsJust() && result.FromJust(); } v8::Local<v8::Value> ReentrantGetArrayElementHelper(v8::Isolate* pIsolate, v8::Local<v8::Array> pArray, - unsigned index) { + size_t index) { if (pArray.IsEmpty()) return v8::Local<v8::Value>(); v8::TryCatch squash_exceptions(pIsolate); v8::Local<v8::Value> val; - if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val)) + if (!pArray + ->Get(pIsolate->GetCurrentContext(), + pdfium::base::checked_cast<uint32_t>(index)) + .ToLocal(&val)) { return v8::Local<v8::Value>(); + } return val; } -unsigned GetArrayLengthHelper(v8::Local<v8::Array> pArray) { +size_t GetArrayLengthHelper(v8::Local<v8::Array> pArray) { if (pArray.IsEmpty()) return 0; return pArray->Length();
diff --git a/fxjs/fxv8.h b/fxjs/fxv8.h index a370047..23f5e3d 100644 --- a/fxjs/fxv8.h +++ b/fxjs/fxv8.h
@@ -7,13 +7,17 @@ #ifndef FXJS_FXV8_H_ #define FXJS_FXV8_H_ +#include <stddef.h> + #include <vector> #include "core/fxcrt/fx_string.h" #include "third_party/base/span.h" #include "v8/include/v8-forward.h" -// The fxv8 functions soften up the interface to the V8 API. +// The fxv8 functions soften up the interface to the V8 API. In particular, +// PDFium uses size_t for sizes and indices, but V8 mostly uses ints, so +// these routines perform checked conversions. namespace fxv8 { @@ -85,18 +89,18 @@ v8::Local<v8::Object> pObj, ByteStringView bsUTF8PropertyName, v8::Local<v8::Value> pPut); -bool ReentrantPutArrayElementHelper(v8::Isolate* pIsolate, - v8::Local<v8::Array> pArray, - unsigned index, - v8::Local<v8::Value> pValue); void ReentrantDeleteObjectPropertyHelper(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj, ByteStringView bsUTF8PropertyName); + +bool ReentrantPutArrayElementHelper(v8::Isolate* pIsolate, + v8::Local<v8::Array> pArray, + size_t index, + v8::Local<v8::Value> pValue); v8::Local<v8::Value> ReentrantGetArrayElementHelper(v8::Isolate* pIsolate, v8::Local<v8::Array> pArray, - unsigned index); - -unsigned GetArrayLengthHelper(v8::Local<v8::Array> pArray); + size_t index); +size_t GetArrayLengthHelper(v8::Local<v8::Array> pArray); void ThrowExceptionHelper(v8::Isolate* pIsolate, ByteStringView str); void ThrowExceptionHelper(v8::Isolate* pIsolate, WideStringView str);