Commit c5ea9b95 authored by Chris Palmer's avatar Chris Palmer Committed by Commit Bot

Use checked_cast instead of static_cast when casting sizes in Pickle.

These functions should arguably have been defined to take uint32_t all along.
Use checked_cast to see if signedness or width bugs have been lurking in the
shadows.

Bug: None
Change-Id: Ieef9244f1ecf8e72b7dc4d5e85a42007b145d932
Reviewed-on: https://chromium-review.googlesource.com/676694Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarTom Sepez <tsepez@chromium.org>
Commit-Queue: Chris Palmer <palmer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#504195}
parent 1963fcc8
......@@ -119,14 +119,14 @@ void WriteValue(base::Pickle* m, const base::Value* value, int recursion) {
}
case base::Value::Type::BINARY: {
m->WriteData(value->GetBlob().data(),
static_cast<int>(value->GetBlob().size()));
base::checked_cast<int>(value->GetBlob().size()));
break;
}
case base::Value::Type::DICTIONARY: {
const base::DictionaryValue* dict =
static_cast<const base::DictionaryValue*>(value);
WriteParam(m, static_cast<int>(dict->size()));
WriteParam(m, base::checked_cast<int>(dict->size()));
for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
it.Advance()) {
......@@ -137,7 +137,7 @@ void WriteValue(base::Pickle* m, const base::Value* value, int recursion) {
}
case base::Value::Type::LIST: {
const base::ListValue* list = static_cast<const base::ListValue*>(value);
WriteParam(m, static_cast<int>(list->GetSize()));
WriteParam(m, base::checked_cast<int>(list->GetSize()));
for (const auto& entry : *list) {
WriteValue(m, &entry, recursion + 1);
}
......@@ -402,7 +402,7 @@ void ParamTraits<std::vector<char>>::Write(base::Pickle* m,
if (p.empty()) {
m->WriteData(NULL, 0);
} else {
m->WriteData(&p.front(), static_cast<int>(p.size()));
m->WriteData(&p.front(), base::checked_cast<int>(p.size()));
}
}
......@@ -429,7 +429,7 @@ void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m,
m->WriteData(NULL, 0);
} else {
m->WriteData(reinterpret_cast<const char*>(&p.front()),
static_cast<int>(p.size()));
base::checked_cast<int>(p.size()));
}
}
......@@ -453,7 +453,7 @@ void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p,
void ParamTraits<std::vector<bool>>::Write(base::Pickle* m,
const param_type& p) {
WriteParam(m, static_cast<int>(p.size()));
WriteParam(m, base::checked_cast<int>(p.size()));
// Cast to bool below is required because libc++'s
// vector<bool>::const_reference is different from bool, and we want to avoid
// writing an extra specialization of ParamTraits for it.
......
......@@ -365,7 +365,7 @@ template <class P>
struct ParamTraits<std::vector<P>> {
typedef std::vector<P> param_type;
static void Write(base::Pickle* m, const param_type& p) {
WriteParam(m, static_cast<int>(p.size()));
WriteParam(m, base::checked_cast<int>(p.size()));
for (size_t i = 0; i < p.size(); i++)
WriteParam(m, p[i]);
}
......@@ -399,7 +399,7 @@ template <class P>
struct ParamTraits<std::set<P> > {
typedef std::set<P> param_type;
static void Write(base::Pickle* m, const param_type& p) {
WriteParam(m, static_cast<int>(p.size()));
WriteParam(m, base::checked_cast<int>(p.size()));
typename param_type::const_iterator iter;
for (iter = p.begin(); iter != p.end(); ++iter)
WriteParam(m, *iter);
......@@ -427,7 +427,7 @@ template <class K, class V, class C, class A>
struct ParamTraits<std::map<K, V, C, A> > {
typedef std::map<K, V, C, A> param_type;
static void Write(base::Pickle* m, const param_type& p) {
WriteParam(m, static_cast<int>(p.size()));
WriteParam(m, base::checked_cast<int>(p.size()));
typename param_type::const_iterator iter;
for (iter = p.begin(); iter != p.end(); ++iter) {
WriteParam(m, iter->first);
......@@ -695,7 +695,7 @@ template <class P, size_t stack_capacity>
struct ParamTraits<base::StackVector<P, stack_capacity> > {
typedef base::StackVector<P, stack_capacity> param_type;
static void Write(base::Pickle* m, const param_type& p) {
WriteParam(m, static_cast<int>(p->size()));
WriteParam(m, base::checked_cast<int>(p->size()));
for (size_t i = 0; i < p->size(); i++)
WriteParam(m, p[i]);
}
......@@ -735,7 +735,7 @@ struct ParamTraits<base::small_map<NormalMap, kArraySize, EqualKey, MapInit>> {
using K = typename param_type::key_type;
using V = typename param_type::data_type;
static void Write(base::Pickle* m, const param_type& p) {
WriteParam(m, static_cast<int>(p.size()));
WriteParam(m, base::checked_cast<int>(p.size()));
typename param_type::const_iterator iter;
for (iter = p.begin(); iter != p.end(); ++iter) {
WriteParam(m, iter->first);
......@@ -768,7 +768,7 @@ struct ParamTraits<base::flat_map<Key, Mapped, Compare>> {
using param_type = base::flat_map<Key, Mapped, Compare>;
static void Write(base::Pickle* m, const param_type& p) {
DCHECK(base::IsValueInRangeForNumericType<int>(p.size()));
WriteParam(m, static_cast<int>(p.size()));
WriteParam(m, base::checked_cast<int>(p.size()));
for (const auto& iter : p) {
WriteParam(m, iter.first);
WriteParam(m, iter.second);
......
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