Skip to content

Commit c8f3697

Browse files
committed
Use size_t or ssize_t everywhere
The parser and generator had a bunch of `long` and even some `int`. `long` particularly is annoying because it's shorter on Windows, so let's standardize on `size_t` style everywhere.
1 parent 419e966 commit c8f3697

10 files changed

Lines changed: 172 additions & 116 deletions

File tree

‎ext/json/ext/generator/generator.c‎

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ typedef struct JSON_Generator_StateStruct {
1717
VALUE array_nl;
1818
VALUE as_json;
1919

20-
long max_nesting;
21-
long depth;
22-
long buffer_initial_length;
20+
size_t max_nesting;
21+
size_t depth;
22+
size_t buffer_initial_length;
2323

2424
bool allow_duplicate_key;
2525
bool as_json_single_arg;
@@ -56,7 +56,7 @@ struct generate_json_data {
5656
JSON_Generator_State *state;
5757
VALUE obj;
5858
generator_func func;
59-
long depth;
59+
size_t depth;
6060
};
6161

6262
#if defined(HAVE_SIMD) && defined(HAVE_SIMD_SSE2)
@@ -368,7 +368,7 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
368368

369369
#ifdef HAVE_SIMD
370370

371-
ALWAYS_INLINE(static) char *copy_remaining_bytes(search_state *search, unsigned long vec_len, unsigned long len)
371+
ALWAYS_INLINE(static) char *copy_remaining_bytes(search_state *search, size_t vec_len, size_t len)
372372
{
373373
RBIMPL_ASSERT_OR_ASSUME(len < vec_len);
374374

@@ -480,7 +480,7 @@ static inline unsigned char search_escape_basic_neon(search_state *search)
480480
}
481481

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

@@ -563,7 +563,7 @@ ALWAYS_INLINE(static) TARGET_SSE2 unsigned char search_escape_basic_sse2(search_
563563
}
564564

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

@@ -1013,7 +1013,7 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
10131013
FBuffer *buffer = data->buffer;
10141014
JSON_Generator_State *state = data->state;
10151015

1016-
long depth = data->depth;
1016+
size_t depth = data->depth;
10171017
int key_type = rb_type(key);
10181018

10191019
if (arg->first) {
@@ -1087,11 +1087,11 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
10871087
return ST_CONTINUE;
10881088
}
10891089

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

1110-
long depth = increase_depth(data);
1110+
size_t depth = increase_depth(data);
11111111

11121112
if (RHASH_SIZE(obj) == 0) {
11131113
fbuffer_append(buffer, "{}", 2);
@@ -1136,7 +1136,7 @@ static void generate_json_object(FBuffer *buffer, struct generate_json_data *dat
11361136

11371137
static void generate_json_array(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
11381138
{
1139-
long depth = increase_depth(data);
1139+
size_t depth = increase_depth(data);
11401140

11411141
if (RARRAY_LEN(obj) == 0) {
11421142
fbuffer_append(buffer, "[]", 2);
@@ -1687,26 +1687,31 @@ static VALUE cState_check_circular_p(VALUE self)
16871687
static VALUE cState_max_nesting(VALUE self)
16881688
{
16891689
GET_STATE(self);
1690-
return LONG2NUM(state->max_nesting);
1690+
return SIZET2NUM(state->max_nesting);
16911691
}
16921692

1693-
static long long_config(VALUE num)
1693+
static size_t max_nesting_config(VALUE num)
16941694
{
1695-
return RTEST(num) ? NUM2LONG(num) : 0;
1695+
if (!RTEST(num)) return 0;
1696+
ssize_t max_nesting = NUM2SSIZET(num);
1697+
if (RB_UNLIKELY(max_nesting < 0)) {
1698+
rb_raise(rb_eArgError, "max_nesting must be >= 0 (got %"PRIdSIZE")", max_nesting);
1699+
}
1700+
return (size_t)max_nesting;
16961701
}
16971702

16981703
// depth must never be negative; reject early with a clear error.
1699-
static long depth_config(VALUE num)
1704+
static size_t depth_config(VALUE num)
17001705
{
17011706
if (!RTEST(num)) return 0;
1702-
long d = NUM2LONG(num);
1707+
ssize_t d = NUM2SSIZET(num);
17031708
if (RB_UNLIKELY(d < 0)) {
1704-
rb_raise(rb_eArgError, "depth must be >= 0 (got %ld)", d);
1709+
rb_raise(rb_eArgError, "depth must be >= 0 (got %"PRIdSIZE")", d);
17051710
}
17061711
if (RB_UNLIKELY(d > INT_MAX)) {
1707-
rb_raise(rb_eArgError, "depth is too large (got %ld)", d);
1712+
rb_raise(rb_eArgError, "depth is too large (got %"PRIdSIZE")", d);
17081713
}
1709-
return d;
1714+
return (size_t)d;
17101715
}
17111716

17121717
/*
@@ -1719,7 +1724,7 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
17191724
{
17201725
rb_check_frozen(self);
17211726
GET_STATE(self);
1722-
state->max_nesting = long_config(depth);
1727+
state->max_nesting = max_nesting_config(depth);
17231728
return Qnil;
17241729
}
17251730

@@ -1945,7 +1950,7 @@ static VALUE cState_allow_duplicate_key_p(VALUE self)
19451950
static VALUE cState_depth(VALUE self)
19461951
{
19471952
GET_STATE(self);
1948-
return LONG2FIX(state->depth);
1953+
return SIZET2NUM(state->depth);
19491954
}
19501955

19511956
/*
@@ -1969,15 +1974,15 @@ static VALUE cState_depth_set(VALUE self, VALUE depth)
19691974
static VALUE cState_buffer_initial_length(VALUE self)
19701975
{
19711976
GET_STATE(self);
1972-
return LONG2FIX(state->buffer_initial_length);
1977+
return SIZET2NUM(state->buffer_initial_length);
19731978
}
19741979

19751980
static void buffer_initial_length_set(JSON_Generator_State *state, VALUE buffer_initial_length)
19761981
{
19771982
Check_Type(buffer_initial_length, T_FIXNUM);
1978-
long initial_length = FIX2LONG(buffer_initial_length);
1983+
ssize_t initial_length = NUM2SSIZET(buffer_initial_length);
19791984
if (initial_length > 0) {
1980-
state->buffer_initial_length = initial_length;
1985+
state->buffer_initial_length = (size_t)initial_length;
19811986
}
19821987
}
19831988

@@ -2020,7 +2025,7 @@ static int configure_state_i(VALUE key, VALUE val, VALUE _arg)
20202025
else if (key == sym_space_before) { state_write_value(data, &state->space_before, string_config(val)); }
20212026
else if (key == sym_object_nl) { state_write_value(data, &state->object_nl, string_config(val)); }
20222027
else if (key == sym_array_nl) { state_write_value(data, &state->array_nl, string_config(val)); }
2023-
else if (key == sym_max_nesting) { state->max_nesting = long_config(val); }
2028+
else if (key == sym_max_nesting) { state->max_nesting = max_nesting_config(val); }
20242029
else if (key == sym_allow_nan) { state->allow_nan = RTEST(val); }
20252030
else if (key == sym_ascii_only) { state->ascii_only = RTEST(val); }
20262031
else if (key == sym_depth) { state->depth = depth_config(val); }

0 commit comments

Comments
 (0)