Skip to content

Commit 5abd434

Browse files
etiennebarriebyroot
authored andcommitted
Add depth to struct generate_json_data
Instead of incrementing JSON_Generator_State::depth, we now increment generate_json_data::depth, and only copied at the end.
1 parent ac0a980 commit 5abd434

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

ext/json/ext/generator/generator.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct generate_json_data {
6060
JSON_Generator_State *state;
6161
VALUE obj;
6262
generator_func func;
63+
long depth;
6364
};
6465

6566
static VALUE cState_from_state_s(VALUE self, VALUE opts);
@@ -972,6 +973,8 @@ static inline VALUE vstate_get(struct generate_json_data *data)
972973
if (RB_UNLIKELY(!data->vstate)) {
973974
vstate_spill(data);
974975
}
976+
GET_STATE(data->vstate);
977+
state->depth = data->depth;
975978
return data->vstate;
976979
}
977980

@@ -1145,7 +1148,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
11451148
FBuffer *buffer = data->buffer;
11461149
JSON_Generator_State *state = data->state;
11471150

1148-
long depth = state->depth;
1151+
long depth = data->depth;
11491152
int key_type = rb_type(key);
11501153

11511154
if (arg->first) {
@@ -1219,9 +1222,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
12191222
static inline long increase_depth(struct generate_json_data *data)
12201223
{
12211224
JSON_Generator_State *state = data->state;
1222-
long depth = ++state->depth;
1225+
long depth = ++data->depth;
12231226
if (RB_UNLIKELY(depth > state->max_nesting && state->max_nesting)) {
1224-
rb_raise(eNestingError, "nesting of %ld is too deep. Did you try to serialize objects with circular references?", --state->depth);
1227+
rb_raise(eNestingError, "nesting of %ld is too deep. Did you try to serialize objects with circular references?", --data->depth);
12251228
}
12261229
return depth;
12271230
}
@@ -1232,7 +1235,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
12321235

12331236
if (RHASH_SIZE(obj) == 0) {
12341237
fbuffer_append(buffer, "{}", 2);
1235-
--data->state->depth;
1238+
--data->depth;
12361239
return;
12371240
}
12381241

@@ -1245,7 +1248,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
12451248
};
12461249
rb_hash_foreach(obj, json_object_i, (VALUE)&arg);
12471250

1248-
depth = --data->state->depth;
1251+
depth = --data->depth;
12491252
if (RB_UNLIKELY(data->state->object_nl)) {
12501253
fbuffer_append_str(buffer, data->state->object_nl);
12511254
if (RB_UNLIKELY(data->state->indent)) {
@@ -1261,7 +1264,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
12611264

12621265
if (RARRAY_LEN(obj) == 0) {
12631266
fbuffer_append(buffer, "[]", 2);
1264-
--data->state->depth;
1267+
--data->depth;
12651268
return;
12661269
}
12671270

@@ -1277,7 +1280,7 @@ static void generate_json_array(FBuffer *buffer, struct generate_json_data *data
12771280
}
12781281
generate_json(buffer, data, RARRAY_AREF(obj, i));
12791282
}
1280-
data->state->depth = --depth;
1283+
data->depth = --depth;
12811284
if (RB_UNLIKELY(data->state->array_nl)) {
12821285
fbuffer_append_str(buffer, data->state->array_nl);
12831286
if (RB_UNLIKELY(data->state->indent)) {
@@ -1358,7 +1361,7 @@ static void generate_json_float(FBuffer *buffer, struct generate_json_data *data
13581361
if (casted_obj != obj) {
13591362
increase_depth(data);
13601363
generate_json(buffer, data, casted_obj);
1361-
data->state->depth--;
1364+
data->depth--;
13621365
return;
13631366
}
13641367
}
@@ -1477,6 +1480,7 @@ static VALUE generate_json_ensure(VALUE d)
14771480
{
14781481
struct generate_json_data *data = (struct generate_json_data *)d;
14791482
fbuffer_free(data->buffer);
1483+
data->state->depth = data->depth;
14801484

14811485
return Qundef;
14821486
}
@@ -1495,6 +1499,7 @@ static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func func,
14951499
.buffer = &buffer,
14961500
.vstate = self,
14971501
.state = state,
1502+
.depth = state->depth,
14981503
.obj = obj,
14991504
.func = func
15001505
};
@@ -1541,6 +1546,7 @@ static VALUE cState_generate_new(int argc, VALUE *argv, VALUE self)
15411546
.buffer = &buffer,
15421547
.vstate = Qfalse,
15431548
.state = &new_state,
1549+
.depth = new_state.depth,
15441550
.obj = obj,
15451551
.func = generate_json
15461552
};
@@ -2061,6 +2067,7 @@ static VALUE cState_m_generate(VALUE klass, VALUE obj, VALUE opts, VALUE io)
20612067
.buffer = &buffer,
20622068
.vstate = Qfalse,
20632069
.state = &state,
2070+
.depth = state.depth,
20642071
.obj = obj,
20652072
.func = generate_json,
20662073
};

0 commit comments

Comments
 (0)