ox 2.14.29 → 2.14.30
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +36 -0
- data/ext/ox/builder.c +6 -4
- data/ext/ox/cache.c +54 -45
- data/ext/ox/dump.c +39 -23
- data/ext/ox/extconf.rb +0 -3
- data/ext/ox/hash_load.c +42 -59
- data/ext/ox/intern.c +1 -1
- data/ext/ox/obj_load.c +78 -14
- data/ext/ox/ox.c +24 -17
- data/ext/ox/ox.h +2 -3
- data/ext/ox/parse.c +41 -9
- data/ext/ox/sax.c +47 -3
- data/ext/ox/sax_as.c +55 -21
- data/ext/ox/sax_buf.c +53 -22
- data/ext/ox/sax_buf.h +1 -0
- data/ext/ox/special.c +24 -0
- data/ext/ox/special.h +2 -0
- data/ext/ox/time_conv.h +57 -0
- data/lib/ox/version.rb +1 -1
- metadata +1 -1
data/ext/ox/obj_load.c
CHANGED
|
@@ -105,7 +105,15 @@ inline static VALUE structname2obj(const char *name) {
|
|
|
105
105
|
s = name;
|
|
106
106
|
break;
|
|
107
107
|
} else if (':' == *s) {
|
|
108
|
-
|
|
108
|
+
// Step over the "::" separator, but only over a second ':' that is
|
|
109
|
+
// actually there. A name ending in a single ':' (the attacker
|
|
110
|
+
// controlled `c` attribute in object mode) otherwise skipped past
|
|
111
|
+
// the terminating '\0' and rb_intern() read on into the rest of the
|
|
112
|
+
// document. classname2class() already guards this the same way.
|
|
113
|
+
s++;
|
|
114
|
+
if (':' == *s) {
|
|
115
|
+
s++;
|
|
116
|
+
}
|
|
109
117
|
break;
|
|
110
118
|
}
|
|
111
119
|
}
|
|
@@ -430,9 +438,11 @@ static void add_text(PInfo pi, char *text, size_t len, int closed) {
|
|
|
430
438
|
}
|
|
431
439
|
break;
|
|
432
440
|
case FixnumCode: {
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
441
|
+
const char *num = text;
|
|
442
|
+
long n = 0;
|
|
443
|
+
char c;
|
|
444
|
+
int neg = 0;
|
|
445
|
+
bool big = false;
|
|
436
446
|
|
|
437
447
|
if ('-' == *text) {
|
|
438
448
|
neg = 1;
|
|
@@ -441,12 +451,23 @@ static void add_text(PInfo pi, char *text, size_t len, int closed) {
|
|
|
441
451
|
for (; '\0' != *text; text++) {
|
|
442
452
|
c = *text;
|
|
443
453
|
if ('0' <= c && c <= '9') {
|
|
444
|
-
|
|
454
|
+
// Past LONG_MAX n would wrap to whatever value the document picked,
|
|
455
|
+
// so stop there and let Ruby build the Integer once the rest is
|
|
456
|
+
// checked.
|
|
457
|
+
if (big || (LONG_MAX - (c - '0')) / 10 < n) {
|
|
458
|
+
big = true;
|
|
459
|
+
} else {
|
|
460
|
+
n = n * 10 + (c - '0');
|
|
461
|
+
}
|
|
445
462
|
} else {
|
|
446
463
|
set_error(&pi->err, "bad number format", pi->str, pi->s);
|
|
447
464
|
return;
|
|
448
465
|
}
|
|
449
466
|
}
|
|
467
|
+
if (big) {
|
|
468
|
+
h->obj = rb_cstr_to_inum(num, 10, 0);
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
450
471
|
if (neg) {
|
|
451
472
|
n = -n;
|
|
452
473
|
}
|
|
@@ -532,6 +553,19 @@ static void add_text(PInfo pi, char *text, size_t len, int closed) {
|
|
|
532
553
|
}
|
|
533
554
|
}
|
|
534
555
|
|
|
556
|
+
// snprintf() returns the length it wanted to write, not what fit, so adding it
|
|
557
|
+
// to s unchecked moves s past end once the element name and attributes are
|
|
558
|
+
// longer than the buffer.
|
|
559
|
+
static char *trace_append(char *s, const char *end, int cnt) {
|
|
560
|
+
if (cnt < 0) {
|
|
561
|
+
return s;
|
|
562
|
+
}
|
|
563
|
+
if (end - s <= cnt) {
|
|
564
|
+
return (char *)end - 1;
|
|
565
|
+
}
|
|
566
|
+
return s + cnt;
|
|
567
|
+
}
|
|
568
|
+
|
|
535
569
|
static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
|
|
536
570
|
Attr a;
|
|
537
571
|
Helper h;
|
|
@@ -545,9 +579,9 @@ static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren
|
|
|
545
579
|
char *s = buf;
|
|
546
580
|
char *end = buf + sizeof(buf) - 2;
|
|
547
581
|
|
|
548
|
-
s
|
|
582
|
+
s = trace_append(s, end, snprintf(s, end - s, " <%s%s", (hasChildren) ? "" : "/", ename));
|
|
549
583
|
for (a = attrs; 0 != a->name; a++) {
|
|
550
|
-
s
|
|
584
|
+
s = trace_append(s, end, snprintf(s, end - s, " %s=%s", a->name, a->value));
|
|
551
585
|
}
|
|
552
586
|
*s++ = '>';
|
|
553
587
|
*s++ = '\0';
|
|
@@ -652,6 +686,9 @@ static void add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren
|
|
|
652
686
|
if (0 != pi->circ_array) {
|
|
653
687
|
circ_array_set(pi->circ_array, h->obj, get_id_from_attrs(pi, attrs));
|
|
654
688
|
}
|
|
689
|
+
if (Qtrue == rb_obj_is_kind_of(h->obj, rb_cRange)) {
|
|
690
|
+
h->obj = rb_ary_new_from_args(4, Qnil, Qnil, Qfalse, h->obj);
|
|
691
|
+
}
|
|
655
692
|
break;
|
|
656
693
|
case ClassCode:
|
|
657
694
|
if (Qundef == (h->obj = get_class_from_attrs(attrs, pi, ox_bag_clas))) {
|
|
@@ -707,6 +744,18 @@ static void end_element(PInfo pi, const char *ename) {
|
|
|
707
744
|
const VALUE *ap = RARRAY_PTR(h->obj);
|
|
708
745
|
|
|
709
746
|
h->obj = rb_range_new(*ap, *(ap + 1), Qtrue == *(ap + 2));
|
|
747
|
+
} else if (StructCode == h->type && T_ARRAY == rb_type(h->obj)) {
|
|
748
|
+
VALUE args = h->obj;
|
|
749
|
+
VALUE init = rb_funcall(rb_cRange, rb_intern("instance_method"), 1, ID2SYM(rb_intern("initialize")));
|
|
750
|
+
|
|
751
|
+
h->obj = rb_ary_entry(args, 3);
|
|
752
|
+
rb_funcall(init,
|
|
753
|
+
rb_intern("bind_call"),
|
|
754
|
+
4,
|
|
755
|
+
h->obj,
|
|
756
|
+
rb_ary_entry(args, 0),
|
|
757
|
+
rb_ary_entry(args, 1),
|
|
758
|
+
rb_ary_entry(args, 2));
|
|
710
759
|
}
|
|
711
760
|
pi->obj = h->obj;
|
|
712
761
|
if (0 != ph) {
|
|
@@ -735,7 +784,17 @@ static void end_element(PInfo pi, const char *ename) {
|
|
|
735
784
|
set_error(&pi->err, "Invalid element for object mode", pi->str, pi->s);
|
|
736
785
|
return;
|
|
737
786
|
}
|
|
738
|
-
|
|
787
|
+
if (T_ARRAY == rb_type(ph->obj)) {
|
|
788
|
+
long i = NUM2LONG((VALUE)h->var);
|
|
789
|
+
|
|
790
|
+
if (2 < i) {
|
|
791
|
+
set_error(&pi->err, "Invalid range attribute", pi->str, pi->s);
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
rb_ary_store(ph->obj, i, h->obj);
|
|
795
|
+
} else {
|
|
796
|
+
rb_struct_aset(ph->obj, h->var, h->obj);
|
|
797
|
+
}
|
|
739
798
|
break;
|
|
740
799
|
case HashCode:
|
|
741
800
|
// put back h
|
|
@@ -812,7 +871,7 @@ static void end_element(PInfo pi, const char *ename) {
|
|
|
812
871
|
}
|
|
813
872
|
|
|
814
873
|
static VALUE parse_double_time(const char *text, VALUE clas) {
|
|
815
|
-
|
|
874
|
+
ox_time_t v = 0;
|
|
816
875
|
long v2 = 0;
|
|
817
876
|
const char *dot = 0;
|
|
818
877
|
char c;
|
|
@@ -828,7 +887,12 @@ static VALUE parse_double_time(const char *text, VALUE clas) {
|
|
|
828
887
|
if (c < '0' || '9' < c) {
|
|
829
888
|
return Qnil;
|
|
830
889
|
}
|
|
831
|
-
|
|
890
|
+
// Past the largest ox_time_t the seconds would wrap to whatever the document
|
|
891
|
+
// picked, so leave a count that long to the parsers below.
|
|
892
|
+
if ((OX_TIME_MAX - (c - '0')) / 10 < v) {
|
|
893
|
+
return Qnil;
|
|
894
|
+
}
|
|
895
|
+
v = 10 * v + (ox_time_t)(c - '0');
|
|
832
896
|
}
|
|
833
897
|
dot = text++;
|
|
834
898
|
for (; '\0' != *text && text - dot <= 6; text++) {
|
|
@@ -842,7 +906,7 @@ static VALUE parse_double_time(const char *text, VALUE clas) {
|
|
|
842
906
|
v2 *= 10;
|
|
843
907
|
}
|
|
844
908
|
if (neg) {
|
|
845
|
-
//
|
|
909
|
+
// ox_time_nano_new() wants a non negative nanosecond count, so the
|
|
846
910
|
// fraction goes back into the second count the way dump_time_thin()
|
|
847
911
|
// took it out.
|
|
848
912
|
if (0 == v2) {
|
|
@@ -852,7 +916,7 @@ static VALUE parse_double_time(const char *text, VALUE clas) {
|
|
|
852
916
|
v2 = 1000000000L - v2;
|
|
853
917
|
}
|
|
854
918
|
}
|
|
855
|
-
return
|
|
919
|
+
return ox_time_nano_new(v, v2);
|
|
856
920
|
}
|
|
857
921
|
|
|
858
922
|
typedef struct _tp {
|
|
@@ -914,8 +978,8 @@ static VALUE parse_xsd_time(const char *text, VALUE clas) {
|
|
|
914
978
|
}
|
|
915
979
|
// mktime() would read the wall clock as local time and throw the offset
|
|
916
980
|
// away, and returns -1 for everything before the epoch on Windows.
|
|
917
|
-
return
|
|
918
|
-
(
|
|
981
|
+
return ox_time_nano_new(
|
|
982
|
+
(ox_time_t)(ox_epoch_from_civil(cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5]) - offset),
|
|
919
983
|
nsec);
|
|
920
984
|
}
|
|
921
985
|
|
data/ext/ox/ox.c
CHANGED
|
@@ -58,6 +58,7 @@ ID ox_end_element_id;
|
|
|
58
58
|
ID ox_end_id;
|
|
59
59
|
ID ox_end_instruct_id;
|
|
60
60
|
ID ox_error_id;
|
|
61
|
+
ID ox_exception_id;
|
|
61
62
|
ID ox_excl_id;
|
|
62
63
|
ID ox_external_encoding_id;
|
|
63
64
|
ID ox_fileno_id;
|
|
@@ -732,6 +733,7 @@ static VALUE to_obj(VALUE self, VALUE ruby_xml) {
|
|
|
732
733
|
char *xml, *x, *str;
|
|
733
734
|
size_t len;
|
|
734
735
|
VALUE obj;
|
|
736
|
+
VALUE xml_buf = 0;
|
|
735
737
|
VALUE was_disabled;
|
|
736
738
|
struct _options options = ox_default_options;
|
|
737
739
|
struct _err err;
|
|
@@ -746,8 +748,10 @@ static VALUE to_obj(VALUE self, VALUE ruby_xml) {
|
|
|
746
748
|
// Drop the BOM from len as well, otherwise the copy below reads past the
|
|
747
749
|
// end of the Ruby string and xml is left without a terminating '\0'.
|
|
748
750
|
len -= (size_t)(x - str);
|
|
751
|
+
// The parse can raise, which skips ALLOCV_END(). The GC then frees the
|
|
752
|
+
// copy, where an ALLOC_N() copy was leaked.
|
|
749
753
|
if (SMALL_XML < len) {
|
|
750
|
-
xml =
|
|
754
|
+
xml = ALLOCV_N(char, xml_buf, len);
|
|
751
755
|
} else {
|
|
752
756
|
xml = ALLOCA_N(char, len);
|
|
753
757
|
}
|
|
@@ -758,9 +762,7 @@ static VALUE to_obj(VALUE self, VALUE ruby_xml) {
|
|
|
758
762
|
args.err = &err;
|
|
759
763
|
was_disabled = rb_gc_disable();
|
|
760
764
|
obj = rb_ensure(obj_parse_body, (VALUE)&args, obj_parse_reenable, was_disabled);
|
|
761
|
-
|
|
762
|
-
xfree(xml);
|
|
763
|
-
}
|
|
765
|
+
ALLOCV_END(xml_buf);
|
|
764
766
|
RB_GC_GUARD(obj);
|
|
765
767
|
if (err_has(&err)) {
|
|
766
768
|
ox_err_raise(&err);
|
|
@@ -780,6 +782,7 @@ static VALUE to_gen(VALUE self, VALUE ruby_xml) {
|
|
|
780
782
|
char *xml, *x, *str;
|
|
781
783
|
size_t len;
|
|
782
784
|
VALUE obj;
|
|
785
|
+
VALUE xml_buf = 0;
|
|
783
786
|
struct _options options = ox_default_options;
|
|
784
787
|
struct _err err;
|
|
785
788
|
|
|
@@ -793,15 +796,13 @@ static VALUE to_gen(VALUE self, VALUE ruby_xml) {
|
|
|
793
796
|
// end of the Ruby string and xml is left without a terminating '\0'.
|
|
794
797
|
len -= (size_t)(x - str);
|
|
795
798
|
if (SMALL_XML < len) {
|
|
796
|
-
xml =
|
|
799
|
+
xml = ALLOCV_N(char, xml_buf, len);
|
|
797
800
|
} else {
|
|
798
801
|
xml = ALLOCA_N(char, len);
|
|
799
802
|
}
|
|
800
803
|
memcpy(xml, x, len);
|
|
801
804
|
obj = ox_parse(xml, len - 1, ox_gen_callbacks, 0, &options, &err);
|
|
802
|
-
|
|
803
|
-
xfree(xml);
|
|
804
|
-
}
|
|
805
|
+
ALLOCV_END(xml_buf);
|
|
805
806
|
if (err_has(&err)) {
|
|
806
807
|
ox_err_raise(&err);
|
|
807
808
|
}
|
|
@@ -1018,6 +1019,7 @@ static VALUE load_str(int argc, VALUE *argv, VALUE self) {
|
|
|
1018
1019
|
char *xml;
|
|
1019
1020
|
size_t len;
|
|
1020
1021
|
VALUE obj;
|
|
1022
|
+
VALUE xml_buf = 0;
|
|
1021
1023
|
VALUE encoding;
|
|
1022
1024
|
struct _err err;
|
|
1023
1025
|
|
|
@@ -1029,7 +1031,7 @@ static VALUE load_str(int argc, VALUE *argv, VALUE self) {
|
|
|
1029
1031
|
/* the xml string gets modified so make a copy of it */
|
|
1030
1032
|
len = RSTRING_LEN(*argv) + 1;
|
|
1031
1033
|
if (SMALL_XML < len) {
|
|
1032
|
-
xml =
|
|
1034
|
+
xml = ALLOCV_N(char, xml_buf, len);
|
|
1033
1035
|
} else {
|
|
1034
1036
|
xml = ALLOCA_N(char, len);
|
|
1035
1037
|
}
|
|
@@ -1037,9 +1039,7 @@ static VALUE load_str(int argc, VALUE *argv, VALUE self) {
|
|
|
1037
1039
|
memcpy(xml, StringValuePtr(*argv), len);
|
|
1038
1040
|
xml[len - 1] = '\0';
|
|
1039
1041
|
obj = load(xml, len - 1, argc - 1, argv + 1, self, encoding, &err);
|
|
1040
|
-
|
|
1041
|
-
xfree(xml);
|
|
1042
|
-
}
|
|
1042
|
+
ALLOCV_END(xml_buf);
|
|
1043
1043
|
if (err_has(&err)) {
|
|
1044
1044
|
ox_err_raise(&err);
|
|
1045
1045
|
}
|
|
@@ -1087,6 +1087,7 @@ static VALUE load_file(int argc, VALUE *argv, VALUE self) {
|
|
|
1087
1087
|
off_t len;
|
|
1088
1088
|
struct stat st;
|
|
1089
1089
|
VALUE obj;
|
|
1090
|
+
VALUE xml_buf = 0;
|
|
1090
1091
|
struct _err err;
|
|
1091
1092
|
|
|
1092
1093
|
if (1 > argc) {
|
|
@@ -1108,22 +1109,27 @@ static VALUE load_file(int argc, VALUE *argv, VALUE self) {
|
|
|
1108
1109
|
rb_raise(rb_eIOError, "%s is not a regular file.\n", path);
|
|
1109
1110
|
}
|
|
1110
1111
|
len = st.st_size;
|
|
1112
|
+
// ALLOCV_N() counts in a long, which is 32 bits on Windows.
|
|
1113
|
+
if (LONG_MAX <= len) {
|
|
1114
|
+
fclose(f);
|
|
1115
|
+
rb_raise(rb_eIOError, "%s is too large.\n", path);
|
|
1116
|
+
}
|
|
1111
1117
|
if (SMALL_XML < len) {
|
|
1112
|
-
xml =
|
|
1118
|
+
xml = ALLOCV_N(char, xml_buf, (long)len + 1);
|
|
1113
1119
|
} else {
|
|
1114
1120
|
xml = ALLOCA_N(char, (size_t)len + 1);
|
|
1115
1121
|
}
|
|
1122
|
+
// The file is closed before the parse since load() can raise.
|
|
1116
1123
|
if ((size_t)len != fread(xml, 1, len, f)) {
|
|
1124
|
+
fclose(f);
|
|
1117
1125
|
ox_err_set(&err, rb_eLoadError, "Failed to read %ld bytes from %s.\n", (long)len, path);
|
|
1118
1126
|
obj = Qnil;
|
|
1119
1127
|
} else {
|
|
1128
|
+
fclose(f);
|
|
1120
1129
|
xml[len] = '\0';
|
|
1121
1130
|
obj = load(xml, len, argc - 1, argv + 1, self, Qnil, &err);
|
|
1122
1131
|
}
|
|
1123
|
-
|
|
1124
|
-
if (SMALL_XML < len) {
|
|
1125
|
-
xfree(xml);
|
|
1126
|
-
}
|
|
1132
|
+
ALLOCV_END(xml_buf);
|
|
1127
1133
|
if (err_has(&err)) {
|
|
1128
1134
|
ox_err_raise(&err);
|
|
1129
1135
|
}
|
|
@@ -1565,6 +1571,7 @@ void Init_ox(void) {
|
|
|
1565
1571
|
ox_end_id = rb_intern("@end");
|
|
1566
1572
|
ox_end_instruct_id = rb_intern("end_instruct");
|
|
1567
1573
|
ox_error_id = rb_intern("error");
|
|
1574
|
+
ox_exception_id = rb_intern("exception");
|
|
1568
1575
|
ox_excl_id = rb_intern("@excl");
|
|
1569
1576
|
ox_external_encoding_id = rb_intern("external_encoding");
|
|
1570
1577
|
ox_fileno_id = rb_intern("fileno");
|
data/ext/ox/ox.h
CHANGED
|
@@ -151,9 +151,7 @@ struct _pInfo {
|
|
|
151
151
|
CircArray circ_array;
|
|
152
152
|
unsigned long id; // set for text types when cirs_array is set
|
|
153
153
|
Options options;
|
|
154
|
-
|
|
155
|
-
int mark_size; // allocated size
|
|
156
|
-
int mark_cnt;
|
|
154
|
+
st_table *marked;
|
|
157
155
|
char last; // last character read, rarely set
|
|
158
156
|
};
|
|
159
157
|
|
|
@@ -192,6 +190,7 @@ extern ID ox_end_element_id;
|
|
|
192
190
|
extern ID ox_end_id;
|
|
193
191
|
extern ID ox_end_instruct_id;
|
|
194
192
|
extern ID ox_error_id;
|
|
193
|
+
extern ID ox_exception_id;
|
|
195
194
|
extern ID ox_excl_id;
|
|
196
195
|
extern ID ox_external_encoding_id;
|
|
197
196
|
extern ID ox_fileno_id;
|
data/ext/ox/parse.c
CHANGED
|
@@ -128,6 +128,7 @@ static void fix_newlines(char *buf) {
|
|
|
128
128
|
static void mark_pi_cb(void *ptr) {
|
|
129
129
|
if (NULL != ptr) {
|
|
130
130
|
HelperStack stack = &((PInfo)ptr)->helpers;
|
|
131
|
+
CircArray ca = ((PInfo)ptr)->circ_array;
|
|
131
132
|
Helper h;
|
|
132
133
|
|
|
133
134
|
for (h = stack->head; h < stack->tail; h++) {
|
|
@@ -135,6 +136,17 @@ static void mark_pi_cb(void *ptr) {
|
|
|
135
136
|
rb_gc_mark(h->obj);
|
|
136
137
|
}
|
|
137
138
|
}
|
|
139
|
+
// An object registered for a later <p i="..."/> can drop out of the
|
|
140
|
+
// tree before that reference is read, for example when the document
|
|
141
|
+
// sets the same instance variable twice. The table is on the heap, so
|
|
142
|
+
// nothing else keeps it alive.
|
|
143
|
+
if (NULL != ca) {
|
|
144
|
+
unsigned long i;
|
|
145
|
+
|
|
146
|
+
for (i = 0; i < ca->cnt; i++) {
|
|
147
|
+
rb_gc_mark(ca->objs[i]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
138
150
|
}
|
|
139
151
|
}
|
|
140
152
|
|
|
@@ -238,11 +250,11 @@ static VALUE ox_parse_ensure(VALUE ctxv) {
|
|
|
238
250
|
ox_circ_array_free(ctx->pi->circ_array);
|
|
239
251
|
ctx->pi->circ_array = 0;
|
|
240
252
|
}
|
|
241
|
-
// Same for hash mode's mark
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
253
|
+
// Same for hash mode's mark set, freed by finish() only if the loop ends.
|
|
254
|
+
if (NULL != ctx->pi->marked) {
|
|
255
|
+
st_free_table(ctx->pi->marked);
|
|
256
|
+
ctx->pi->marked = NULL;
|
|
257
|
+
}
|
|
246
258
|
return Qnil;
|
|
247
259
|
}
|
|
248
260
|
|
|
@@ -269,8 +281,6 @@ ox_parse(char *xml, size_t len, ParseCallbacks pcb, char **endp, Options options
|
|
|
269
281
|
pi.circ_array = 0;
|
|
270
282
|
pi.options = options;
|
|
271
283
|
pi.marked = NULL;
|
|
272
|
-
pi.mark_size = 0;
|
|
273
|
-
pi.mark_cnt = 0;
|
|
274
284
|
|
|
275
285
|
ctx.pi = π
|
|
276
286
|
ctx.endp = endp;
|
|
@@ -373,6 +383,9 @@ DONE:
|
|
|
373
383
|
}
|
|
374
384
|
if (attrs_ok) {
|
|
375
385
|
if ('>' != *pi->s++) {
|
|
386
|
+
// Step back first: the '>' check stepped over whatever was there,
|
|
387
|
+
// and when that was the terminating NUL set_error would read past it.
|
|
388
|
+
pi->s--;
|
|
376
389
|
set_error(&pi->err, "invalid format, processing instruction not terminated", pi->str, pi->s);
|
|
377
390
|
goto CLEANUP;
|
|
378
391
|
}
|
|
@@ -606,6 +619,14 @@ static char *read_element(PInfo pi, int depth) {
|
|
|
606
619
|
attr_stack_cleanup(&attrs);
|
|
607
620
|
return 0;
|
|
608
621
|
}
|
|
622
|
+
// An unquoted tolerant value stepped pi->s past the '/' or '>' that
|
|
623
|
+
// ended it, while the close handling below (like the no-value branch
|
|
624
|
+
// above) expects pi->s left on it. Without this the close is stepped
|
|
625
|
+
// over twice, walking pi->s past the terminating NUL into an
|
|
626
|
+
// out-of-bounds read in the next next_non_white.
|
|
627
|
+
if ('/' == pi->last || '>' == pi->last) {
|
|
628
|
+
pi->s--;
|
|
629
|
+
}
|
|
609
630
|
if (pi->options->convert_special && 0 != strchr(attr_value, '&')) {
|
|
610
631
|
if (0 != collapse_special(pi, (char *)attr_value) || err_has(&pi->err)) {
|
|
611
632
|
attr_stack_cleanup(&attrs);
|
|
@@ -681,6 +702,14 @@ static char *read_element(PInfo pi, int depth) {
|
|
|
681
702
|
((TolerantEffort == pi->options->effort) ? strcasecmp(name, ename) : strcmp(name, ename))) {
|
|
682
703
|
attr_stack_cleanup(&attrs);
|
|
683
704
|
if (TolerantEffort == pi->options->effort) {
|
|
705
|
+
// The element that matches name steps pi->s over
|
|
706
|
+
// the '>', so it has to be there. Without it
|
|
707
|
+
// (</a at the end of the input) pi->s is on the
|
|
708
|
+
// terminating NUL and that step walks past it.
|
|
709
|
+
if ('>' != c) {
|
|
710
|
+
set_error(&pi->err, "invalid format, element not closed", pi->str, pi->s);
|
|
711
|
+
return 0;
|
|
712
|
+
}
|
|
684
713
|
pi->pcb->end_element(pi, ename);
|
|
685
714
|
return name;
|
|
686
715
|
} else {
|
|
@@ -1225,7 +1254,7 @@ static char *read_coded_chars(PInfo pi, char *text) {
|
|
|
1225
1254
|
} else {
|
|
1226
1255
|
b = read_10_uint64(b, &u);
|
|
1227
1256
|
}
|
|
1228
|
-
if (0 == b) {
|
|
1257
|
+
if (0 == b || ox_ucs_has_nul(u)) {
|
|
1229
1258
|
*text++ = '&';
|
|
1230
1259
|
} else {
|
|
1231
1260
|
if (u <= 0x000000000000007FULL) {
|
|
@@ -1288,7 +1317,7 @@ static int collapse_special(PInfo pi, char *str) {
|
|
|
1288
1317
|
x = '\0';
|
|
1289
1318
|
end = read_10_uint64(s, &u);
|
|
1290
1319
|
}
|
|
1291
|
-
if (0 == end) {
|
|
1320
|
+
if (0 == end || ox_ucs_has_nul(u)) {
|
|
1292
1321
|
if (TolerantEffort == pi->options->effort) {
|
|
1293
1322
|
*b++ = '&';
|
|
1294
1323
|
*b++ = '#';
|
|
@@ -1297,6 +1326,9 @@ static int collapse_special(PInfo pi, char *str) {
|
|
|
1297
1326
|
}
|
|
1298
1327
|
continue;
|
|
1299
1328
|
}
|
|
1329
|
+
if (0 != end) {
|
|
1330
|
+
set_error(&pi->err, "invalid format, character reference resolves to a NUL", pi->str, pi->s);
|
|
1331
|
+
}
|
|
1300
1332
|
return EDOM;
|
|
1301
1333
|
}
|
|
1302
1334
|
if (u <= 0x000000000000007FULL) {
|
data/ext/ox/sax.c
CHANGED
|
@@ -73,6 +73,31 @@ const rb_data_type_t ox_sax_value_type = {
|
|
|
73
73
|
0,
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
+
// Past STACK_INC elements the name stack moves to the heap, where the
|
|
77
|
+
// conservative stack scan does not see the element names on it. Mark them
|
|
78
|
+
// while parsing or a GC can free a name before end_element() is called with it.
|
|
79
|
+
static void mark_stack_cb(void *ptr) {
|
|
80
|
+
if (NULL != ptr) {
|
|
81
|
+
NStack stack = &((SaxDrive)ptr)->stack;
|
|
82
|
+
Nv nv;
|
|
83
|
+
|
|
84
|
+
for (nv = stack->head; nv < stack->tail; nv++) {
|
|
85
|
+
rb_gc_mark(nv->val);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
static const rb_data_type_t sax_wrap_type = {
|
|
91
|
+
"Object",
|
|
92
|
+
{
|
|
93
|
+
mark_stack_cb,
|
|
94
|
+
NULL,
|
|
95
|
+
NULL,
|
|
96
|
+
},
|
|
97
|
+
0,
|
|
98
|
+
0,
|
|
99
|
+
};
|
|
100
|
+
|
|
76
101
|
static VALUE protect_parse(VALUE drp) {
|
|
77
102
|
parse((SaxDrive)drp);
|
|
78
103
|
|
|
@@ -96,10 +121,14 @@ void ox_sax_parse(VALUE handler, VALUE io, SaxOptions options) {
|
|
|
96
121
|
rb_ext_ractor_safe(true);
|
|
97
122
|
#endif
|
|
98
123
|
struct _saxDrive dr;
|
|
124
|
+
volatile VALUE wrap;
|
|
99
125
|
int line = 0;
|
|
100
126
|
|
|
101
127
|
sax_drive_init(&dr, handler, io, options);
|
|
128
|
+
wrap = TypedData_Wrap_Struct(rb_cObject, &sax_wrap_type, &dr);
|
|
102
129
|
rb_protect(protect_parse, (VALUE)&dr, &line);
|
|
130
|
+
// dr is about to go away with this frame, detach it from the wrapper.
|
|
131
|
+
DATA_PTR(wrap) = NULL;
|
|
103
132
|
ox_sax_drive_cleanup(&dr);
|
|
104
133
|
if (0 != line) {
|
|
105
134
|
rb_jump_tag(line);
|
|
@@ -149,7 +178,12 @@ static void attr_value(SaxDrive dr, VALUE name, char *value, long pos, long line
|
|
|
149
178
|
dr->set_col(dr->handler, col);
|
|
150
179
|
args[0] = name;
|
|
151
180
|
args[1] = dr->value_obj;
|
|
181
|
+
// The Value reads dr, which lives in ox_sax_parse()'s frame and whose
|
|
182
|
+
// buf.str is only this value while the callback runs, so it is attached
|
|
183
|
+
// for the callback alone.
|
|
184
|
+
DATA_PTR(dr->value_obj) = dr;
|
|
152
185
|
rb_funcall2(dr->handler, ox_attr_value_id, 2, args);
|
|
186
|
+
DATA_PTR(dr->value_obj) = NULL;
|
|
153
187
|
}
|
|
154
188
|
|
|
155
189
|
static void attrs_done_noop(VALUE handler) {
|
|
@@ -267,7 +301,7 @@ static void sax_drive_init(SaxDrive dr, VALUE handler, VALUE io, SaxOptions opti
|
|
|
267
301
|
dr->buf.dr = dr;
|
|
268
302
|
stack_init(&dr->stack);
|
|
269
303
|
dr->handler = handler;
|
|
270
|
-
dr->value_obj = TypedData_Wrap_Struct(ox_sax_value_class, &ox_sax_value_type,
|
|
304
|
+
dr->value_obj = TypedData_Wrap_Struct(ox_sax_value_class, &ox_sax_value_type, NULL);
|
|
271
305
|
rb_gc_register_address(&dr->value_obj);
|
|
272
306
|
dr->options = *options;
|
|
273
307
|
dr->err = 0;
|
|
@@ -326,6 +360,9 @@ static void sax_drive_init(SaxDrive dr, VALUE handler, VALUE io, SaxOptions opti
|
|
|
326
360
|
}
|
|
327
361
|
|
|
328
362
|
void ox_sax_drive_cleanup(SaxDrive dr) {
|
|
363
|
+
// A handler can keep the Value past the parse, or a raise can skip the
|
|
364
|
+
// detach after the callback, so make sure it no longer points at dr.
|
|
365
|
+
DATA_PTR(dr->value_obj) = NULL;
|
|
329
366
|
rb_gc_unregister_address(&dr->value_obj);
|
|
330
367
|
buf_cleanup(&dr->buf);
|
|
331
368
|
stack_cleanup(&dr->stack);
|
|
@@ -1105,7 +1142,10 @@ END_OF_BUF:
|
|
|
1105
1142
|
dr->set_line(dr->handler, line);
|
|
1106
1143
|
dr->set_col(dr->handler, col);
|
|
1107
1144
|
*args = dr->value_obj;
|
|
1145
|
+
// Attached for the callback alone, see attr_value().
|
|
1146
|
+
DATA_PTR(dr->value_obj) = dr;
|
|
1108
1147
|
rb_funcall2(dr->handler, ox_value_id, 1, args);
|
|
1148
|
+
DATA_PTR(dr->value_obj) = NULL;
|
|
1109
1149
|
} else if (dr->has_text) {
|
|
1110
1150
|
if (dr->options.convert_special) {
|
|
1111
1151
|
ox_sax_collapse_special(dr, dr->buf.str, pos, line, col);
|
|
@@ -1434,8 +1474,12 @@ int ox_sax_collapse_special(SaxDrive dr, char *str, long pos, long line, long co
|
|
|
1434
1474
|
x = '\0';
|
|
1435
1475
|
end = read_10_uint64(s, &u);
|
|
1436
1476
|
}
|
|
1437
|
-
if (0 == end) {
|
|
1438
|
-
|
|
1477
|
+
if (0 == end || ox_ucs_has_nul(u)) {
|
|
1478
|
+
if (0 == end) {
|
|
1479
|
+
ox_sax_drive_error(dr, NO_TERM "special character does not end with a semicolon");
|
|
1480
|
+
} else {
|
|
1481
|
+
ox_sax_drive_error(dr, INVALID_FORMAT "character reference resolves to a NUL");
|
|
1482
|
+
}
|
|
1439
1483
|
*b++ = '&';
|
|
1440
1484
|
*b++ = '#';
|
|
1441
1485
|
if ('\0' != x) {
|