summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2012-11-29 15:33:55 +0100
committerFlorian Frank <flori@ping.de>2012-11-29 15:35:18 +0100
commitb2da3c25163e61b1ee1509a1b36687b52537f2a3 (patch)
treeb182cb698afc48104f16c98e340faba6b52e34cf
parent103ae8eb39b598964b497601c73b7a6fc8c61ad5 (diff)
parentbdb8e000a7e12e74313d349a4e9b1f7e8b385778 (diff)
downloadjson-b2da3c25163e61b1ee1509a1b36687b52537f2a3.tar.gz
Use len for return values
-rw-r--r--ext/json/ext/generator/generator.c20
-rwxr-xr-xtests/test_json_generate.rb16
2 files changed, 26 insertions, 10 deletions
diff --git a/ext/json/ext/generator/generator.c b/ext/json/ext/generator/generator.c
index ad7dfe3..17f5508 100644
--- a/ext/json/ext/generator/generator.c
+++ b/ext/json/ext/generator/generator.c
@@ -522,7 +522,7 @@ static VALUE cState_configure(VALUE self, VALUE opts)
unsigned long len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
- state->indent = fstrndup(RSTRING_PTR(tmp), len);
+ state->indent = fstrndup(RSTRING_PTR(tmp), len + 1);
state->indent_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_space));
@@ -530,7 +530,7 @@ static VALUE cState_configure(VALUE self, VALUE opts)
unsigned long len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
- state->space = fstrndup(RSTRING_PTR(tmp), len);
+ state->space = fstrndup(RSTRING_PTR(tmp), len + 1);
state->space_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_space_before));
@@ -538,7 +538,7 @@ static VALUE cState_configure(VALUE self, VALUE opts)
unsigned long len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
- state->space_before = fstrndup(RSTRING_PTR(tmp), len);
+ state->space_before = fstrndup(RSTRING_PTR(tmp), len + 1);
state->space_before_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_array_nl));
@@ -546,7 +546,7 @@ static VALUE cState_configure(VALUE self, VALUE opts)
unsigned long len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
- state->array_nl = fstrndup(RSTRING_PTR(tmp), len);
+ state->array_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
state->array_nl_len = len;
}
tmp = rb_hash_aref(opts, ID2SYM(i_object_nl));
@@ -554,7 +554,7 @@ static VALUE cState_configure(VALUE self, VALUE opts)
unsigned long len;
Check_Type(tmp, T_STRING);
len = RSTRING_LEN(tmp);
- state->object_nl = fstrndup(RSTRING_PTR(tmp), len);
+ state->object_nl = fstrndup(RSTRING_PTR(tmp), len + 1);
state->object_nl_len = len;
}
tmp = ID2SYM(i_max_nesting);
@@ -970,7 +970,7 @@ static VALUE cState_from_state_s(VALUE self, VALUE opts)
static VALUE cState_indent(VALUE self)
{
GET_STATE(self);
- return state->indent ? rb_str_new2(state->indent) : rb_str_new2("");
+ return state->indent ? rb_str_new(state->indent, state->indent_len) : rb_str_new2("");
}
/*
@@ -1007,7 +1007,7 @@ static VALUE cState_indent_set(VALUE self, VALUE indent)
static VALUE cState_space(VALUE self)
{
GET_STATE(self);
- return state->space ? rb_str_new2(state->space) : rb_str_new2("");
+ return state->space ? rb_str_new(state->space, state->space_len) : rb_str_new2("");
}
/*
@@ -1044,7 +1044,7 @@ static VALUE cState_space_set(VALUE self, VALUE space)
static VALUE cState_space_before(VALUE self)
{
GET_STATE(self);
- return state->space_before ? rb_str_new2(state->space_before) : rb_str_new2("");
+ return state->space_before ? rb_str_new(state->space_before, state->space_before_len) : rb_str_new2("");
}
/*
@@ -1081,7 +1081,7 @@ static VALUE cState_space_before_set(VALUE self, VALUE space_before)
static VALUE cState_object_nl(VALUE self)
{
GET_STATE(self);
- return state->object_nl ? rb_str_new2(state->object_nl) : rb_str_new2("");
+ return state->object_nl ? rb_str_new(state->object_nl, state->object_nl_len) : rb_str_new2("");
}
/*
@@ -1117,7 +1117,7 @@ static VALUE cState_object_nl_set(VALUE self, VALUE object_nl)
static VALUE cState_array_nl(VALUE self)
{
GET_STATE(self);
- return state->array_nl ? rb_str_new2(state->array_nl) : rb_str_new2("");
+ return state->array_nl ? rb_str_new(state->array_nl, state->array_nl_len) : rb_str_new2("");
}
/*
diff --git a/tests/test_json_generate.rb b/tests/test_json_generate.rb
index f580c12..5a96b05 100755
--- a/tests/test_json_generate.rb
+++ b/tests/test_json_generate.rb
@@ -252,6 +252,22 @@ EOT
end
if defined?(JSON::Ext::Generator)
+ [:merge, :configure].each do |method|
+ define_method "test_configure_using_#{method}" do
+ state = JSON::Ext::Generator::State.new
+ state.send method, :indent => "1",
+ :space => '2',
+ :space_before => '3',
+ :object_nl => '4',
+ :array_nl => '5'
+ assert_equal '1', state.indent
+ assert_equal '2', state.space
+ assert_equal '3', state.space_before
+ assert_equal '4', state.object_nl
+ assert_equal '5', state.array_nl
+ end
+ end
+
def test_broken_bignum # [ruby-core:38867]
pid = fork do
Bignum.class_eval do