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(
String header_content_language =
response_.HttpHeaderField(http_names::kContentLanguage);
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
header_content_language.Truncate(comma_index);
header_content_language =
......
......@@ -377,7 +377,7 @@ void ModuleTreeLinker::FetchDescendants(ModuleScript* module_script) {
//
// [FD] Step 7. These invocations of the internal module script graph fetching
// 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
// procedure given url, fetch client settings object, destination, options,
// module script's settings object, visited set, module script's base URL,
......
......@@ -28,6 +28,7 @@
#include <memory>
#include <utility>
#include "base/numerics/safe_conversions.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_info.h"
......@@ -297,8 +298,9 @@ void ImageResource::DestroyDecodedDataIfPossible() {
GetContent()->DestroyDecodedData();
if (GetContent()->HasImage() && !IsUnusedPreload() &&
GetContent()->IsRefetchableDataFromDiskCache()) {
UMA_HISTOGRAM_MEMORY_KB("Memory.Renderer.EstimatedDroppableEncodedSize",
EncodedSize() / 1024);
UMA_HISTOGRAM_MEMORY_KB(
"Memory.Renderer.EstimatedDroppableEncodedSize",
base::saturated_cast<base::Histogram::Sample>(EncodedSize() / 1024));
}
}
......@@ -335,7 +337,7 @@ scoped_refptr<const SharedBuffer> ImageResource::ResourceBuffer() const {
void ImageResource::AppendData(const char* data, size_t length) {
v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(length);
if (multipart_parser_) {
multipart_parser_->AppendData(data, length);
multipart_parser_->AppendData(data, SafeCast<wtf_size_t>(length));
} else {
Resource::AppendData(data, length);
......
......@@ -77,7 +77,7 @@ int64_t EstimateOriginalImageSizeForPlaceholder(
if (response.HttpHeaderField("chrome-proxy-content-transform") ==
"empty-image") {
const String& str = response.HttpHeaderField("chrome-proxy");
size_t index = str.Find("ofcl=");
wtf_size_t index = str.Find("ofcl=");
if (index != kNotFound) {
bool ok = false;
int bytes = str.Substring(index + (sizeof("ofcl=") - 1)).ToInt(&ok);
......
......@@ -1769,7 +1769,7 @@ TEST(ImageResourceTest,
const struct {
int status_code;
AtomicString content_range;
size_t data_size;
uint32_t data_size;
} tests[] = {
{200, g_null_atom, sizeof(kBadImageData)},
{206, BuildContentRange(sizeof(kBadImageData), sizeof(kBadImageData)),
......
......@@ -23,7 +23,8 @@ MultipartImageResourceParser::MultipartImageResourceParser(
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());
// 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
......@@ -34,7 +35,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) {
if (is_parsing_top_) {
// Eat leading \r\n
size_t pos = SkippableLength(data_, 0);
wtf_size_t pos = SkippableLength(data_, 0);
// +2 for "--"
if (data_.size() < boundary_.size() + 2 + pos) {
// 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) {
return;
}
size_t boundary_position;
wtf_size_t boundary_position;
while ((boundary_position = FindBoundary(data_, &boundary_)) != kNotFound) {
// Strip out trailing \r\n characters in the buffer preceding the boundary
// 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') {
data_size--;
if (boundary_position > 1 && data_[boundary_position - 2] == '\r') {
......@@ -81,7 +82,7 @@ void MultipartImageResourceParser::AppendData(const char* bytes, size_t size) {
if (IsCancelled())
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() &&
'-' == data_[boundary_end_position]) {
// This was the last boundary so we can stop processing.
......@@ -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,
// as we may ignore the last CRLF.
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);
data_.EraseAt(0, send_length);
}
......@@ -124,8 +125,9 @@ void MultipartImageResourceParser::Finish() {
saw_last_boundary_ = true;
}
size_t MultipartImageResourceParser::SkippableLength(const Vector<char>& data,
size_t pos) {
wtf_size_t MultipartImageResourceParser::SkippableLength(
const Vector<char>& data,
wtf_size_t pos) {
if (data.size() >= pos + 2 && data[pos] == '\r' && data[pos + 1] == '\n')
return 2;
if (data.size() >= pos + 1 && data[pos] == '\n')
......@@ -135,7 +137,7 @@ size_t MultipartImageResourceParser::SkippableLength(const Vector<char>& data,
bool MultipartImageResourceParser::ParseHeaders() {
// 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
// replacement headers. We only replace the same few headers that gecko does.
......@@ -159,14 +161,14 @@ bool MultipartImageResourceParser::ParseHeaders() {
// Boundaries are supposed to be preceeded with --, but it looks like gecko
// doesn't require the dashes to exist. See nsMultiMixedConv::FindToken.
size_t MultipartImageResourceParser::FindBoundary(const Vector<char>& data,
Vector<char>* boundary) {
wtf_size_t MultipartImageResourceParser::FindBoundary(const Vector<char>& data,
Vector<char>* boundary) {
auto* it = std::search(data.data(), data.data() + data.size(),
boundary->data(), boundary->data() + boundary->size());
if (it == data.data() + data.size())
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
// TODO(tc): Don't we only want to do this once? Gecko code doesn't seem to
// care.
......
......@@ -64,26 +64,28 @@ class CORE_EXPORT MultipartImageResourceParser final
MultipartImageResourceParser(const ResourceResponse&,
const Vector<char>& boundary,
Client*);
void AppendData(const char* bytes, size_t);
void AppendData(const char* bytes, wtf_size_t);
void Finish();
void Cancel() { is_cancelled_ = true; }
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);
}
static size_t FindBoundaryForTest(const Vector<char>& data,
Vector<char>* boundary) {
static wtf_size_t FindBoundaryForTest(const Vector<char>& data,
Vector<char>* boundary) {
return FindBoundary(data, boundary);
}
private:
bool ParseHeaders();
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|.
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_;
Vector<char> boundary_;
......
......@@ -4,13 +4,14 @@
#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 <stdint.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 multipart_image_resource_parser_test {
......@@ -30,7 +31,7 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
data_.push_back(Vector<char>());
}
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_;
......@@ -40,8 +41,8 @@ class MockClient final : public GarbageCollectedFinalized<MockClient>,
TEST(MultipartResponseTest, SkippableLength) {
struct {
const char* input;
const size_t position;
const size_t expected;
const wtf_size_t position;
const wtf_size_t expected;
} line_tests[] = {
{"Line", 0, 0}, {"Line", 2, 0}, {"Line", 10, 0},
{"\r\nLine", 0, 2}, {"\nLine", 0, 1}, {"\n\nLine", 0, 1},
......@@ -50,7 +51,8 @@ TEST(MultipartResponseTest, SkippableLength) {
};
for (size_t i = 0; i < arraysize(line_tests); ++i) {
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,
MultipartImageResourceParser::SkippableLengthForTest(
input, line_tests[i].position));
......@@ -71,8 +73,9 @@ TEST(MultipartResponseTest, FindBoundary) {
for (size_t i = 0; i < arraysize(boundary_tests); ++i) {
Vector<char> boundary, data;
boundary.Append(boundary_tests[i].boundary,
strlen(boundary_tests[i].boundary));
data.Append(boundary_tests[i].data, strlen(boundary_tests[i].data));
static_cast<uint32_t>(strlen(boundary_tests[i].boundary)));
data.Append(boundary_tests[i].data,
static_cast<uint32_t>(strlen(boundary_tests[i].data)));
EXPECT_EQ(
boundary_tests[i].position,
MultipartImageResourceParser::FindBoundaryForTest(data, &boundary));
......
......@@ -910,7 +910,7 @@ void ThreadableLoader::SetSerializedCachedMetadata(Resource*,
if (!actual_request_.IsNull())
return;
client_->DidReceiveCachedMetadata(data, size);
client_->DidReceiveCachedMetadata(data, SafeCast<int>(size));
}
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