Skip to content

Commit

Permalink
Fix memory leak when exception is raised during JSON generation
Browse files Browse the repository at this point in the history
If an exception is raised the FBuffer is leaked.

For example, the following script leaks memory:

    o = Object.new
    def o.to_json(a) = raise

    10.times do
      100_000.times do
        begin
          JSON(o)
        rescue
        end
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    31824
    35696
    40240
    44304
    47424
    50944
    54000
    58384
    62416
    65296

After:

    24416
    24640
    24640
    24736
    24736
    24736
    24736
    24736
    24736
    24736
  • Loading branch information
peterzhu2118 committed Mar 21, 2024
1 parent 72d8d40 commit 44df509
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ static void generate_json_object(FBuffer *buffer, VALUE Vstate, JSON_Generator_S
struct hash_foreach_arg arg;

if (max_nesting != 0 && depth > max_nesting) {
fbuffer_free(buffer);
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
}
fbuffer_append_char(buffer, '{');
Expand Down Expand Up @@ -927,7 +926,6 @@ static void generate_json_array(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
long depth = ++state->depth;
int i, j;
if (max_nesting != 0 && depth > max_nesting) {
fbuffer_free(buffer);
rb_raise(eNestingError, "nesting of %ld is too deep", --state->depth);
}
fbuffer_append_char(buffer, '[');
Expand Down Expand Up @@ -1020,10 +1018,8 @@ static void generate_json_float(FBuffer *buffer, VALUE Vstate, JSON_Generator_St
VALUE tmp = rb_funcall(obj, i_to_s, 0);
if (!allow_nan) {
if (isinf(value)) {
fbuffer_free(buffer);
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
} else if (isnan(value)) {
fbuffer_free(buffer);
rb_raise(eGeneratorError, "%"PRIsVALUE" not allowed in JSON", RB_OBJ_STRING(tmp));
}
}
Expand Down Expand Up @@ -1096,11 +1092,45 @@ static FBuffer *cState_prepare_buffer(VALUE self)
return buffer;
}

struct generate_json_data {
FBuffer *buffer;
VALUE vstate;
JSON_Generator_State *state;
VALUE obj;
};

static VALUE generate_json_try(VALUE d)
{
struct generate_json_data *data = (struct generate_json_data *)d;

generate_json(data->buffer, data->vstate, data->state, data->obj);

return Qnil;
}

static VALUE generate_json_rescue(VALUE d, VALUE exc)
{
struct generate_json_data *data = (struct generate_json_data *)d;
fbuffer_free(data->buffer);

rb_exc_raise(exc);

return Qundef;
}

static VALUE cState_partial_generate(VALUE self, VALUE obj)
{
FBuffer *buffer = cState_prepare_buffer(self);
GET_STATE(self);
generate_json(buffer, self, state, obj);

struct generate_json_data data = {
.buffer = buffer,
.vstate = self,
.state = state,
.obj = obj
};
rb_rescue(generate_json_try, (VALUE)&data, generate_json_rescue, (VALUE)&data);

return fbuffer_to_s(buffer);
}

Expand Down

0 comments on commit 44df509

Please sign in to comment.