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

Unified Diff: content/renderer/dom_storage/local_storage_cached_area.cc

Issue 2748023008: Future-proof data format used for LocalStorage. (Closed)
Patch Set: no CHECK, just silently ignore broken data for now Created 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/dom_storage/local_storage_cached_area.cc
diff --git a/content/renderer/dom_storage/local_storage_cached_area.cc b/content/renderer/dom_storage/local_storage_cached_area.cc
index 77ab50b3df3bbe74cc0378a03532e5343b6054ce..2bb78e3190c49a6b4683de455f1486cc7acdb07e 100644
--- a/content/renderer/dom_storage/local_storage_cached_area.cc
+++ b/content/renderer/dom_storage/local_storage_cached_area.cc
@@ -24,14 +24,32 @@ namespace content {
namespace {
+// Don't change or reorder any of the values in this enum, as these values
+// are serialized on disk.
+enum class StorageFormat : uint8_t { UTF16 = 0 };
+
base::string16 Uint8VectorToString16(const std::vector<uint8_t>& input) {
- return base::string16(reinterpret_cast<const base::char16*>(input.data()),
- input.size() / sizeof(base::char16));
+ // TODO(mek): Better error recovery when corrupt (or otherwise invalid) data
+ // is detected.
+ if (input.size() % sizeof(base::char16) != 1 ||
+ input[0] != static_cast<uint8_t>(StorageFormat::UTF16)) {
+ VLOG(1) << "Corrupt data in localstorage";
+ return base::string16();
+ }
+ base::string16 result;
+ result.resize(input.size() / sizeof(base::char16));
+ std::memcpy(reinterpret_cast<void*>(&result[0]), input.data() + 1,
+ input.size() - 1);
+ return result;
}
std::vector<uint8_t> String16ToUint8Vector(const base::string16& input) {
const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data());
- return std::vector<uint8_t>(data, data + input.size() * sizeof(base::char16));
+ std::vector<uint8_t> result;
+ result.reserve(input.size() * sizeof(base::char16) + 1);
+ result.push_back(static_cast<uint8_t>(StorageFormat::UTF16));
+ result.insert(result.end(), data, data + input.size() * sizeof(base::char16));
+ return result;
}
class GetAllCallback : public mojom::LevelDBWrapperGetAllCallback {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698