Commit 1ffab702 authored by pkalinnikov's avatar pkalinnikov Committed by Commit bot

Fix UnindexedRulesetWriter finishing.

This CL fixes the bug where UnindexedRulesetWriter doesn't trim the
CodedOutputStream on Finish, resulting in the output having some extra
uninitialized bytes at the end.

BUG=609747

Review-Url: https://codereview.chromium.org/2279803002
Cr-Commit-Position: refs/heads/master@{#414762}
parent 0e8bc9ea
......@@ -300,7 +300,7 @@ bool RulesetService::IndexRuleset(base::File unindexed_ruleset_file,
"SubresourceFilter.IndexRuleset.NumUnsupportedRules",
num_unsupported_rules);
return zero_copy_stream_adaptor.ByteCount() == unindexed_ruleset_size;
return reader.num_bytes_read() == unindexed_ruleset_size;
}
// static
......
......@@ -51,7 +51,10 @@ bool UnindexedRulesetWriter::AddUrlRule(const proto::UrlRule& rule) {
bool UnindexedRulesetWriter::Finish() {
DCHECK(!had_error());
return !pending_chunk_.url_rules_size() || WritePendingChunk();
const bool success = !pending_chunk_.url_rules_size() || WritePendingChunk();
if (success)
coded_stream_.Trim();
return success;
}
bool UnindexedRulesetWriter::WritePendingChunk() {
......
......@@ -42,6 +42,9 @@ class UnindexedRulesetReader {
// ReadNextChunk is undefined befaviour.
bool ReadNextChunk(proto::FilteringRules* chunk);
// Returns how many bytes of the |stream| have been consumed.
int num_bytes_read() const { return coded_stream_.CurrentPosition(); }
private:
google::protobuf::io::CodedInputStream coded_stream_;
......
......@@ -9,6 +9,7 @@
#include <vector>
#include "base/macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "components/subresource_filter/core/common/proto/rules.pb.h"
#include "components/subresource_filter/core/common/url_pattern.h"
......@@ -122,6 +123,10 @@ bool IsRulesetValid(const std::string& ruleset_contents,
read_rules.insert(read_rules.end(), chunk.url_rules().begin(),
chunk.url_rules().end());
}
if (base::checked_cast<size_t>(reader.num_bytes_read()) !=
ruleset_contents.size()) {
return false;
}
if (expected_url_rules.size() != read_rules.size())
return false;
......
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