Try to avoid crashing when deserializing JSON

This commit is contained in:
Jacob Dufault 2017-11-18 09:27:37 -08:00
parent 544818bdd4
commit eea8a1b07a
5 changed files with 30 additions and 9 deletions

View File

@ -64,6 +64,7 @@ bool operator!=(const Id<T>& a, const Id<T>& b) {
template <typename T> template <typename T>
void Reflect(Reader& visitor, Id<T>& id) { void Reflect(Reader& visitor, Id<T>& id) {
if (visitor.IsUint64())
id.id = visitor.GetUint64(); id.id = visitor.GetUint64();
} }
template <typename T> template <typename T>

View File

@ -303,6 +303,10 @@ const std::string& lsCompletionItem::InsertedContent() const {
} }
void Reflect(Reader& reader, lsInitializeParams::lsTrace& value) { void Reflect(Reader& reader, lsInitializeParams::lsTrace& value) {
if (!reader.IsString()) {
value = lsInitializeParams::lsTrace::Off;
return;
}
std::string v = reader.GetString(); std::string v = reader.GetString();
if (v == "off") if (v == "off")
value = lsInitializeParams::lsTrace::Off; value = lsInitializeParams::lsTrace::Off;

View File

@ -146,6 +146,9 @@ bool Range::operator<(const Range& that) const {
// Position // Position
void Reflect(Reader& visitor, Position& value) { void Reflect(Reader& visitor, Position& value) {
if (!visitor.IsString())
value = Position();
else
value = Position(visitor.GetString()); value = Position(visitor.GetString());
} }
void Reflect(Writer& visitor, Position& value) { void Reflect(Writer& visitor, Position& value) {
@ -155,6 +158,9 @@ void Reflect(Writer& visitor, Position& value) {
// Range // Range
void Reflect(Reader& visitor, Range& value) { void Reflect(Reader& visitor, Range& value) {
if (!visitor.IsString())
value = Range();
else
value = Range(visitor.GetString()); value = Range(visitor.GetString());
} }
void Reflect(Writer& visitor, Range& value) { void Reflect(Writer& visitor, Range& value) {

View File

@ -8,6 +8,7 @@ bool gTestOutputMode = false;
// int16_t // int16_t
void Reflect(Reader& visitor, int16_t& value) { void Reflect(Reader& visitor, int16_t& value) {
if (visitor.IsInt())
value = (int16_t)visitor.GetInt(); value = (int16_t)visitor.GetInt();
} }
void Reflect(Writer& visitor, int16_t& value) { void Reflect(Writer& visitor, int16_t& value) {
@ -15,6 +16,7 @@ void Reflect(Writer& visitor, int16_t& value) {
} }
// int32_t // int32_t
void Reflect(Reader& visitor, int32_t& value) { void Reflect(Reader& visitor, int32_t& value) {
if (visitor.IsInt())
value = visitor.GetInt(); value = visitor.GetInt();
} }
void Reflect(Writer& visitor, int32_t& value) { void Reflect(Writer& visitor, int32_t& value) {
@ -22,6 +24,7 @@ void Reflect(Writer& visitor, int32_t& value) {
} }
// int64_t // int64_t
void Reflect(Reader& visitor, int64_t& value) { void Reflect(Reader& visitor, int64_t& value) {
if (visitor.IsInt64())
value = visitor.GetInt64(); value = visitor.GetInt64();
} }
void Reflect(Writer& visitor, int64_t& value) { void Reflect(Writer& visitor, int64_t& value) {
@ -29,6 +32,7 @@ void Reflect(Writer& visitor, int64_t& value) {
} }
// uint64_t // uint64_t
void Reflect(Reader& visitor, uint64_t& value) { void Reflect(Reader& visitor, uint64_t& value) {
if (visitor.IsUint64())
value = visitor.GetUint64(); value = visitor.GetUint64();
} }
void Reflect(Writer& visitor, uint64_t& value) { void Reflect(Writer& visitor, uint64_t& value) {
@ -36,6 +40,7 @@ void Reflect(Writer& visitor, uint64_t& value) {
} }
// bool // bool
void Reflect(Reader& visitor, bool& value) { void Reflect(Reader& visitor, bool& value) {
if (visitor.IsBool())
value = visitor.GetBool(); value = visitor.GetBool();
} }
void Reflect(Writer& visitor, bool& value) { void Reflect(Writer& visitor, bool& value) {
@ -43,6 +48,7 @@ void Reflect(Writer& visitor, bool& value) {
} }
// std::string // std::string
void Reflect(Reader& visitor, std::string& value) { void Reflect(Reader& visitor, std::string& value) {
if (visitor.IsString())
value = visitor.GetString(); value = visitor.GetString();
} }
void Reflect(Writer& visitor, std::string& value) { void Reflect(Writer& visitor, std::string& value) {

View File

@ -168,6 +168,8 @@ void ReflectMember(Writer& visitor, const char* name, std::string& value);
// Reader: // Reader:
template <typename T> template <typename T>
void Reflect(Reader& visitor, std::vector<T>& values) { void Reflect(Reader& visitor, std::vector<T>& values) {
if (!visitor.IsArray())
return;
for (auto& entry : visitor.GetArray()) { for (auto& entry : visitor.GetArray()) {
T entry_value; T entry_value;
Reflect(entry, entry_value); Reflect(entry, entry_value);
@ -176,6 +178,8 @@ void Reflect(Reader& visitor, std::vector<T>& values) {
} }
template <typename T> template <typename T>
void Reflect(Reader& visitor, optional<T>& value) { void Reflect(Reader& visitor, optional<T>& value) {
if (visitor.IsNull())
return;
T real_value; T real_value;
Reflect(visitor, real_value); Reflect(visitor, real_value);
value = real_value; value = real_value;