Commit e7eed439 authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Fix 64 bit truncations in core/loader

Use appropriate types in loader code.

BUG=879657

Change-Id: I3d94d7a6d6b3dce98792f4bf055a871f49d5a2da
Reviewed-on: https://chromium-review.googlesource.com/c/1332507Reviewed-by: default avatarDavid Bokan <bokan@chromium.org>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/master@{#608032}
parent e1e4445d
...@@ -1007,7 +1007,7 @@ void DocumentLoader::DidInstallNewDocument( ...@@ -1007,7 +1007,7 @@ void DocumentLoader::DidInstallNewDocument(
String header_content_language = String header_content_language =
response_.HttpHeaderField(http_names::kContentLanguage); response_.HttpHeaderField(http_names::kContentLanguage);
if (!header_content_language.IsEmpty()) { if (!header_content_language.IsEmpty()) {
size_t comma_index = header_content_language.find(','); wtf_size_t comma_index = header_content_language.find(',');
// kNotFound == -1 == don't truncate // kNotFound == -1 == don't truncate
header_content_language.Truncate(comma_index); header_content_language.Truncate(comma_index);
header_content_language = header_content_language =
......
...@@ -377,7 +377,7 @@ void ModuleTreeLinker::FetchDescendants(ModuleScript* module_script) { ...@@ -377,7 +377,7 @@ void ModuleTreeLinker::FetchDescendants(ModuleScript* module_script) {
// //
// [FD] Step 7. These invocations of the internal module script graph fetching // [FD] Step 7. These invocations of the internal module script graph fetching
// procedure should be performed in parallel to each other. // procedure should be performed in parallel to each other.
for (size_t i = 0; i < urls.size(); ++i) { for (wtf_size_t i = 0; i < urls.size(); ++i) {
// [FD] Step 7. ... perform the internal module script graph fetching // [FD] Step 7. ... perform the internal module script graph fetching
// procedure given url, fetch client settings object, destination, options, // procedure given url, fetch client settings object, destination, options,
// module script's settings object, visited set, module script's base URL, // module script's settings object, visited set, module script's base URL,
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include "base/numerics/safe_conversions.h"
#include "third_party/blink/public/platform/platform.h" #include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_content.h" #include "third_party/blink/renderer/core/loader/resource/image_resource_content.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_info.h" #include "third_party/blink/renderer/core/loader/resource/image_resource_info.h"
...@@ -297,8 +298,9 @@ void ImageResource::DestroyDecodedDataIfPossible() { ...@@ -297,8 +298,9 @@ void ImageResource::DestroyDecodedDataIfPossible() {
GetContent()->DestroyDecodedData(); GetContent()->DestroyDecodedData();
if (GetContent()->HasImage() && !IsUnusedPreload() && if (GetContent()->HasImage() && !IsUnusedPreload() &&
GetContent()->IsRefetchableDataFromDiskCache()) { GetContent()->IsRefetchableDataFromDiskCache()) {
UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer.EstimatedDroppableEncodedSize", UMA_HISTOGRAM_MEMORY_KB(
EncodedSize() / 1024); "Memory.Renderer.EstimatedDroppableEncodedSize",
base::saturated_cast<base::Histogram::Sample>(EncodedSize() / 1024));
} }
} }
...@@ -335,7 +337,7 @@ scoped_refptr<const SharedBuffer> ImageResource::ResourceBuffer() const { ...@@ -335,7 +337,7 @@ scoped_refptr<const SharedBuffer> ImageResource::ResourceBuffer() const {
void ImageResource::AppendData(const char* data, size_t length) { void ImageResource::AppendData(const char* data, size_t length) {
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(length); v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(length);
if (multipart_parser_) { if (multipart_parser_) {
multipart_parser_->AppendData(data, length); multipart_parser_->AppendData(data, SafeCast<wtf_size_t>(length));
} else { } else {
Resource::AppendData(data, length); Resource::AppendData(data, length);
......
...@@ -77,7 +77,7 @@ int64_t EstimateOriginalImageSizeForPlaceholder( ...@@ -77,7 +77,7 @@ int64_t EstimateOriginalImageSizeForPlaceholder(
if (response.HttpHeaderField("chrome-proxy-content-transform") == if (response.HttpHeaderField("chrome-proxy-content-transform") ==
"empty-image") { "empty-image") {
const String& str = response.HttpHeaderField("chrome-proxy"); const String& str = response.HttpHeaderField("chrome-proxy");
size_t index = str.Find("ofcl="); wtf_size_t index = str.Find("ofcl=");
if (index != kNotFound) { if (index != kNotFound) {
bool ok = false; bool ok = false;
int bytes = str.Substring(index + (sizeof("ofcl=") - 1)).ToInt(&ok); int bytes = str.Substring(index + (sizeof("ofcl=") - 1)).ToInt(&ok);
......
...@@ -1769,7 +1769,7 @@ TEST(ImageResourceTest, ...@@ -1769,7 +1769,7 @@ TEST(ImageResourceTest,
const struct { const struct {
int status_code; int status_code;
AtomicString content_range; AtomicString content_range;
size_t data_size; uint32_t data_size;
} tests[] = { } tests[] = {
{200, g_null_atom, sizeof(kBadImageData)}, {200, g_null_atom, sizeof(kBadImageData)},
{206, BuildContentRange(sizeof(kBadImageData), sizeof(kBadImageData)), {206, BuildContentRange(sizeof(kBadImageData), sizeof(kBadImageData)),
......
...@@ -23,7 +23,8 @@ MultipartImageResourceParser::MultipartImageResourceParser( ...@@ -23,7 +23,8 @@ MultipartImageResourceParser::MultipartImageResourceParser(
boundary_.push_front("--", 2); boundary_.push_front("--", 2);
} }
void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) { void MultipartImageResourceParser::AppendData(const char* bytes,
wtf_size_t size) {
DCHECK(!IsCancelled()); DCHECK(!IsCancelled());
// m_sawLastBoundary means that we've already received the final boundary // m_sawLastBoundary means that we've already received the final boundary
// token. The server should stop sending us data at this point, but if it // token. The server should stop sending us data at this point, but if it
...@@ -34,7 +35,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) { ...@@ -34,7 +35,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) {
if (is_parsing_top_) { if (is_parsing_top_) {
// Eat leading \r\n // Eat leading \r\n
size_t pos = SkippableLength(data_, 0); wtf_size_t pos = SkippableLength(data_, 0);
// +2 for "--" // +2 for "--"
if (data_.size() < boundary_.size() + 2 + pos) { if (data_.size() < boundary_.size() + 2 + pos) {
// We don't have enough data yet to make a boundary token. Just wait // We don't have enough data yet to make a boundary token. Just wait
...@@ -65,11 +66,11 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) { ...@@ -65,11 +66,11 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) {
return; return;
} }
size_t boundary_position; wtf_size_t boundary_position;
while ((boundary_position = FindBoundary(data_, &boundary_)) != kNotFound) { while ((boundary_position = FindBoundary(data_, &boundary_)) != kNotFound) {
// Strip out trailing \r\n characters in the buffer preceding the boundary // Strip out trailing \r\n characters in the buffer preceding the boundary
// on the same lines as does Firefox. // on the same lines as does Firefox.
size_t data_size = boundary_position; wtf_size_t data_size = boundary_position;
if (boundary_position > 0 && data_[boundary_position - 1] == '\n') { if (boundary_position > 0 && data_[boundary_position - 1] == '\n') {
data_size--; data_size--;
if (boundary_position > 1 && data_[boundary_position - 2] == '\r') { if (boundary_position > 1 && data_[boundary_position - 2] == '\r') {
...@@ -81,7 +82,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) { ...@@ -81,7 +82,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) {
if (IsCancelled()) if (IsCancelled())
return; return;
} }
size_t boundary_end_position = boundary_position + boundary_.size(); wtf_size_t boundary_end_position = boundary_position + boundary_.size();
if (boundary_end_position < data_.size() && if (boundary_end_position < data_.size() &&
'-' == data_[boundary_end_position]) { '-' == data_[boundary_end_position]) {
// This was the last boundary so we can stop processing. // This was the last boundary so we can stop processing.
...@@ -106,7 +107,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) { ...@@ -106,7 +107,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) {
// buffered to handle a boundary that may have been truncated. "+2" for CRLF, // buffered to handle a boundary that may have been truncated. "+2" for CRLF,
// as we may ignore the last CRLF. // as we may ignore the last CRLF.
if (!is_parsing_headers_ && data_.size() > boundary_.size() + 2) { if (!is_parsing_headers_ && data_.size() > boundary_.size() + 2) {
size_t send_length = data_.size() - boundary_.size() - 2; wtf_size_t send_length = data_.size() - boundary_.size() - 2;
client_->MultipartDataReceived(data_.data(), send_length); client_->MultipartDataReceived(data_.data(), send_length);
data_.EraseAt(0, send_length); data_.EraseAt(0, send_length);
} }
...@@ -124,8 +125,9 @@ void MultipartImageResourceParser::Finish() { ...@@ -124,8 +125,9 @@ void MultipartImageResourceParser::Finish() {
saw_last_boundary_ = true; saw_last_boundary_ = true;
} }
size_t MultipartImageResourceParser::SkippableLength(const Vector<char>& data, wtf_size_t MultipartImageResourceParser::SkippableLength(
size_t pos) { const Vector<char>& data,
wtf_size_t pos) {
if (data.size() >= pos + 2 && data[pos] == '\r' && data[pos + 1] == '\n') if (data.size() >= pos + 2 && data[pos] == '\r' && data[pos + 1] == '\n')
return 2; return 2;
if (data.size() >= pos + 1 && data[pos] == '\n') if (data.size() >= pos + 1 && data[pos] == '\n')
...@@ -135,7 +137,7 @@ size_t MultipartImageResourceParser::SkippableLength(const Vector<char>& data, ...@@ -135,7 +137,7 @@ size_t MultipartImageResourceParser::SkippableLength(const Vector<char>& data,
bool MultipartImageResourceParser::ParseHeaders() { bool MultipartImageResourceParser::ParseHeaders() {
// Eat leading \r\n // Eat leading \r\n
size_t pos = SkippableLength(data_, 0); wtf_size_t pos = SkippableLength(data_, 0);
// Create a ResourceResponse based on the original set of headers + the // Create a ResourceResponse based on the original set of headers + the
// replacement headers. We only replace the same few headers that gecko does. // replacement headers. We only replace the same few headers that gecko does.
...@@ -159,14 +161,14 @@ bool MultipartImageResourceParser::ParseHeaders() { ...@@ -159,14 +161,14 @@ bool MultipartImageResourceParser::ParseHeaders() {
// Boundaries are supposed to be preceeded with --, but it looks like gecko // Boundaries are supposed to be preceeded with --, but it looks like gecko
// doesn't require the dashes to exist. See nsMultiMixedConv::FindToken. // doesn't require the dashes to exist. See nsMultiMixedConv::FindToken.
size_t MultipartImageResourceParser::FindBoundary(const Vector<char>& data, wtf_size_t MultipartImageResourceParser::FindBoundary(const Vector<char>& data,
Vector<char>* boundary) { Vector<char>* boundary) {
auto* it = std::search(data.data(), data.data() + data.size(), auto* it = std::search(data.data(), data.data() + data.size(),
boundary->data(), boundary->data() + boundary->size()); boundary->data(), boundary->data() + boundary->size());
if (it == data.data() + data.size()) if (it == data.data() + data.size())
return kNotFound; return kNotFound;
size_t boundary_position = it - data.data(); wtf_size_t boundary_position = static_cast<wtf_size_t>(it - data.data());
// Back up over -- for backwards compat // Back up over -- for backwards compat
// TODO(tc): Don't we only want to do this once? Gecko code doesn't seem to // TODO(tc): Don't we only want to do this once? Gecko code doesn't seem to
// care. // care.
......
...@@ -64,26 +64,28 @@ class CORE_EXPORT MultipartImageResourceParser final ...@@ -64,26 +64,28 @@ class CORE_EXPORT MultipartImageResourceParser final
MultipartImageResourceParser(const ResourceResponse&, MultipartImageResourceParser(const ResourceResponse&,
const Vector<char>& boundary, const Vector<char>& boundary,
Client*); Client*);
void AppendData(const char* bytes, size_t); void AppendData(const char* bytes, wtf_size_t);
void Finish(); void Finish();
void Cancel() { is_cancelled_ = true; } void Cancel() { is_cancelled_ = true; }
void Trace(blink::Visitor*); void Trace(blink::Visitor*);
static size_t SkippableLengthForTest(const Vector<char>& data, size_t size) { static wtf_size_t SkippableLengthForTest(const Vector<char>& data,
wtf_size_t size) {
return SkippableLength(data, size); return SkippableLength(data, size);
} }
static size_t FindBoundaryForTest(const Vector<char>& data, static wtf_size_t FindBoundaryForTest(const Vector<char>& data,
Vector<char>* boundary) { Vector<char>* boundary) {
return FindBoundary(data, boundary); return FindBoundary(data, boundary);
} }
private: private:
bool ParseHeaders(); bool ParseHeaders();
bool IsCancelled() const { return is_cancelled_; } bool IsCancelled() const { return is_cancelled_; }
static size_t SkippableLength(const Vector<char>&, size_t); static wtf_size_t SkippableLength(const Vector<char>&, wtf_size_t);
// This function updates |*boundary|. // This function updates |*boundary|.
static size_t FindBoundary(const Vector<char>& data, Vector<char>* boundary); static wtf_size_t FindBoundary(const Vector<char>& data,
Vector<char>* boundary);
const ResourceResponse original_response_; const ResourceResponse original_response_;
Vector<char> boundary_; Vector<char> boundary_;
......
...@@ -4,13 +4,14 @@ ...@@ -4,13 +4,14 @@
#include "third_party/blink/renderer/core/loader/resource/multipart_image_resource_parser.h" #include "third_party/blink/renderer/core/loader/resource/multipart_image_resource_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
namespace blink { namespace blink {
namespace multipart_image_resource_parser_test { namespace multipart_image_resource_parser_test {
...@@ -30,7 +31,7 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>, ...@@ -30,7 +31,7 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
data_.push_back(Vector<char>()); data_.push_back(Vector<char>());
} }
void MultipartDataReceived(const char* bytes, size_t size) override { void MultipartDataReceived(const char* bytes, size_t size) override {
data_.back().Append(bytes, size); data_.back().Append(bytes, SafeCast<wtf_size_t>(size));
} }
Vector<ResourceResponse> responses_; Vector<ResourceResponse> responses_;
...@@ -40,8 +41,8 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>, ...@@ -40,8 +41,8 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
TEST(MultipartResponseTest, SkippableLength) { TEST(MultipartResponseTest, SkippableLength) {
struct { struct {
const char* input; const char* input;
const size_t position; const wtf_size_t position;
const size_t expected; const wtf_size_t expected;
} line_tests[] = { } line_tests[] = {
{"Line", 0, 0}, {"Line", 2, 0}, {"Line", 10, 0}, {"Line", 0, 0}, {"Line", 2, 0}, {"Line", 10, 0},
{"\r\nLine", 0, 2}, {"\nLine", 0, 1}, {"\n\nLine", 0, 1}, {"\r\nLine", 0, 2}, {"\nLine", 0, 1}, {"\n\nLine", 0, 1},
...@@ -50,7 +51,8 @@ TEST(MultipartResponseTest, SkippableLength) { ...@@ -50,7 +51,8 @@ TEST(MultipartResponseTest, SkippableLength) {
}; };
for (size_t i = 0; i < arraysize(line_tests); ++i) { for (size_t i = 0; i < arraysize(line_tests); ++i) {
Vector<char> input; Vector<char> input;
input.Append(line_tests[i].input, strlen(line_tests[i].input)); input.Append(line_tests[i].input,
static_cast<wtf_size_t>(strlen(line_tests[i].input)));
EXPECT_EQ(line_tests[i].expected, EXPECT_EQ(line_tests[i].expected,
MultipartImageResourceParser::SkippableLengthForTest( MultipartImageResourceParser::SkippableLengthForTest(
input, line_tests[i].position)); input, line_tests[i].position));
...@@ -71,8 +73,9 @@ TEST(MultipartResponseTest, FindBoundary) { ...@@ -71,8 +73,9 @@ TEST(MultipartResponseTest, FindBoundary) {
for (size_t i = 0; i < arraysize(boundary_tests); ++i) { for (size_t i = 0; i < arraysize(boundary_tests); ++i) {
Vector<char> boundary, data; Vector<char> boundary, data;
boundary.Append(boundary_tests[i].boundary, boundary.Append(boundary_tests[i].boundary,
strlen(boundary_tests[i].boundary)); static_cast<uint32_t>(strlen(boundary_tests[i].boundary)));
data.Append(boundary_tests[i].data, strlen(boundary_tests[i].data)); data.Append(boundary_tests[i].data,
static_cast<uint32_t>(strlen(boundary_tests[i].data)));
EXPECT_EQ( EXPECT_EQ(
boundary_tests[i].position, boundary_tests[i].position,
MultipartImageResourceParser::FindBoundaryForTest(data, &boundary)); MultipartImageResourceParser::FindBoundaryForTest(data, &boundary));
......
...@@ -910,7 +910,7 @@ void ThreadableLoader::SetSerializedCachedMetadata(Resource*, ...@@ -910,7 +910,7 @@ void ThreadableLoader::SetSerializedCachedMetadata(Resource*,
if (!actual_request_.IsNull()) if (!actual_request_.IsNull())
return; return;
client_->DidReceiveCachedMetadata(data, size); client_->DidReceiveCachedMetadata(data, SafeCast<int>(size));
} }
void ThreadableLoader::DataReceived(Resource* resource, void ThreadableLoader::DataReceived(Resource* resource,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment