Project

General

Profile

Bug #6566 ยป bug-6566.diff

reject invalid UTF-8 sequence in JSON.generate - nobu (Nobuyoshi Nakada), 06/10/2012 07:19 AM

View differences:

w/ext/json/generator/generator.c
16 16
          i_object_nl, i_array_nl, i_max_nesting, i_allow_nan, i_ascii_only,
17 17
          i_quirks_mode, i_pack, i_unpack, i_create_id, i_extend, i_key_p,
18 18
          i_aref, i_send, i_respond_to_p, i_match, i_keys, i_depth,
19
          i_buffer_initial_length, i_dup;
19
          i_buffer_initial_length, i_dup, i_valid_encoding_p;
20 20

  
21 21
/*
22 22
 * Copyright 2001-2004 Unicode, Inc.
......
726 726

  
727 727
static void generate_json_string(FBuffer *buffer, VALUE Vstate, JSON_Generator_State *state, VALUE obj)
728 728
{
729
    int ascii = state->ascii_only;
729 730
    fbuffer_append_char(buffer, '"');
730 731
#ifdef HAVE_RUBY_ENCODING_H
731 732
    obj = rb_funcall(obj, i_encode, 1, CEncoding_UTF_8);
733
    if (!ascii && !RTEST(rb_funcall(obj, i_valid_encoding_p, 0, 0))) ascii = 1;
732 734
#endif
733
    if (state->ascii_only) {
735
    if (ascii) {
734 736
        convert_UTF8_to_JSON_ASCII(buffer, obj);
735 737
    } else {
736 738
        convert_UTF8_to_JSON(buffer, obj);
......
1387 1389
    CEncoding_UTF_8 = rb_funcall(rb_path2class("Encoding"), rb_intern("find"), 1, rb_str_new2("utf-8"));
1388 1390
    i_encoding = rb_intern("encoding");
1389 1391
    i_encode = rb_intern("encode");
1392
    i_valid_encoding_p = rb_intern("valid_encoding?");
1390 1393
#endif
1391 1394
    i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
1392 1395
    CJSON_SAFE_STATE_PROTOTYPE = Qnil;