Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON parser fix #40804

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions core/io/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,13 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in
return err;
}
value = d;
return OK;
} else if (token.type == TK_BRACKET_OPEN) {
Array a;
Error err = _parse_array(a, p_str, index, p_len, line, r_err_str);
if (err) {
return err;
}
value = a;
return OK;

} else if (token.type == TK_IDENTIFIER) {
String id = token.value;
if (id == "true") {
Expand All @@ -323,18 +320,16 @@ Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, in
r_err_str = "Expected 'true','false' or 'null', got '" + id + "'.";
return ERR_PARSE_ERROR;
}
return OK;

} else if (token.type == TK_NUMBER) {
value = token.value;
return OK;
} else if (token.type == TK_STRING) {
value = token.value;
return OK;
} else {
r_err_str = "Expected value, got " + String(tk_name[token.type]) + ".";
return ERR_PARSE_ERROR;
}

return OK;
}

Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) {
Expand Down Expand Up @@ -453,6 +448,19 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int &

err = _parse_value(r_ret, token, str, idx, len, r_err_line, r_err_str);

// Check if EOF is reached
// or it's a type of the next token.
if (err == OK && idx < len) {
err = _get_token(str, idx, len, token, r_err_line, r_err_str);

if (err || token.type != TK_EOF) {
r_err_str = "Expected 'EOF'";
// Reset return value to empty `Variant`
r_ret = Variant();
return ERR_PARSE_ERROR;
Comment on lines +458 to +460
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just tested now, I confirm it resets the value to Variant() (aka null in GDScript), all assertions in #40795 pass!

This is the case where I'm not really sure whether returning a "half-way" token even on parse error is intended, but I'm not sure if this "bug" was used as a "feature" before. 🙂

}
}

return err;
}

Expand Down