oj 3.17.3 → 3.17.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -0
  3. data/ext/oj/cache.c +72 -39
  4. data/ext/oj/cache.h +5 -0
  5. data/ext/oj/circarray.c +6 -2
  6. data/ext/oj/code.c +38 -28
  7. data/ext/oj/code.h +2 -0
  8. data/ext/oj/compat.c +63 -31
  9. data/ext/oj/custom.c +43 -8
  10. data/ext/oj/dump.c +120 -41
  11. data/ext/oj/dump.h +10 -0
  12. data/ext/oj/dump_compat.c +19 -15
  13. data/ext/oj/dump_object.c +38 -12
  14. data/ext/oj/dump_strict.c +2 -1
  15. data/ext/oj/extconf.rb +7 -0
  16. data/ext/oj/fast.c +132 -72
  17. data/ext/oj/intern.c +80 -77
  18. data/ext/oj/mimic_json.c +5 -29
  19. data/ext/oj/object.c +47 -17
  20. data/ext/oj/odd.c +6 -2
  21. data/ext/oj/oj.c +393 -44
  22. data/ext/oj/oj.h +10 -3
  23. data/ext/oj/parse.c +75 -7
  24. data/ext/oj/parse.h +23 -2
  25. data/ext/oj/parser.c +219 -71
  26. data/ext/oj/parser.h +1 -1
  27. data/ext/oj/rails.c +188 -45
  28. data/ext/oj/reader.c +22 -6
  29. data/ext/oj/reader.h +1 -0
  30. data/ext/oj/resolve.c +16 -6
  31. data/ext/oj/resolve.h +9 -0
  32. data/ext/oj/rxclass.c +16 -0
  33. data/ext/oj/rxclass.h +1 -0
  34. data/ext/oj/safe.c +62 -48
  35. data/ext/oj/safe.h +14 -2
  36. data/ext/oj/saj.c +60 -17
  37. data/ext/oj/saj2.c +44 -28
  38. data/ext/oj/simd.h +5 -3
  39. data/ext/oj/sparse.c +23 -6
  40. data/ext/oj/stream_writer.c +19 -1
  41. data/ext/oj/strict.c +42 -4
  42. data/ext/oj/string_writer.c +40 -10
  43. data/ext/oj/usual.c +17 -5
  44. data/ext/oj/util.c +27 -0
  45. data/ext/oj/util.h +2 -1
  46. data/ext/oj/val_stack.c +11 -22
  47. data/ext/oj/val_stack.h +70 -30
  48. data/ext/oj/wab.c +17 -26
  49. data/lib/oj/generic_object.rb +82 -0
  50. data/lib/oj/json.rb +1 -71
  51. data/lib/oj/mimic.rb +0 -1
  52. data/lib/oj/version.rb +1 -1
  53. data/pages/Compatibility.md +1 -1
  54. data/pages/Custom.md +2 -3
  55. data/pages/InstallOptions.md +8 -6
  56. data/pages/Modes.md +1 -1
  57. data/pages/Options.md +5 -6
  58. data/pages/Parser.md +2 -2
  59. data/pages/Rails.md +6 -0
  60. data/pages/Security.md +21 -7
  61. metadata +17 -16
data/ext/oj/safe.c CHANGED
@@ -5,7 +5,7 @@ static VALUE max_hash_size_sym, max_array_size_sym, max_depth_sym, max_total_ele
5
5
  max_array_size_error_class, max_depth_error_class, max_total_elements_error_class;
6
6
 
