Skip to content

Commit

Permalink
Add error checking of object deserialization; ignore non-object initi…
Browse files Browse the repository at this point in the history
…alizationOptions
  • Loading branch information
MaskRay committed Mar 29, 2019
1 parent 4808ccb commit 4c871d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/messages/initialize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ void Reflect(JsonReader &reader, InitializeParam::Trace &value) {
value = InitializeParam::Trace::Verbose;
}

REFLECT_STRUCT(InitializeParam, rootUri, initializationOptions, capabilities,
trace, workspaceFolders);
// initializationOptions is deserialized separately.
REFLECT_STRUCT(InitializeParam, rootUri, capabilities, trace, workspaceFolders);

struct InitializeResult {
ServerCap capabilities;
Expand Down Expand Up @@ -384,6 +384,17 @@ void Initialize(MessageHandler *m, InitializeParam &param, ReplyOnce &reply) {
void MessageHandler::initialize(JsonReader &reader, ReplyOnce &reply) {
InitializeParam param;
Reflect(reader, param);
auto it = reader.m->FindMember("initializationOptions");
if (it != reader.m->MemberEnd() && it->value.IsObject()) {
JsonReader m1(&it->value);
try {
Reflect(m1, param.initializationOptions);
} catch (std::invalid_argument &) {
reader.path_.push_back("initializationOptions");
reader.path_.insert(reader.path_.end(), m1.path_.begin(), m1.path_.end());
throw;
}
}
if (!param.rootUri) {
reply.Error(ErrorCode::InvalidRequest, "expected rootUri");
return;
Expand Down
5 changes: 5 additions & 0 deletions src/serializer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,11 @@ void Reflect(JsonWriter &vis, SerializeFormat &v) {
}
}

void ReflectMemberStart(JsonReader &vis) {
if (!vis.m->IsObject())
throw std::invalid_argument("object");
}

static BumpPtrAllocator Alloc;
static DenseSet<CachedHashStringRef> Strings;
static std::mutex AllocMutex;
Expand Down
1 change: 1 addition & 0 deletions src/serializer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ template <typename T> void Reflect(BinaryWriter &vis, std::vector<T> &v) {

// ReflectMember

void ReflectMemberStart(JsonReader &);
template <typename T> void ReflectMemberStart(T &) {}
inline void ReflectMemberStart(JsonWriter &vis) { vis.StartObject(); }

Expand Down

0 comments on commit 4c871d9

Please sign in to comment.