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/mimic_json.c CHANGED
@@ -241,12 +241,7 @@ static VALUE mimic_dump(int argc, VALUE *argv, VALUE self) {
241
241
  // default ::JSON::State argument is passed in. Basically a hack to get
242
242
  // around the active support hack so two wrongs make a right this time.
243
243
  active_hack[0] = rb_funcall(state_class, oj_new_id, 0);
244
- oj_dump_obj_to_json_using_params(*argv, &copts, &out, 1, active_hack);
245
-
246
- if (0 == out.buf) {
247
- rb_raise(rb_eNoMemError, "Not enough memory.");
248
- }
249
- rstr = rb_utf8_str_new_cstr(out.buf);
244
+ rstr = oj_dump_obj_to_str(*argv, &copts, &out, 1, active_hack);
250
245
  if (2 <= argc && Qnil != argv[1] && rb_respond_to(argv[1], oj_write_id)) {
251
246
  VALUE io = argv[1];
252
247
  VALUE args[1];
@@ -255,9 +250,6 @@ static VALUE mimic_dump(int argc, VALUE *argv, VALUE self) {
255
250
  rb_funcall2(io, oj_write_id, 1, args);
256
251
  rstr = io;
257
252
  }
258
-
259
- oj_out_free(&out);
260
-
261
253
  return rstr;
262
254
  }
263
255
 
@@ -380,7 +372,7 @@ static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
380
372
  }
381
373
  */
382
374
  if (1 < argc) {
383
- oj_dump_obj_to_json_using_params(*argv, copts, &out, argc - 1, argv + 1);
375
+ rstr = oj_dump_obj_to_str(*argv, copts, &out, argc - 1, argv + 1);
384
376
  } else {
385
377
  VALUE active_hack[1];
386
378
 
@@ -390,15 +382,8 @@ static VALUE mimic_generate_core(int argc, VALUE *argv, Options copts) {
390
382
  oj_define_mimic_json(0, NULL, Qnil);
391
383
  }
392
384
  active_hack[0] = rb_funcall(state_class, oj_new_id, 0);
393
- oj_dump_obj_to_json_using_params(*argv, copts, &out, 1, active_hack);
394
- }
395
- if (0 == out.buf) {
396
- rb_raise(rb_eNoMemError, "Not enough memory.");
385
+ rstr = oj_dump_obj_to_str(*argv, copts, &out, 1, active_hack);
397
386
  }
398
- rstr = rb_utf8_str_new_cstr(out.buf);
399
-
400
- oj_out_free(&out);
401
-
402
387
  return rstr;
403
388
  }
404
389
 
