Project

General

Profile

Bug #5173 ยป 0001-ext-json-generator-generator.c-prevent-GC-for-tempor.patch

normalperson (Eric Wong), 08/09/2011 10:45 AM

View differences:

ext/json/generator/generator.c
}
}
static void fbuffer_append_str(FBuffer *fb, VALUE str)
{
const char *newstr = RSTRING_PTR(str);
unsigned long len = RSTRING_LEN(str);
RB_GC_GUARD(str);
fbuffer_append(fb, newstr, len);
}
static void fbuffer_append_char(FBuffer *fb, char newchr)
{
fbuffer_inc_capa(fb, 1);
......
static void generate_json_bignum(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
{
VALUE tmp = rb_funcall(obj, i_to_s, 0);
fbuffer_append(buffer, RSTRING_PAIR(tmp));
fbuffer_append_str(buffer, tmp);
}
static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
......
rb_raise(eGeneratorError, "%u: %s not allowed in JSON", __LINE__, StringValueCStr(tmp));
}
}
fbuffer_append(buffer, RSTRING_PAIR(tmp));
fbuffer_append_str(buffer, tmp);
}
static void generate_json(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
......
} else if (rb_respond_to(obj, i_to_json)) {
tmp = rb_funcall(obj, i_to_json, 1, Vstate);
Check_Type(tmp, T_STRING);
fbuffer_append(buffer, RSTRING_PAIR(tmp));
fbuffer_append_str(buffer, tmp);
} else {
tmp = rb_funcall(obj, i_to_s, 0);
Check_Type(tmp, T_STRING);
ext/json/generator/generator.h
#define RSTRING_LEN(string) RSTRING(string)->len
#endif
#define RSTRING_PAIR(string) RSTRING_PTR(string), RSTRING_LEN(string)
/* fbuffer implementation */
typedef struct FBufferStruct {
test/json/test_json_generate.rb
assert_raises(JSON::NestingError) { ary.to_json(s) }
assert_equal 19, s.depth
end
def test_gc
bignum_too_long_to_embed_as_string = 1234567890123456789012345
expect = bignum_too_long_to_embed_as_string.to_s
stress, GC.stress = GC.stress, true
10.times do |i|
tmp = bignum_too_long_to_embed_as_string.to_json
assert_equal expect, tmp
end
ensure
GC.stress = stress
end
end
    (1-1/1)