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

Fail noisily on unfinished JSON prefixes... #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions ext/yajl/yajl_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@ static VALUE rb_yajl_parser_parse(int argc, VALUE * argv, VALUE self) {
return Qnil;
}

// Because we've overridden the yajl_parse_complete state of the parser to allow
// us to parse multiple objects out of one stream; we can't use it to determine
// whether we've parsed a single object. Instead we'll check whether we've successfully
// parsed a single token.
if (!RARRAY_LEN(wrapper->builderStack) ||
wrapper->nestedHashLevel ||
wrapper->nestedArrayLevel) {
rb_raise(cParseError, "unexpected end of JSON string");
}

return rb_ary_pop(wrapper->builderStack);
}

Expand Down
28 changes: 27 additions & 1 deletion spec/parsing/one_off_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@
output.should == {"key" => 1234}
end

%w(
{"hi":
"worl
1.
tru
nu
[[[[[f
["hey","babe"
[{"wtf...
).each do |example|
it "should not parse #{example.inspect}" do
lambda {
Yajl::Parser.parse(example)
}.should raise_error "unexpected end of JSON string"
end
end

it "should not parse tails after incomplete heads" do
lambda {
Yajl::Parser.parse('{"hi":')
}.should raise_error
lambda {
Yajl::Parser.parse('"hi"}')
}.should raise_error
end

it "should parse numbers greater than 2,147,483,648" do
Yajl::Parser.parse("{\"id\": 2147483649}").should eql({"id" => 2147483649})
Yajl::Parser.parse("{\"id\": 5687389800}").should eql({"id" => 5687389800})
Expand All @@ -78,4 +104,4 @@
Yajl::Parser.parse('{"key": "value"}').values.first.encoding.should eql(Encoding.default_internal)
end
end
end
end