Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: services/url_response_disk_cache/url_response_disk_cache_db.cc

Issue 1800753005: C++ bindings: A struct's Deserialize() now does validation before deserializing. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Address comments. DeserializeWithoutValidation returns void, doesn't take in buf_size. Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/url_response_disk_cache/url_response_disk_cache_db.h" 5 #include "services/url_response_disk_cache/url_response_disk_cache_db.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <limits> 9 #include <limits>
10 #include <string> 10 #include <string>
11 #include <type_traits> 11 #include <type_traits>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "leveldb/comparator.h" 17 #include "leveldb/comparator.h"
18 #include "leveldb/db.h" 18 #include "leveldb/db.h"
19 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
20 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h " 19 #include "services/url_response_disk_cache/url_response_disk_cache_entry.mojom.h "
21 20
22 namespace mojo { 21 namespace mojo {
23 namespace { 22 namespace {
24 23
25 // List of keys for metadata. All metadata keys must start with a '\1' 24 // List of keys for metadata. All metadata keys must start with a '\1'
26 // character. 25 // character.
27 const char kVersionKey[] = "\1version"; 26 const char kVersionKey[] = "\1version";
28 27
29 // TODO(vardhan): Rework these to use the public Serialize/Deserialize 28 // T is a |StructPtr|.
30 // functions.
31 template <typename T> 29 template <typename T>
32 void Serialize(T input, std::string* output) { 30 void Serialize(T input, std::string* output) {
33 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType; 31 size_t size = input->GetSerializedSize();
34 size_t size = GetSerializedSize_(*input);
35
36 output->clear(); 32 output->clear();
37 output->resize(size); 33 output->resize(size);
38 34
39 mojo::internal::FixedBuffer buf; 35 input->Serialize(&output->at(0), size);
40 buf.Initialize(&output->at(0), size);
41
42 DataType data_type;
43 Serialize_(input.get(), &buf, &data_type);
44 std::vector<Handle> handles;
45 data_type->EncodePointersAndHandles(&handles);
46 } 36 }
47 37
48 template <typename T> 38 template <typename T>
49 bool Deserialize(void* data, size_t size, T* output) { 39 bool Deserialize(void* data, size_t size, T* output) {
50 typedef typename mojo::internal::WrapperTraits<T>::DataType DataType;
51 mojo::internal::BoundsChecker bounds_checker(data, size, 0);
52 if (std::remove_pointer<DataType>::type::Validate(data, &bounds_checker,
53 nullptr) !=
54 mojo::internal::ValidationError::NONE)
55 return false;
56
57 DataType data_type = reinterpret_cast<DataType>(data);
58 std::vector<Handle> handles;
59 data_type->DecodePointersAndHandles(&handles);
60 *output = mojo::internal::RemoveStructPtr<T>::type::New(); 40 *output = mojo::internal::RemoveStructPtr<T>::type::New();
61 Deserialize_(data_type, output->get()); 41 return (*output)->Deserialize(data, size);
62 return true;
63 } 42 }
64 43
65 template <typename T> 44 template <typename T>
66 bool Deserialize(std::string s, T* output) { 45 bool Deserialize(std::string s, T* output) {
67 return Deserialize(&s.at(0), s.size(), output); 46 return Deserialize(&s.at(0), s.size(), output);
68 } 47 }
69 48
70 template <typename T> 49 template <typename T>
71 bool Deserialize(const leveldb::Slice& s, T* output) { 50 bool Deserialize(const leveldb::Slice& s, T* output) {
72 return Deserialize(s.ToString(), output); 51 return Deserialize(s.ToString(), output);
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 220 }
242 221
243 scoped_ptr<URLResponseDiskCacheDB::Iterator> 222 scoped_ptr<URLResponseDiskCacheDB::Iterator>
244 URLResponseDiskCacheDB::GetIterator() { 223 URLResponseDiskCacheDB::GetIterator() {
245 return make_scoped_ptr(new Iterator(db_)); 224 return make_scoped_ptr(new Iterator(db_));
246 } 225 }
247 226
248 URLResponseDiskCacheDB::~URLResponseDiskCacheDB() {} 227 URLResponseDiskCacheDB::~URLResponseDiskCacheDB() {}
249 228
250 } // namespace mojo 229 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698