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.
data/ext/ox/sax_as.c CHANGED
@@ -22,7 +22,7 @@
22
22
  #include "time_conv.h"
23
23
 
24
24
  static VALUE parse_double_time(const char *text) {
25
- time_t v = 0;
25
+ ox_time_t v = 0;
26
26
  long v2 = 0;
27
27
  const char *dot = 0;
28
28
  char c;
@@ -38,7 +38,12 @@ static VALUE parse_double_time(const char *text) {
38
38
  if (c < '0' || '9' < c) {
39
39
  return Qnil;
40
40
  }
41
- v = 10 * v + (time_t)(c - '0');
41
+ // Past the largest ox_time_t the seconds would wrap to whatever the document
42
+ // picked, so leave a count that long to the parsers below.
43
+ if ((OX_TIME_MAX - (c - '0')) / 10 < v) {
44
+ return Qnil;
45
+ }
46
+ v = 10 * v + (ox_time_t)(c - '0');
42
47
  }
43
48
  dot = text++;
44
49
  for (; '\0' != *text && text - dot <= 6; text++) {
@@ -52,7 +57,7 @@ static VALUE parse_double_time(const char *text) {
52
57
  v2 *= 10;
53
58
  }
54
59
  if (neg) {
55
- // rb_time_nano_new() wants a non negative nanosecond count, so the
60
+ // ox_time_nano_new() wants a non negative nanosecond count, so the
56
61
  // fraction goes back into the second count the way dump_time_thin()
57
62
  // took it out.
58
63
  if (0 == v2) {
@@ -62,7 +67,7 @@ static VALUE parse_double_time(const char *text) {
62
67
  v2 = 1000000000L - v2;
63
68
  }
64
69
  }
65
- return rb_time_nano_new(v, v2);
70
+ return ox_time_nano_new(v, v2);
66
71
  }
67
72
 
68
73
  typedef struct _tp {
@@ -128,33 +133,52 @@ static VALUE parse_xsd_time(const char *text) {
128
133
  }
129
134
  // mktime() would read the wall clock as local time and throw the offset
130
135
  // away, and returns -1 for everything before the epoch on Windows.
131
- return rb_time_nano_new(
132
- (time_t)(ox_epoch_from_civil(cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5]) - offset),
136
+ return ox_time_nano_new(
137
+ (ox_time_t)(ox_epoch_from_civil(cargs[0], cargs[1], cargs[2], cargs[3], cargs[4], cargs[5]) - offset),
133
138
  nsec);
134
139
  }
135
140
 
141
+ // A Value is only attached to the drive while the value() or attr_value()
142
+ // callback it was passed to runs. Outside of that the drive is gone or reading
143
+ // something else.
144
+ static SaxDrive value_drive(VALUE self) {
145
+ SaxDrive dr;
146
+
147
+ TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
148
+ if (NULL == dr) {
149
+ rb_raise(ox_arg_error_class, "Ox::Sax::Value can only be used in the callback it was passed to.\n");
150
+ }
151
+ return dr;
152
+ }
153
+
136
154
  /* call-seq: as_s()
137
155
  *
138
156
  * *return* value as an String.
139
157
  */
140
158
  static VALUE sax_value_as_s(VALUE self) {
141
- SaxDrive dr;
142
- VALUE rs;
159
+ SaxDrive dr;
160
+ volatile VALUE rs;
161
+ char *s;
143
162
 
144
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
163
+ dr = value_drive(self);
145
164
 
146
165
  if ('\0' == *dr->buf.str) {
147
166
  return Qnil;
148
167
  }
168
+ // Collapse a copy. dr->buf.str is what the next as_s reads, so collapsing it
169
+ // in place decoded the value once more on every call: &amp;lt; came back as
170
+ // &lt; and then as <.
171
+ rs = rb_str_new2(dr->buf.str);
172
+ s = RSTRING_PTR(rs);
149
173
  if (dr->options.convert_special) {
150
- ox_sax_collapse_special(dr, dr->buf.str, dr->buf.pos, dr->buf.line, dr->buf.col);
174
+ ox_sax_collapse_special(dr, s, dr->buf.pos, dr->buf.line, dr->buf.col);
151
175
  }
152
176
  switch (dr->options.skip) {
153
- case CrSkip: buf_collapse_return(dr->buf.str); break;
154
- case SpcSkip: buf_collapse_white(dr->buf.str); break;
177
+ case CrSkip: buf_collapse_return(s); break;
178
+ case SpcSkip: buf_collapse_white(s); break;
155
179
  default: break;
156
180
  }
157
- rs = rb_str_new2(dr->buf.str);
181
+ rb_str_set_len(rs, (long)strlen(s));
158
182
  if (0 != dr->encoding) {
159
183
  rb_enc_associate(rs, dr->encoding);
160
184
  }
@@ -168,7 +192,7 @@ static VALUE sax_value_as_s(VALUE self) {
168
192
  static VALUE sax_value_as_sym(VALUE self) {
169
193
  SaxDrive dr;
170
194
 
171
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
195
+ dr = value_drive(self);
172
196
 
173
197
  if ('\0' == *dr->buf.str) {
174
198
  return Qnil;
@@ -183,7 +207,7 @@ static VALUE sax_value_as_sym(VALUE self) {
183
207
  static VALUE sax_value_as_f(VALUE self) {
184
208
  SaxDrive dr;
185
209
 
186
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
210
+ dr = value_drive(self);
187
211
 
188
212
  if ('\0' == *dr->buf.str) {
189
213
  return Qnil;
@@ -200,9 +224,10 @@ static VALUE sax_value_as_i(VALUE self) {
200
224
  const char *s;
201
225
  long n = 0;
202
226
  int neg = 0;
227
+ bool big = false;
203
228
 
204
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
205
- s = dr->buf.str;
229
+ dr = value_drive(self);
230
+ s = dr->buf.str;
206
231
 
207
232
  if ('\0' == *s) {
208
233
  return Qnil;
@@ -215,11 +240,20 @@ static VALUE sax_value_as_i(VALUE self) {
215
240
  }
216
241
  for (; '\0' != *s; s++) {
217
242
  if ('0' <= *s && *s <= '9') {
218
- n = n * 10 + (*s - '0');
243
+ // Past LONG_MAX n would wrap to whatever value the document picked, so
244
+ // stop there and let Ruby build the Integer once the rest is checked.
245
+ if (big || (LONG_MAX - (*s - '0')) / 10 < n) {
246
+ big = true;
247
+ } else {
248
+ n = n * 10 + (*s - '0');
249
+ }
219
250
  } else {
220
251
  rb_raise(ox_arg_error_class, "Not a valid Fixnum.\n");
221
252
  }
222
253
  }
254
+ if (big) {
255
+ return rb_cstr_to_inum(dr->buf.str, 10, 0);
256
+ }
223
257
  if (neg) {
224
258
  n = -n;
225
259
  }
@@ -235,7 +269,7 @@ static VALUE sax_value_as_time(VALUE self) {
235
269
  const char *str;
236
270
  VALUE t;
237
271
 
238
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
272
+ dr = value_drive(self);
239
273
  str = dr->buf.str;
240
274
 
241
275
  if ('\0' == *str) {
@@ -258,7 +292,7 @@ static VALUE sax_value_as_time(VALUE self) {
258
292
  static VALUE sax_value_as_bool(VALUE self) {
259
293
  SaxDrive dr;
260
294
 
261
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
295
+ dr = value_drive(self);
262
296
  return (0 == strcasecmp("true", dr->buf.str)) ? Qtrue : Qfalse;
263
297
  }
264
298
 
@@ -269,7 +303,7 @@ static VALUE sax_value_as_bool(VALUE self) {
269
303
  static VALUE sax_value_empty(VALUE self) {
270
304
  SaxDrive dr;
271
305
 
272
- TypedData_Get_Struct(self, struct _saxDrive, &ox_sax_value_type, dr);
306
+ dr = value_drive(self);
273
307
  return ('\0' == *dr->buf.str) ? Qtrue : Qfalse;
274
308
  }
275
309
 
data/ext/ox/sax_buf.c CHANGED
@@ -32,14 +32,17 @@ void ox_sax_buf_init(Buf buf, VALUE io) {
32
32
  volatile VALUE io_class = rb_obj_class(io);
33
33
  VALUE rfd;
34
34
 
35
+ buf->str_remaining_len = 0;
35
36
  if (rb_cString == io_class) {
36
- buf->read_func = read_from_str;
37
- buf->in.str = StringValuePtr(io);
37
+ buf->read_func = read_from_str;
38
+ buf->in.str = StringValuePtr(io);
39
+ buf->str_remaining_len = strlen(buf->in.str);
38
40
  } else if (ox_stringio_class == io_class && 0 == FIX2INT(rb_funcall2(io, ox_pos_id, 0, 0))) {
39
41
  volatile VALUE s = rb_funcall2(io, ox_string_id, 0, 0);
40
42
 
41
- buf->read_func = read_from_str;
42
- buf->in.str = StringValuePtr(s);
43
+ buf->read_func = read_from_str;
44
+ buf->in.str = StringValuePtr(s);
45
+ buf->str_remaining_len = strlen(buf->in.str);
43
46
  } else if (rb_cFile == io_class && Qnil != (rfd = rb_funcall(io, ox_fileno_id, 0))) {
44
47
  buf->read_func = read_from_fd;
45
48
  buf->in.fd = FIX2INT(rfd);
@@ -116,16 +119,35 @@ int ox_sax_buf_read(Buf buf) {
116
119
  return err;
117
120
  }
118
121
 
122
+ // rb_rescue hands the handler the exception instance, not its class. Passing
123
+ // that instance to rb_raise(), which expects a class, made Ruby raise a
124
+ // TypeError about the argument type while building the new exception, so the
125
+ // caller saw that TypeError instead of the error its IO raised. Re-raise the
126
+ // original error with the position appended, keeping its class.
127
+ //
128
+ // EOFError still ends the parse: that is how readpartial() signals end of
129
+ // input. TypeError is no longer swallowed here; read() returning nil at EOF is
130
+ // recognized in io_cb() rather than through the TypeError StringValuePtr()
131
+ // raised on it.
132
+ //
133
+ // Raising here is safe: ox_sax_drive_cleanup() runs after the rb_protect() in
134
+ // sax_parse() catches this.
119
135
  static VALUE rescue_cb(VALUE rbuf, VALUE err) {
120
- VALUE err_class = rb_obj_class(err);
136
+ Buf buf = (Buf)rbuf;
137
+ VALUE msg;
121
138
 
122
- if (err_class != rb_eTypeError && err_class != rb_eEOFError) {
123
- Buf buf = (Buf)rbuf;
124
-
125
- // ox_sax_drive_cleanup(buf->dr); called after exiting protect
126
- rb_raise(err, "at line %ld, column %ld\n", (long)buf->line, (long)buf->col);
139
+ if (rb_obj_is_kind_of(err, rb_eEOFError)) {
140
+ return Qfalse;
127
141
  }
128
- return Qfalse;
142
+ msg = rb_sprintf("%" PRIsVALUE " at line %ld, column %ld",
143
+ rb_funcall(err, ox_message_id, 0),
144
+ (long)buf->line,
145
+ (long)buf->col);
146
+ // Exception#exception clones the receiver and swaps in the new message, so
147
+ // the class survives whatever initialize() it happens to define.
148
+ rb_exc_raise(rb_funcall(err, ox_exception_id, 1, msg));
149
+
150
+ return Qfalse; // not reached
129
151
  }
130
152
 
131
153
  static VALUE partial_io_cb(VALUE rbuf) {
@@ -138,7 +160,10 @@ static VALUE partial_io_cb(VALUE rbuf) {
138
160
 
139
161
  args[0] = ULONG2NUM(max);
140
162
  rstr = rb_funcall2(buf->in.io, ox_readpartial_id, 1, args);
141
- str = StringValuePtr(rstr);
163
+ if (NIL_P(rstr)) { // end of input
164
+ return Qfalse;
165
+ }
166
+ str = StringValuePtr(rstr);
142
167
  // Clamp to the space actually requested. A misbehaving IO can return more
143
168
  // than max bytes; copying it unclamped overflows the buffer (which starts
144
169
  // on the C stack). Use the real byte length, not strlen(), so an embedded
@@ -164,7 +189,13 @@ static VALUE io_cb(VALUE rbuf) {
164
189
 
165
190
  args[0] = ULONG2NUM(max);
166
191
  rstr = rb_funcall2(buf->in.io, ox_read_id, 1, args);
167
- str = StringValuePtr(rstr);
192
+ // IO#read(len) returns nil at EOF. That used to reach StringValuePtr(),
193
+ // whose TypeError rescue_cb() read as end of input; say so directly so
194
+ // rescue_cb() no longer has to treat every TypeError as EOF.
195
+ if (NIL_P(rstr)) {
196
+ return Qfalse;
197
+ }
198
+ str = StringValuePtr(rstr);
168
199
  // See partial_io_cb: clamp to the requested size to prevent overflow.
169
200
  cnt = (size_t)RSTRING_LEN(rstr);
170
201
  if (cnt > max) {
@@ -201,23 +232,23 @@ static int read_from_fd(Buf buf) {
201
232
  return 0;
202
233
  }
203
234
 
235
+ // What is left of the string is carried in the buffer rather than measured
236
+ // again here. Measuring it per refill walks the rest of the document every
237
+ // 4KB, which makes reading a String quadratic in its length. The caller writes
238
+ // the terminator at read_end, as it does for the other readers.
204
239
  static int read_from_str(Buf buf) {
205
240
  size_t max = buf->end - buf->tail - 1;
206
- char *s;
207
- size_t cnt;
241
+ size_t cnt = buf->str_remaining_len;
208
242
 
209
- if ('\0' == *buf->in.str) {
243
+ if (0 == cnt || max < 2) {
210
244
  return -1;
211
245
  }
212
- cnt = strlen(buf->in.str) + 1;
213
- if (max < cnt) {
214
- cnt = max;
246
+ if (max <= cnt) {
247
+ cnt = max - 1;
215
248
  }
216
249
  memcpy(buf->tail, buf->in.str, cnt);
217
- s = buf->tail + cnt - 1;
218
- *s = '\0';
219
- cnt = s - buf->tail;
220
250
  buf->in.str += cnt;
251
+ buf->str_remaining_len -= cnt;
221
252
  buf->read_end = buf->tail + cnt;
222
253
 
223
254
  return 0;
data/ext/ox/sax_buf.h CHANGED
@@ -28,6 +28,7 @@ typedef struct _buf {
28
28
  VALUE io;
29
29
  const char *str;
30
30
  } in;
31
+ size_t str_remaining_len; /* bytes of in.str still to copy, the length strlen() gave at init */
31
32
  struct _saxDrive *dr;
32
33
  } *Buf;
33
34
 
data/ext/ox/special.c CHANGED
@@ -52,6 +52,30 @@ char *ox_ucs_to_utf8_chars(char *text, uint64_t u) {
52
52
  return text;
53
53
  }
54
54
 
55
+ // True if ox_ucs_to_utf8_chars() would write a NUL for u: &#0; itself, or a
56
+ // value it copies out byte for byte (a surrogate or past U+10FFFF) with a zero
57
+ // byte after the first non-zero one, such as &#xD800; or &#x110000;. The
58
+ // callers collapse text in place and the result is used as a C string, so the
59
+ // NUL silently dropped everything after the reference.
60
+ bool ox_ucs_has_nul(uint64_t u) {
61
+ int i = 56;
62
+
63
+ if (0 == u) {
64
+ return true;
65
+ }
66
+ if (u <= 0x000000000010FFFFULL && (u < 0x000000000000D800ULL || 0x000000000000DFFFULL < u)) {
67
+ return false;
68
+ }
69
+ for (; 0 == ((u >> i) & 0x00000000000000FFULL); i -= 8) {
70
+ }
71
+ for (; 0 <= i; i -= 8) {
72
+ if (0 == ((u >> i) & 0x00000000000000FFULL)) {
73
+ return true;
74
+ }
75
+ }
76
+ return false;
77
+ }
78
+
55
79
  #define BUCKET_SIZE 256
56
80
  #define BUCKET_MASK 255
57
81
 
data/ext/ox/special.h CHANGED
@@ -6,9 +6,11 @@
6
6
  #ifndef OX_SPECIAL_H
7
7
  #define OX_SPECIAL_H
8
8
 
9
+ #include <stdbool.h>
9
10
  #include <stdint.h>
10
11
 
11
12
  extern char *ox_ucs_to_utf8_chars(char *text, uint64_t u);
13
+ extern bool ox_ucs_has_nul(uint64_t u);
12
14
  extern char *ox_entity_lookup(char *text, const char *key);
13
15
 
14
16
  #endif /* OX_SPECIAL_H */
data/ext/ox/time_conv.h CHANGED
@@ -9,6 +9,63 @@
9
9
  #include <stdint.h>
10
10
  #include <time.h>
11
11
 
12
+ #include "ruby.h"
13
+
14
+ // ox_time_t is the second count Ox converts Time objects to and from.
15
+ //
16
+ // Where Ruby's time_t is 64 bits it is time_t, and the conversions go straight
17
+ // through rb_time_nano_new() and rb_time_timespec().
18
+ //
19
+ // Where it is narrower -- 32-bit glibc without _TIME_BITS=64 -- it is int64_t,
20
+ // and the conversions go through Time.at, Time#to_i and Time#nsec instead, so
21
+ // no time_t crosses into Ruby. That matters twice over. A 32-bit time_t only
22
+ // reaches 1901 to 2038, and Ruby's Time does not have that limit. And a
23
+ // precompiled binary can meet a Ruby with a different time_t: 32-bit ARM
24
+ // distros that moved to a 64-bit time_t (Debian 13, Ubuntu 24.04) build Ruby
25
+ // with one, while cross compilers for that target still default to 32 bits. A
26
+ // time_t passed between the two is read at the wrong width, and on 32-bit ARM
27
+ // that garbles the nanoseconds as well as the seconds.
28
+ #if SIZEOF_TIME_T >= 8
29
+
30
+ typedef time_t ox_time_t;
31
+
32
+ // The largest time_t, which C has no name for. Built from two halves so that no
33
+ // step overflows on its way to the sign bit.
34
+ #define OX_TIME_MAX ((time_t)((((time_t)1 << (sizeof(time_t) * 8 - 2)) - 1) + ((time_t)1 << (sizeof(time_t) * 8 - 2))))
35
+
36
+ // A local Time for sec seconds and nsec (0 to 999,999,999) nanoseconds.
37
+ inline static VALUE ox_time_nano_new(ox_time_t sec, long nsec) {
38
+ return rb_time_nano_new(sec, nsec);
39
+ }
40
+
41
+ // Splits a Time into whole seconds, rounded down, and the nanoseconds past
42
+ // them, which are never negative.
43
+ inline static void ox_time_split(VALUE time, ox_time_t *secp, long *nsecp) {
44
+ struct timespec ts = rb_time_timespec(time);
45
+
46
+ *secp = ts.tv_sec;
47
+ *nsecp = ts.tv_nsec;
48
+ }
49
+
50
+ #else
51
+
52
+ typedef int64_t ox_time_t;
53
+
54
+ #define OX_TIME_MAX INT64_MAX
55
+
56
+ inline static VALUE ox_time_nano_new(ox_time_t sec, long nsec) {
57
+ return rb_funcall(rb_cTime, rb_intern("at"), 3, LL2NUM(sec), LONG2NUM(nsec), ID2SYM(rb_intern("nanosecond")));
58
+ }
59
+
60
+ // Time#to_i rounds down and Time#nsec is never negative, the same split
61
+ // rb_time_timespec() makes. A Time past the int64_t range raises RangeError.
62
+ inline static void ox_time_split(VALUE time, ox_time_t *secp, long *nsecp) {
63
+ *secp = (ox_time_t)NUM2LL(rb_funcall(time, rb_intern("to_i"), 0));
64
+ *nsecp = NUM2LONG(rb_funcall(time, rb_intern("nsec"), 0));
65
+ }
66
+
67
+ #endif
68
+
12
69
  // Howard Hinnant's civil_from_days and days_from_civil, which are inverses.
13
70
  // Neither consults a timezone database, and that is the point: mktime() reads
14
71
  // the wall clock as local time no matter what offset the document carries, and
data/lib/ox/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Ox
2
2
  # Current version of the module.
3
- VERSION = '2.14.29'
3
+ VERSION = '2.14.30'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ox
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.14.29
4
+ version: 2.14.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler