Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fail noisily on unfinished JSON prefixes... #69

Open
wants to merge 1 commit into from

1 participant

@ConradIrwin

Fixes #58.

Based on our discussion on IRC, I guess you're not too interested in further patches to the yajl-1-based series, though I consider this a pretty major bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 28, 2011
  1. @ConradIrwin
This page is out of date. Refresh to see the latest.
Showing with 37 additions and 1 deletion.
  1. +10 −0 ext/yajl/yajl_ext.c
  2. +27 −1 spec/parsing/one_off_spec.rb
View
10 ext/yajl/yajl_ext.c
@@ -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);
}
View
28 spec/parsing/one_off_spec.rb
@@ -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})
@@ -78,4 +104,4 @@
Yajl::Parser.parse('{"key": "value"}').values.first.encoding.should eql(Encoding.default_internal)
end
end
-end
+end
Something went wrong with that request. Please try again.