Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 31 additions & 26 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ typedef struct JSON_Generator_StateStruct {
VALUE array_nl;
VALUE as_json;

long max_nesting;
long depth;
long buffer_initial_length;
size_t max_nesting;
size_t depth;
size_t buffer_initial_length;

bool allow_duplicate_key;
bool as_json_single_arg;
Expand Down Expand Up @@ -56,7 +56,7 @@ struct generate_json_data {
JSON_Generator_State *state;
VALUE obj;
generator_func func;
long depth;
size_t depth;
};

#if defined(HAVE_SIMD) && defined(HAVE_SIMD_SSE2)
Expand Down Expand Up @@ -284,7 +284,7 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)

#ifdef HAVE_SIMD

ALWAYS_INLINE(static) char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
ALWAYS_INLINE(static) char *copy_remaining_bytes(search_state *search, size_t vec_len, size_t len)
{
RBIMPL_ASSERT_OR_ASSUME(len < vec_len);

Expand Down Expand Up @@ -396,7 +396,7 @@ static inline unsigned char search_escape_basic_neon(search_state *search)
}

// There are fewer than 16 bytes left.
unsigned long remaining = (search->end - search->ptr);
size_t remaining = (search->end - search->ptr);
if (remaining >= SIMD_MINIMUM_THRESHOLD) {
char *s = copy_remaining_bytes(search, sizeof(uint8x16_t), remaining);

Expand Down Expand Up @@ -479,7 +479,7 @@ ALWAYS_INLINE(static) TARGET_SSE2 unsigned char search_escape_basic_sse2(search_
}

// There are fewer than 16 bytes left.
unsigned long remaining = (search->end - search->ptr);
size_t remaining = (search->end - search->ptr);
if (remaining >= SIMD_MINIMUM_THRESHOLD) {
char *s = copy_remaining_bytes(search, sizeof(__m128i), remaining);

Expand Down Expand Up @@ -966,7 +966,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
FBuffer *buffer = data->buffer;
JSON_Generator_State *state = data->state;

long depth = data->depth;
size_t depth = data->depth;
int key_type = rb_type(key);

if (arg->first) {
Expand Down Expand Up @@ -1040,11 +1040,11 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
return ST_CONTINUE;
}

static inline long increase_depth(struct generate_json_data *data)
static inline size_t increase_depth(struct generate_json_data *data)
{
JSON_Generator_State *state = data->state;
if (RB_UNLIKELY(data->depth >= state->max_nesting && state->max_nesting)) {
rb_raise(eNestingError, "nesting of %ld is too deep. Did you try to serialize objects with circular references?", data->depth + 1);
rb_raise(eNestingError, "nesting of %"PRIuSIZE" is too deep. Did you try to serialize objects with circular references?", data->depth + 1);
}
return ++data->depth;
}
Expand All @@ -1060,7 +1060,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
Check_Type(obj, T_HASH);
}

long depth = increase_depth(data);
size_t depth = increase_depth(data);

if (RHASH_SIZE(obj) == 0) {
fbuffer_append(buffer, "{}", 2);
Expand Down Expand Up @@ -1089,7 +1089,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat

static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
{
long depth = increase_depth(data);
size_t depth = increase_depth(data);

if (RARRAY_LEN(obj) == 0) {
fbuffer_append(buffer, "[]", 2);
Expand Down Expand Up @@ -1639,26 +1639,31 @@ static VALUE cState_check_circular_p(VALUE self)
static VALUE cState_max_nesting(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->max_nesting);
return SIZET2NUM(state->max_nesting);
}

static long long_config(VALUE num)
static size_t max_nesting_config(VALUE num)
{
return RTEST(num) ? NUM2LONG(num) : 0;
if (!RTEST(num)) return 0;
ssize_t max_nesting = NUM2SSIZET(num);
if (RB_UNLIKELY(max_nesting < 0)) {
rb_raise(rb_eArgError, "max_nesting must be >= 0 (got %"PRIdSIZE")", max_nesting);
}
return (size_t)max_nesting;
}

// depth must never be negative; reject early with a clear error.
static long depth_config(VALUE num)
static size_t depth_config(VALUE num)
{
if (!RTEST(num)) return 0;
long d = NUM2LONG(num);
ssize_t d = NUM2SSIZET(num);
if (RB_UNLIKELY(d < 0)) {
rb_raise(rb_eArgError, "depth must be >= 0 (got %ld)", d);
rb_raise(rb_eArgError, "depth must be >= 0 (got %"PRIdSIZE")", d);
}
if (RB_UNLIKELY(d > INT_MAX)) {
rb_raise(rb_eArgError, "depth is too large (got %ld)", d);
rb_raise(rb_eArgError, "depth is too large (got %"PRIdSIZE")", d);
}
return d;
return (size_t)d;
}

/*
Expand All @@ -1671,7 +1676,7 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
{
rb_check_frozen(self);
GET_STATE(self);
state->max_nesting = long_config(depth);
state->max_nesting = max_nesting_config(depth);
return Qnil;
}

Expand Down Expand Up @@ -1897,7 +1902,7 @@ static VALUE cState_allow_duplicate_key_p(VALUE self)
static VALUE cState_depth(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->depth);
return SIZET2NUM(state->depth);
}

/*
Expand All @@ -1921,15 +1926,15 @@ static VALUE cState_depth_set(VALUE self, VALUE depth)
static VALUE cState_buffer_initial_length(VALUE self)
{
GET_STATE(self);
return LONG2FIX(state->buffer_initial_length);
return SIZET2NUM(state->buffer_initial_length);
}

static void buffer_initial_length_set(JSON_Generator_State *state, VALUE buffer_initial_length)
{
Check_Type(buffer_initial_length, T_FIXNUM);
long initial_length = FIX2LONG(buffer_initial_length);
ssize_t initial_length = NUM2SSIZET(buffer_initial_length);
if (initial_length > 0) {
state->buffer_initial_length = initial_length;
state->buffer_initial_length = (size_t)initial_length;
}
}

Expand Down Expand Up @@ -1972,7 +1977,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
else if (key == sym_space_before) { state_write_value(data, &state->space_before, string_config(val)); }
else if (key == sym_object_nl) { state_write_value(data, &state->object_nl, string_config(val)); }
else if (key == sym_array_nl) { state_write_value(data, &state->array_nl, string_config(val)); }
else if (key == sym_max_nesting) { state->max_nesting = long_config(val); }
else if (key == sym_max_nesting) { state->max_nesting = max_nesting_config(val); }
else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); }
else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); }
else if (key == sym_depth) { state->depth = depth_config(val); }
Expand Down
Loading
Loading