@@ -461,7 +446,7 @@ oj_mimic_pretty_generate(int argc, VALUE *argv, VALUE self) {
461
446
  if (1 == argc || Qnil == argv[1]) {
462
447
  h = rb_hash_new();
463
448
  } else {
464
- h = argv[1];
449
+ h = rb_hash_dup(argv[1]);
465
450
  }
466
451
  if (!oj_hash_has_key(h, oj_indent_sym)) {
467
452
  rb_hash_aset(h, oj_indent_sym, rb_str_new2(" "));
@@ -748,7 +733,6 @@ static struct _options mimic_object_to_json_options = {0, // indent
748
733
 
749
734
  static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
750
735
  struct _out out;
751
- VALUE rstr;
752
736
  struct _options copts = oj_default_options;
753
737
 
754
738
  copts.str_rx.head = NULL;
@@ -765,15 +749,7 @@ static VALUE mimic_object_to_json(int argc, VALUE *argv, VALUE self) {
765
749
  // To be strict the mimic_object_to_json_options should be used but people
766
750
  // seem to prefer the option of changing that.
767
751
  // oj_dump_obj_to_json(self, &mimic_object_to_json_options, &out);
768
- oj_dump_obj_to_json_using_params(self, &copts, &out, argc, argv);
769
- if (NULL == out.buf) {
770
- rb_raise(rb_eNoMemError, "Not enough memory.");
771
- }
772
- rstr = rb_utf8_str_new_cstr(out.buf);
773
-
774
- oj_out_free(&out);
775
-
776
- return rstr;
752
+ return oj_dump_obj_to_str(self, &copts, &out, argc, argv);
777
753
  }
778
754
 
779
755
  /* Document-method: state
data/ext/oj/object.c CHANGED
@@ -31,7 +31,7 @@ inline static long read_long(const char *str, size_t len) {
31
31
  static VALUE calc_hash_key(ParseInfo pi, Val kval, char k1) {
32
32
  volatile VALUE rkey;
33
33
 
34
- if (':' == k1) {
34
+ if (':' == k1 && 0 < kval->klen) {
35
35
  return ID2SYM(rb_intern3(kval->key + 1, kval->klen - 1, oj_utf8_encoding));
36
36
  }
37
37
  if (Yes == pi->options.sym_key) {
@@ -54,7 +54,7 @@ static VALUE str_to_value(ParseInfo pi, const char *str, size_t len, const char
54
54
  } else if (pi->circ_array && 3 <= len && '^' == *orig && 'r' == orig[1]) {
55
55
  long i = read_long(str + 2, len - 2);
56
56
 
57
- if (0 > i) {
57
+ if (0 >= i) {
58
58
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a valid ID number");
59
59
  return Qnil;
60
60
  }
@@ -207,14 +207,18 @@ oj_parse_xml_time(const char *str, int len) {
207
207
 
208
208
  static int hat_cstr(ParseInfo pi, Val parent, Val kval, const char *str, size_t len) {
209
209
  const char *key = kval->key;
210
- int klen = kval->klen;
210
+ size_t klen = kval->klen;
211
211
 
212
212
  if (2 == klen) {
213
213
  switch (key[1]) {
214
214
  case 'o': // object
215
215
  { // name2class sets an error if the class is not found or created
216
- VALUE clas = oj_name2class(pi, str, len, Yes == pi->options.auto_define, rb_eArgError);
216
+ VALUE clas;
217
217
 
218
+ if (oj_is_backtrace_name(str, len)) {
219
+ break;
220
+ }
221
+ clas = oj_name2class(pi, str, len, Yes == pi->options.auto_define, rb_eArgError);
218
222
  if (Qundef != clas) {
219
223
  parent->val = rb_obj_alloc(clas);
220
224
  }
@@ -230,7 +234,12 @@ static int hat_cstr(ParseInfo pi, Val parent, Val kval, const char *str, size_t
230
234
  parent->odd_args = oj_odd_alloc_args(odd);
231
235
  break;
232
236
  }
233
- case 'm': parent->val = ID2SYM(rb_intern3(str + 1, len - 1, oj_utf8_encoding)); break;
237
+ case 'm':
238
+ if (0 == len) {
239
+ return 0;
240
+ }
241
+ parent->val = ID2SYM(rb_intern3(str + 1, len - 1, oj_utf8_encoding));
242
+ break;
234
243
  case 's': parent->val = rb_utf8_str_new(str, len); break;
235
244
  case 'c': // class
236
245
  {
@@ -319,25 +328,45 @@ static int hat_value(ParseInfo pi, Val parent, const char *key, size_t klen, vol
319
328
  e1 = *RARRAY_CONST_PTR(value);
320
329
  // check for anonymous Struct
321
330
  if (T_ARRAY == rb_type(e1)) {
322
- VALUE args[1024];
323
- volatile VALUE rstr;
331
+ // Build the member-name list in a GC-managed Ruby array. Using a
332
+ // fixed C buffer (VALUE args[1024]) here overflowed the stack when
333
+ // the JSON supplied more than 1024 member names.
334
+ volatile VALUE names = rb_ary_new2(RARRAY_LEN(e1));
324
335
  size_t i;
325
336
  size_t cnt = RARRAY_LEN(e1);
326
337
 
327
338
  for (i = 0; i < cnt; i++) {
328
- rstr = RARRAY_AREF(e1, i);
329
- args[i] = rb_funcall(rstr, oj_to_sym_id, 0);
339
+ rb_ary_push(names, rb_funcall(RARRAY_AREF(e1, i), oj_to_sym_id, 0));
330
340
  }
331
- sc = rb_funcall2(rb_cStruct, oj_new_id, (int)cnt, args);
341
+ sc = rb_apply(rb_cStruct, oj_new_id, names);
332
342
  } else {
333
343
  // If struct is not defined then we let this fail and raise an exception.
334
344
  sc = oj_name2struct(pi, *RARRAY_CONST_PTR(value), rb_eArgError);
335
345
  }
346
+ if (Qundef == sc || Qnil == sc) {
347
+ // oj_name2struct already recorded the error; do not pass an
348
+ // unresolved (Qundef) class on to rb_obj_alloc/rb_class_new_instance.
349
+ return 1;
350
+ }
336
351
  if (sc == rb_cRange) {
337
352
  parent->val = rb_class_new_instance((int)(len - 1), RARRAY_CONST_PTR(value) + 1, rb_cRange);
353
+ } else if (Qtrue == rb_class_inherited_p(sc, rb_cRange)) {
354
+ volatile VALUE args = rb_ary_new_from_values((long)len, RARRAY_CONST_PTR(value));
355
+ volatile VALUE init = rb_funcall(rb_cRange,
356
+ rb_intern("instance_method"),
357
+ 1,
358
+ ID2SYM(rb_intern("initialize")));
359
+
360
+ parent->val = rb_obj_alloc(sc);
361
+ rb_ary_store(args, 0, parent->val);
362
+ rb_apply(init, rb_intern("bind_call"), args);
338
363
  } else {
339
364
  // Create a properly initialized struct instance without calling the initialize method.
340
365
  parent->val = rb_obj_alloc(sc);
366
+ if (T_STRUCT != rb_type(parent->val)) {
367
+ oj_set_error_at(pi, rb_eArgError, __FILE__, __LINE__, "%s is not a Struct", rb_class2name(sc));
368
+ return 1;
369
+ }
341
370
  // If the JSON array has more entries than the struct class allows, we record an error.
342
371
  #ifdef RSTRUCT_LEN
343
372
  #if RSTRUCT_LEN_RETURNS_INTEGER_OBJECT
@@ -382,14 +411,15 @@ void oj_set_obj_ivar(Val parent, Val kval, VALUE value) {
382
411
  parent->val = rb_funcall(parent->val, rb_intern("exception"), 1, value);
383
412
  } else if (kval->klen == 3 && strncmp("~bt", kval->key, 3) == 0 && rb_obj_is_kind_of(parent->val, rb_eException)) {
384
413
  rb_funcall(parent->val, rb_intern("set_backtrace"), 1, value);
385
- } else {
414
+ } else if (kval->klen != 13 || strncmp("~bt_locations", kval->key, 13) != 0 ||
415
+ !rb_obj_is_kind_of(parent->val, rb_eException)) {
386
416
  rb_ivar_set(parent->val, oj_attr_intern(kval->key, kval->klen), value);
387
417
  }
388
418
  }
389
419
 
390
420
  static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
391
421
  const char *key = kval->key;
392
- int klen = kval->klen;
422
+ size_t klen = kval->klen;
393
423
  Val parent = stack_peek(&pi->stack);
394
424
  volatile VALUE rval = Qnil;
395
425
 
@@ -431,7 +461,7 @@ WHICH_TYPE:
431
461
  if (0 != oj_odd_set_arg(parent->odd_args, kval->key, kval->klen, rval)) {
432
462
  char buf[256];
433
463
 
434
- if ((int)sizeof(buf) - 1 <= klen) {
464
+ if (sizeof(buf) - 1 <= klen) {
435
465
  klen = sizeof(buf) - 2;
436
466
  }
437
467
  memcpy(buf, key, klen);
@@ -460,7 +490,7 @@ WHICH_TYPE:
460
490
 
461
491
  static void hash_set_num(ParseInfo pi, Val kval, NumInfo ni) {
462
492
  const char *key = kval->key;
463
- int klen = kval->klen;
493
+ size_t klen = kval->klen;
464
494
  Val parent = stack_peek(&pi->stack);
465
495
  volatile VALUE rval = Qnil;
466
496
 
@@ -500,7 +530,7 @@ WHICH_TYPE:
500
530
  if (0 != oj_odd_set_arg(parent->odd_args, key, klen, rval)) {
501
531
  char buf[256];
502
532
 
503
- if ((int)sizeof(buf) - 1 <= klen) {
533
+ if (sizeof(buf) - 1 <= klen) {
504
534
  klen = sizeof(buf) - 2;
505
535
  }
506
536
  memcpy(buf, key, klen);
@@ -529,7 +559,7 @@ WHICH_TYPE:
529
559
 
530
560
  static void hash_set_value(ParseInfo pi, Val kval, VALUE value) {
531
561
  const char *key = kval->key;
532
- int klen = kval->klen;
562
+ size_t klen = kval->klen;
533
563
  Val parent = stack_peek(&pi->stack);
534
564
 
535
565
  WHICH_TYPE:
@@ -585,7 +615,7 @@ WHICH_TYPE:
585
615
  } else if (0 != oj_odd_set_arg(parent->odd_args, key, klen, value)) {
586
616
  char buf[256];
587
617
 
588
- if ((int)sizeof(buf) - 1 <= klen) {
618
+ if (sizeof(buf) - 1 <= klen) {
589
619
  klen = sizeof(buf) - 2;
590
620
  }
591
621
  memcpy(buf, key, klen);
data/ext/oj/odd.c CHANGED
@@ -166,7 +166,11 @@ Odd oj_get_oddc(const char *classname, size_t len) {
166
166
  if (len == odd->clen && 0 == strncmp(classname, odd->classname, len)) {
167
167
  return odd;
168
168
  }
169
- if (odd->is_module && 0 == strncmp(odd->classname, classname, odd->clen) && ':' == classname[odd->clen]) {
169
+ // The module test needs a "::" after the module name, so the name has
170
+ // to be shorter than what it is compared against. Without that the
171
+ // comparison and the character after it both run past len.
172
+ if (odd->is_module && odd->clen < len && 0 == strncmp(odd->classname, classname, odd->clen) &&
173
+ ':' == classname[odd->clen]) {
170
174
  return odd;
171
175
  }
172
176
  }
@@ -195,7 +199,7 @@ int oj_odd_set_arg(OddArgs args, const char *key, size_t klen, VALUE value) {
195
199
  int i;
196
200
 
197
201
  for (i = args->odd->attr_cnt, np = args->odd->attr_names, vp = args->args; 0 < i; i--, np++, vp++) {
198
- if (0 == strncmp(key, *np, klen) && '\0' == *((*np) + klen)) {
202
+ if (klen == strlen(*np) && 0 == memcmp(key, *np, klen)) {
199
203
  *vp = value;
200
204
  return 0;
201
205
  }