7
7
  static void check_object_size(safe_T safe) {
8
- if (NIL_P(safe->max_hash_size)) {
8
+ if (!safe->max_hash_size_isset) {
9
9
  return;
10
10
  }
11
11
 
@@ -23,7 +23,7 @@ static void check_object_size(safe_T safe) {
23
23
  }
24
24
 
25
25
  static void check_array_size(safe_T safe) {
26
- if (NIL_P(safe->max_array_size)) {
26
+ if (!safe->max_array_size_isset) {
27
27
  return;
28
28
  }
29
29
 
@@ -41,7 +41,7 @@ static void check_array_size(safe_T safe) {
41
41
  }
42
42
 
43
43
  static void check_max_depth(safe_T safe, ojParser p) {
44
- if (NIL_P(safe->max_depth) || safe->max_depth >= (p->depth + 1)) {
44
+ if (!safe->max_depth_isset || safe->max_depth >= (p->depth + 1)) {
45
45
  return;
46
46
  }
47
47
 
@@ -55,7 +55,7 @@ static void check_max_total_elements(safe_T safe) {
55
55
  * null, true) are not counted. As a result, `current_elements_count`
56
56
  * always holds one less than the actual total.
57
57
  */
58
- if (NIL_P(safe->max_total_elements) || safe->max_total_elements > safe->current_elements_count) {
58
+ if (!safe->max_total_elements_isset || safe->max_total_elements > safe->current_elements_count) {
59
59
  return;
60
60
  }
61
61
 
@@ -140,58 +140,72 @@ DEFINE_DELEGATED_OBJECT_FUNCTION(add_float);
140
140
  DEFINE_DELEGATED_OBJECT_FUNCTION(add_big);
141
141
  DEFINE_DELEGATED_OBJECT_FUNCTION(add_str);
142
142
 
143
+ // The counting wrappers live in the parser function table, and the option
144
+ // setters of the usual parser write to the same slots, so an option set after
145
+ // the parser was built puts the unwrapped handler back and the limits stop
146
+ // being applied for that kind of value.
147
+ #define WRAP(funcs, slot, wrapper, saved) \
148
+ do { \
149
+ if (wrapper != (funcs)->slot) { \
150
+ safe->saved = (funcs)->slot; \
151
+ } \
152
+ (funcs)->slot = wrapper; \
153
+ } while (0)
154
+
155
+ static void wrap_funcs(ojParser p, safe_T safe) {
156
+ Funcs f = &p->funcs[ARRAY_FUN];
157
+
158
+ WRAP(f, open_object, safe_open_object, delegated_open_object_func);
159
+ WRAP(f, open_array, safe_open_array, delegated_open_array_func);
160
+ // The following overrides are done for counting objects
161
+ WRAP(f, add_null, safe_add_null, delegated_add_null_func);
162
+ WRAP(f, add_true, safe_add_true, delegated_add_true_func);
163
+ WRAP(f, add_false, safe_add_false, delegated_add_false_func);
164
+ WRAP(f, add_int, safe_add_int, delegated_add_int_func);
165
+ WRAP(f, add_float, safe_add_float, delegated_add_float_func);
166
+ WRAP(f, add_big, safe_add_big, delegated_add_big_func);
167
+ WRAP(f, add_str, safe_add_str, delegated_add_str_func);
168
+
169
+ f = &p->funcs[OBJECT_FUN];
170
+ WRAP(f, open_object, safe_open_object_key, delegated_open_object_key_func);
171
+ WRAP(f, open_array, safe_open_array_key, delegated_open_array_key_func);
172
+ WRAP(f, add_null, safe_add_null_key, delegated_add_null_key_func);
173
+ WRAP(f, add_true, safe_add_true_key, delegated_add_true_key_func);
174
+ WRAP(f, add_false, safe_add_false_key, delegated_add_false_key_func);
175
+ WRAP(f, add_int, safe_add_int_key, delegated_add_int_key_func);
176
+ WRAP(f, add_float, safe_add_float_key, delegated_add_float_key_func);
177
+ WRAP(f, add_big, safe_add_big_key, delegated_add_big_key_func);
178
+ WRAP(f, add_str, safe_add_str_key, delegated_add_str_key_func);
179
+ }
180
+
181
+ static VALUE safe_option(ojParser p, const char *key, VALUE value) {
182
+ safe_T safe = (safe_T)p->ctx;
183
+ VALUE rv;
184
+
185
+ if (0 == strcmp("omit_null", key)) {
186
+ return safe->omit_null ? Qtrue : Qfalse;
187
+ }
188
+ rv = safe->delegated_option_func(p, key, value);
189
+ if (0 == strcmp("omit_null=", key)) {
190
+ safe->omit_null = (Qtrue == rv);
191
+ }
192
+ wrap_funcs(p, safe);
193
+
194
+ return rv;
195
+ }
196
+
143
197
  void oj_init_safe_parser(ojParser p, safe_T safe, VALUE options) {
144
198
  // Safe parser inherits all members of usual parser
145
199
  oj_init_usual(p, &safe->usual);
146
200
 
147
201
  safe->delegated_start_func = p->start;
148
202
  p->start = safe_start;
203
+ safe->omit_null = false;
149
204
 
150
- Funcs f;
205
+ wrap_funcs(p, safe);
151
206
 
152
- // Array parser functions
153
- f = &p->funcs[ARRAY_FUN];
154
- safe->delegated_open_object_func = f->open_object;
155
- f->open_object = safe_open_object;
156
- safe->delegated_open_array_func = f->open_array;
157
- f->open_array = safe_open_array;
158
- // The following overrides are done for counting objects
159
- safe->delegated_add_null_func = f->add_null;
160
- f->add_null = safe_add_null;
161
- safe->delegated_add_true_func = f->add_true;
162
- f->add_true = safe_add_true;
163
- safe->delegated_add_false_func = f->add_false;
164
- f->add_false = safe_add_false;
165
- safe->delegated_add_int_func = f->add_int;
166
- f->add_int = safe_add_int;
167
- safe->delegated_add_float_func = f->add_float;
168
- f->add_float = safe_add_float;
169
- safe->delegated_add_big_func = f->add_big;
170
- f->add_big = safe_add_big;
171
- safe->delegated_add_str_func = f->add_str;
172
- f->add_str = safe_add_str;
173
-
174
- // Object parser functions
175
- f = &p->funcs[OBJECT_FUN];
176
- safe->delegated_open_object_key_func = f->open_object;
177
- f->open_object = safe_open_object_key;
178
- safe->delegated_open_array_key_func = f->open_array;
179
- f->open_array = safe_open_array_key;
180
- // The following overrides are done for counting objects
181
- safe->delegated_add_null_key_func = f->add_null;
182
- f->add_null = safe_add_null_key;
183
- safe->delegated_add_true_key_func = f->add_true;
184
- f->add_true = safe_add_true_key;
185
- safe->delegated_add_false_key_func = f->add_false;
186
- f->add_false = safe_add_false_key;
187
- safe->delegated_add_int_key_func = f->add_int;
188
- f->add_int = safe_add_int_key;
189
- safe->delegated_add_float_key_func = f->add_float;
190
- f->add_float = safe_add_float_key;
191
- safe->delegated_add_big_key_func = f->add_big;
192
- f->add_big = safe_add_big_key;
193
- safe->delegated_add_str_key_func = f->add_str;
194
- f->add_str = safe_add_str_key;
207
+ safe->delegated_option_func = p->option;
208
+ p->option = safe_option;
195
209
 
196
210
  SET_CONFIG(max_hash_size);
197
211
  SET_CONFIG(max_array_size);
data/ext/oj/safe.h CHANGED
@@ -8,11 +8,13 @@
8
8
  VALUE rb_##config_name = rb_hash_aref(options, config_name##_sym); \
9
9
  \
10
10
  if (RB_INTEGER_TYPE_P(rb_##config_name)) { \
11
- safe->config_name = NUM2LONG(rb_##config_name); \
11
+ safe->config_name = NUM2LONG(rb_##config_name); \
12
+ safe->config_name##_isset = true; \
12
13
  } else if (!NIL_P(rb_##config_name)) { \
13
14
  rb_raise(rb_eArgError, "Incorrect value provided for `" #config_name "`"); \
14
15
  } else { \
15
- safe->config_name = Qnil; \
16
+ safe->config_name = 0; \
17
+ safe->config_name##_isset = false; \
16
18
  } \
17
19
  } while (0);
18
20
 
@@ -49,10 +51,20 @@ typedef struct _safe_S {
49
51
  long int max_total_elements;
50
52
  long int max_json_size_bytes;
51
53
 
54
+ bool max_hash_size_isset;
55
+ bool max_array_size_isset;
56
+ bool max_depth_isset;
57
+ bool max_total_elements_isset;
58
+
52
59
  long int current_hash_size;
53
60
  long int current_array_size;
54
61
  long int current_elements_count;
55
62
 
63
+ // omit_null is kept here because the usual parser reports it by looking at
64
+ // the add_null slot, which is one of the slots wrapped below.
65
+ bool omit_null;
66
+
67
+ VALUE (*delegated_option_func)(struct _ojParser *p, const char *key, VALUE value);
56
68
  void (*delegated_start_func)(struct _ojParser *p);
57
69
 
58
70
  // Array functions
data/ext/oj/saj.c CHANGED
@@ -82,7 +82,14 @@ inline static void next_non_white(ParseInfo pi) {
82
82
  case '\f':
83
83
  case '\n':
84
84
  case '\r': break;
85
- case '/': skip_comment(pi); break;
85
+ case '/':
86
+ skip_comment(pi);
87
+ /* A comment that is not terminated by a newline ends on the null
88
+ * terminator. Stop here so the loop does not step past it. */
89
+ if ('\0' == *pi->s) {
90
+ return;
91
+ }
92
+ break;
86
93
  default: return;
87
94
  }
88
95
  }
@@ -118,14 +125,15 @@ static void skip_comment(ParseInfo pi) {
118
125
  if ('*' == *pi->s && '/' == *(pi->s + 1)) {
119
126
  pi->s++;
120
127
  return;
121
- } else if ('\0' == *pi->s) {
122
- if (pi->has_error) {
123
- call_error("comment not terminated", pi, __FILE__, __LINE__);
124
- } else {
125
- raise_error("comment not terminated", pi->str, pi->s);
126
- }
127
128
  }
128
129
  }
130
+ /* The loop only ends on the null terminator so the comment was never
131
+ * closed. */
132
+ if (pi->has_error) {
133
+ call_error("comment not terminated", pi, __FILE__, __LINE__);
134
+ } else {
135
+ raise_error("comment not terminated", pi->str, pi->s);
136
+ }
129
137
  } else if ('/' == *pi->s) {
130
138
  for (; 1; pi->s++) {
131
139
  switch (*pi->s) {
@@ -190,6 +198,12 @@ static void read_hash(ParseInfo pi, const char *key) {
190
198
  } else {
191
199
  while (1) {
192
200
  next_non_white(pi);
201
+ if ('"' != *pi->s) {
202
+ if (pi->has_error) {
203
+ call_error("invalid format, expected a key", pi, __FILE__, __LINE__);
204
+ }
205
+ raise_error("invalid format, expected a key", pi->str, pi->s);
206
+ }
193
207
  ks = read_quoted_value(pi);
194
208
  next_non_white(pi);
195
209
  if (':' == *pi->s) {
@@ -606,6 +620,19 @@ static void saj_parse(VALUE handler, char *json) {
606
620
  }
607
621
  }
608
622
 
623
+ struct _sajArgs {
624
+ VALUE handler;
625
+ char *json;
626
+ };
627
+
628
+ static VALUE protect_saj_parse(VALUE x) {
629
+ struct _sajArgs *args = (struct _sajArgs *)x;
630
+
631
+ saj_parse(args->handler, args->json);
632
+
633
+ return Qnil;
634
+ }
635
+
609
636
  /* call-seq: saj_parse(handler, io)
610
637
  *
611
638
  * Parses an IO stream or file containing an JSON document. Raises an exception
@@ -619,13 +646,14 @@ static void saj_parse(VALUE handler, char *json) {
619
646
  */
620
647
  VALUE
621
648
  oj_saj_parse(int argc, VALUE *argv, VALUE self) {
622
- char *json = 0;
623
- size_t len = 0;
624
- VALUE input = argv[1];
649
+ char *json = 0;
650
+ size_t len = 0;
651
+ VALUE input;
625
652
 
626
653
  if (argc < 2) {
627
654
  rb_raise(rb_eArgError, "Wrong number of arguments to saj_parse.\n");
628
655
  }
656
+ input = argv[1];
629
657
  if (rb_type(input) == T_STRING) {
630
658
  // the json string gets modified so make a copy of it
631
659
  len = RSTRING_LEN(input) + 1;
@@ -636,16 +664,20 @@ oj_saj_parse(int argc, VALUE *argv, VALUE self) {
636
664
  volatile VALUE s;
637
665
 
638
666
  if (oj_stringio_class == clas) {
639
- s = rb_funcall2(input, oj_string_id, 0, 0);
667
+ s = rb_funcall2(input, oj_string_id, 0, 0);
668
+ StringValueCStr(s);
640
669
  len = RSTRING_LEN(s) + 1;
641
670
  json = OJ_R_ALLOC_N(char, len);
642
- strcpy(json, StringValueCStr(s));
671
+ strcpy(json, RSTRING_PTR(s));
643
672
  #if !IS_WINDOWS
644
673
  } else if (rb_cFile == clas && 0 == FIX2INT(rb_funcall(input, oj_pos_id, 0))) {
645
674
  int fd = FIX2INT(rb_funcall(input, oj_fileno_id, 0));
646
675
  ssize_t cnt;
647
676
 
648
677
  len = lseek(fd, 0, SEEK_END);
678
+ if ((size_t)-1 == len) {
679
+ rb_raise(rb_eIOError, "failed to read from IO Object.");
680
+ }
649
681
  lseek(fd, 0, SEEK_SET);
650
682
  json = OJ_R_ALLOC_N(char, len + 1);
651
683
  {
@@ -654,6 +686,7 @@ oj_saj_parse(int argc, VALUE *argv, VALUE self) {
654
686
  while (total < len) {
655
687
  cnt = read(fd, json + total, len - total);
656
688
  if (cnt <= 0) {
689
+ OJ_R_FREE(json);
657
690
  rb_raise(rb_eIOError, "failed to read from IO Object.");
658
691
  }
659
692
  total += cnt;
@@ -662,16 +695,26 @@ oj_saj_parse(int argc, VALUE *argv, VALUE self) {
662
695
  json[len] = '\0';
663
696
  #endif
664
697
  } else if (rb_respond_to(input, oj_read_id)) {
665
- s = rb_funcall2(input, oj_read_id, 0, 0);
698
+ s = rb_funcall2(input, oj_read_id, 0, 0);
699
+ StringValueCStr(s);
666
700
  len = RSTRING_LEN(s) + 1;
667
701
  json = OJ_R_ALLOC_N(char, len);
668
- strcpy(json, StringValueCStr(s));
702
+ strcpy(json, RSTRING_PTR(s));
669
703
  } else {
670
704
  rb_raise(rb_eArgError, "saj_parse() expected a String or IO Object.");
671
705
  }
672
706
  }
673
- saj_parse(*argv, json);
674
- OJ_R_FREE(json);
675
-
707
+ {
708
+ // saj_parse() raises on a malformed document so the json buffer has to
709
+ // be freed even when the parse does not return normally.
710
+ struct _sajArgs args = {*argv, json};
711
+ int ex = 0;
712
+
713
+ rb_protect(protect_saj_parse, (VALUE)&args, &ex);
714
+ OJ_R_FREE(json);
715
+ if (0 != ex) {
716
+ rb_jump_tag(ex);
717
+ }
718
+ }
676
719
  return Qnil;
677
720
  }
data/ext/oj/saj2.c CHANGED
@@ -33,9 +33,30 @@ static void push_key(Saj d, VALUE key) {
33
33
  d->tail++;
34
34
  }
35
35
 
36
+ static VALUE pop_key(Saj d) {
37
+ if (d->tail <= d->keys) {
38
+ rb_raise(rb_eIndexError, "accessing key stack");
39
+ }
40
+ d->tail--;
41
+
42
+ return *d->tail;
43
+ }
44
+
36
45
  static void noop(ojParser p) {
37
46
  }
38
47
 
48
+ static void open_push_key(ojParser p) {
49
+ push_key((Saj)p->ctx, get_key(p));
50
+ }
51
+
52
+ static void close_pop_key(ojParser p) {
53
+ Saj d = (Saj)p->ctx;
54
+
55
+ if (d->keys < d->tail) {
56
+ d->tail--;
57
+ }
58
+ }
59
+
39
60
  static void open_object(ojParser p) {
40
61
  rb_funcall(((Saj)p->ctx)->handler, oj_hash_start_id, 1, Qnil);
41
62
  }
@@ -89,11 +110,7 @@ static void close_object(ojParser p) {
89
110
  VALUE key = Qnil;
90
111
 
91
112
  if (OBJECT_FUN == p->stack[p->depth]) {
92
- d->tail--;
93
- if (d->tail < d->keys) {
94
- rb_raise(rb_eIndexError, "accessing key stack");
95
- }
96
- key = *d->tail;
113
+ key = pop_key(d);
97
114
  }
98
115
  rb_funcall(d->handler, oj_hash_end_id, 1, key);
99
116
  }
@@ -103,11 +120,7 @@ static void close_object_loc(ojParser p) {
103
120
  VALUE key = Qnil;
104
121
 
105
122
  if (OBJECT_FUN == p->stack[p->depth]) {
106
- d->tail--;
107
- if (d->tail < d->keys) {
108
- rb_raise(rb_eIndexError, "accessing key stack");
109
- }
110
- key = *d->tail;
123
+ key = pop_key(d);
111
124
  }
112
125
  rb_funcall(d->handler, oj_hash_end_id, 3, key, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
113
126
  }
@@ -117,11 +130,7 @@ static void close_array(ojParser p) {
117
130
  VALUE key = Qnil;
118
131
 
119
132
  if (OBJECT_FUN == p->stack[p->depth]) {
120
- d->tail--;
121
- if (d->tail < d->keys) {
122
- rb_raise(rb_eIndexError, "accessing key stack");
123
- }
124
- key = *d->tail;
133
+ key = pop_key(d);
125
134
  }
126
135
  rb_funcall(d->handler, oj_array_end_id, 1, key);
127
136
  }
@@ -131,11 +140,7 @@ static void close_array_loc(ojParser p) {
131
140
  VALUE key = Qnil;
132
141
 
133
142
  if (OBJECT_FUN == p->stack[p->depth]) {
134
- d->tail--;
135
- if (d->tail < d->keys) {
136
- rb_raise(rb_eIndexError, "accessing key stack");
137
- }
138
- key = *d->tail;
143
+ key = pop_key(d);
139
144
  }
140
145
  rb_funcall(d->handler, oj_array_end_id, 3, key, LONG2FIX(p->line), LONG2FIX(p->cur - p->col));
141
146
  }
@@ -380,10 +385,15 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
380
385
  return d->handler;
381
386
  }
382
387
  if (0 == strcmp(key, "handler=")) {
388
+ int hash_start = rb_respond_to(value, oj_hash_start_id);
389
+ int hash_end = rb_respond_to(value, oj_hash_end_id);
390
+ int array_start = rb_respond_to(value, oj_array_start_id);
391
+ int array_end = rb_respond_to(value, oj_array_end_id);
392
+
383
393
  d->tail = d->keys;
384
394
  d->handler = value;
385
395
  reset(p);
386
- if (rb_respond_to(value, oj_hash_start_id)) {
396
+ if (hash_start) {
387
397
  if (1 == rb_obj_method_arity(value, oj_hash_start_id)) {
388
398
  p->funcs[TOP_FUN].open_object = open_object;
389
399
  p->funcs[ARRAY_FUN].open_object = open_object;
@@ -393,8 +403,10 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
393
403
  p->funcs[ARRAY_FUN].open_object = open_object_loc;
394
404
  p->funcs[OBJECT_FUN].open_object = open_object_loc_key;
395
405
  }
406
+ } else if (hash_end) {
407
+ p->funcs[OBJECT_FUN].open_object = open_push_key;
396
408
  }
397
- if (rb_respond_to(value, oj_array_start_id)) {
409
+ if (array_start) {
398
410
  if (1 == rb_obj_method_arity(value, oj_array_start_id)) {
399
411
  p->funcs[TOP_FUN].open_array = open_array;
400
412
  p->funcs[ARRAY_FUN].open_array = open_array;
@@ -404,8 +416,10 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
404
416
  p->funcs[ARRAY_FUN].open_array = open_array_loc;
405
417
  p->funcs[OBJECT_FUN].open_array = open_array_loc_key;
406
418
  }
419
+ } else if (array_end) {
420
+ p->funcs[OBJECT_FUN].open_array = open_push_key;
407
421
  }
408
- if (rb_respond_to(value, oj_hash_end_id)) {
422
+ if (hash_end) {
409
423
  if (1 == rb_obj_method_arity(value, oj_hash_end_id)) {
410
424
  p->funcs[TOP_FUN].close_object = close_object;
411
425
  p->funcs[ARRAY_FUN].close_object = close_object;
@@ -415,8 +429,10 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
415
429
  p->funcs[ARRAY_FUN].close_object = close_object_loc;
416
430
  p->funcs[OBJECT_FUN].close_object = close_object_loc;
417
431
  }
432
+ } else if (hash_start) {
433
+ p->funcs[OBJECT_FUN].close_object = close_pop_key;
418
434
  }
419
- if (rb_respond_to(value, oj_array_end_id)) {
435
+ if (array_end) {
420
436
  if (1 == rb_obj_method_arity(value, oj_array_end_id)) {
421
437
  p->funcs[TOP_FUN].close_array = close_array;
422
438
  p->funcs[ARRAY_FUN].close_array = close_array;
@@ -426,6 +442,8 @@ static VALUE option(ojParser p, const char *key, VALUE value) {
426
442
  p->funcs[ARRAY_FUN].close_array = close_array_loc;
427
443
  p->funcs[OBJECT_FUN].close_array = close_array_loc;
428
444
  }
445
+ } else if (array_start) {
446
+ p->funcs[OBJECT_FUN].close_array = close_pop_key;
429
447
  }
430
448
  if (rb_respond_to(value, oj_add_value_id)) {
431
449
  if (2 == rb_obj_method_arity(value, oj_add_value_id)) {
@@ -547,10 +565,8 @@ static void mark(ojParser p) {
547
565
  if (Qnil != d->handler) {
548
566
  rb_gc_mark(d->handler);
549
567
  }
550
- if (!d->cache_keys) {
551
- for (kp = d->keys; kp < d->tail; kp++) {
552
- rb_gc_mark(*kp);
553
- }
568
+ for (kp = d->keys; kp < d->tail; kp++) {
569
+ rb_gc_mark(*kp);
554
570
  }
555
571
  }
556
572
 
data/ext/oj/simd.h CHANGED
@@ -98,10 +98,10 @@ static inline int oj_ctz64_fallback(uint64_t x) {
98
98
  #elif defined(__GNUC__) || defined(__clang__)
99
99
  // GCC/Clang: check for header availability and include them
100
100
  // We include headers but use target attributes to enable instructions per-function
101
- // Include cpuid.h for __get_cpuid fallback when __builtin_cpu_supports is unavailable
102
- #if __has_include(<cpuid.h>)
101
+ // Include cpuid.h for __get_cpuid fallback when __builtin_cpu_supports is unavailable.
102
+ // cpuid.h is available on all GCC since 4.3 and all Clang versions, so include
103
+ // it unconditionally rather than gating on __has_include (which GCC < 5 lacks).
103
104
  #include <cpuid.h>
104
- #endif
105
105
  #if defined(__SSE4_2__) || defined(__SSE2__)
106
106
  // If any SSE is enabled globally, x86intrin.h should be available
107
107
  #include <x86intrin.h>
@@ -109,6 +109,7 @@ static inline int oj_ctz64_fallback(uint64_t x) {
109
109
  #define HAVE_SIMD_SSE2 1
110
110
  #else
111
111
  // Try to include headers anyway for target attribute functions
112
+ #ifdef __has_include
112
113
  #if __has_include(<x86intrin.h>)
113
114
  #include <x86intrin.h>
114
115
  #define HAVE_SIMD_SSE4_2 1
@@ -123,6 +124,7 @@ static inline int oj_ctz64_fallback(uint64_t x) {
123
124
  #endif
124
125
  #endif
125
126
  #endif
127
+ #endif
126
128
 
127
129
  // Target attribute macros for function-level SIMD enabling
128
130
  #if defined(__clang__) || defined(__GNUC__)
data/ext/oj/sparse.c CHANGED
@@ -274,6 +274,10 @@ static void read_escaped_str(ParseInfo pi) {
274
274
  buf_cleanup(&buf);
275
275
  return;
276
276
  }
277
+ } else if ((unsigned char)c < 0x20 && (StrictMode == pi->options.mode || CompatMode == pi->options.mode)) {
278
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "invalid control character in string");
279
+ buf_cleanup(&buf);
280
+ return;
277
281
  } else {
278
282
  buf_append(&buf, c);
279
283
  }
@@ -290,13 +294,13 @@ static void read_escaped_str(ParseInfo pi) {
290
294
  case NEXT_HASH_NEW:
291
295
  case NEXT_HASH_KEY:
292
296
  if (Qundef == (parent->key_val = pi->hash_key(pi, buf.head, buf_len(&buf)))) {
293
- parent->klen = buf_len(&buf);
294
- parent->key = malloc(parent->klen + 1);
295
- memcpy((char *)parent->key, buf.head, parent->klen);
296
- *(char *)(parent->key + parent->klen) = '\0';
297
+ parent->klen = buf_len(&buf);
298
+ parent->key = oj_strndup(buf.head, parent->klen);
299
+ parent->kalloc = 1;
297
300
  } else {
298
- parent->key = "";
299
- parent->klen = 0;
301
+ parent->key = "";
302
+ parent->klen = 0;
303
+ parent->kalloc = 0;
300
304
  }
301
305
  parent->k1 = *pi->rd.str;
302
306
  parent->next = NEXT_HASH_COLON;
@@ -341,6 +345,9 @@ static void read_str(ParseInfo pi) {
341
345
  read_escaped_str(pi);
342
346
  reader_release(&pi->rd);
343
347
  return;
348
+ } else if ((unsigned char)c < 0x20 && (StrictMode == pi->options.mode || CompatMode == pi->options.mode)) {
349
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "invalid control character in string");
350
+ return;
344
351
  }
345
352
  }
346
353
  if (0 == parent) { // simple add
@@ -906,7 +913,17 @@ CLEANUP:
906
913
  if (0 != pi->circ_array) {
907
914
  oj_circ_array_free(pi->circ_array);
908
915
  }
916
+ reader_cleanup(&pi->rd);
909
917
  stack_cleanup(&pi->stack);
918
+ // A :match_string option builds a chain of regexps for this call that has to
919
+ // be freed. Without the option the member aliases the chain owned by the
920
+ // defaults and must be left alone.
921
+ if (pi->options.str_rx.head != oj_default_options.str_rx.head) {
922
+ oj_rxclass_cleanup(&pi->options.str_rx);
923
+ pi->options.str_rx.head = NULL;
924
+ pi->options.str_rx.tail = NULL;
925
+ }
926
+ oj_free_call_options(&pi->options);
910
927
  if (0 != fd) {
911
928
  #ifdef _WIN32
912
929
  rb_w32_close(fd);
@@ -16,15 +16,32 @@ static void stream_writer_free(void *ptr) {
16
16
  return;
17
17
  }
18
18
  sw = (StreamWriter)ptr;
19
+ if (NULL != sw->sw.out.circ_cache) {
20
+ oj_cache8_delete(sw->sw.out.circ_cache);
21
+ }
22
+ oj_options_release(&sw->sw.opts);
19
23
  OJ_R_FREE(sw->sw.out.buf);
20
24
  OJ_R_FREE(sw->sw.types);
21
25
  OJ_R_FREE(ptr);
22
26
  }
23
27
 
28
+ static void stream_writer_mark(void *ptr) {
29
+ if (NULL != ptr) {
30
+ StreamWriter sw = (StreamWriter)ptr;
31
+
32
+ oj_options_mark(&sw->sw.opts);
33
+ // The writer can be the only reference to the stream as in
34
+ // Oj::StreamWriter.new(StringIO.new) so it must be marked.
35
+ if (Qnil != sw->stream) {
36
+ rb_gc_mark(sw->stream);
37
+ }
38
+ }
39
+ }
40
+
24
41
  static const rb_data_type_t oj_stream_writer_type = {
25
42
  "Oj/stream_writer",
26
43
  {
27
- NULL,
44
+ stream_writer_mark,
28
45
  stream_writer_free,
29
46
  NULL,
30
47
  },
@@ -116,6 +133,7 @@ static VALUE stream_writer_new(int argc, VALUE *argv, VALUE self) {
116
133
  oj_str_writer_init(&sw->sw, 4096);
117
134
  sw->flush_limit = 0;
118
135
  }
136
+ oj_options_take_ownership(&sw->sw.opts);
119
137
  sw->sw.out.indent = sw->sw.opts.indent;
120
138
  sw->stream = stream;
121
139
  sw->type = type;