Expanded buf to copy at once
Build dumped string from base packed data and extended year at once. Although currently ruby_marshal_write_long() never writes more than 5 bytes per its format specification, allocate `sizeof(long)+1` for the sanitation.
This commit is contained in:
parent
ed2f2b4f98
commit
f487e5b7a4
15
time.c
15
time.c
@ -5016,7 +5016,7 @@ time_mdump(VALUE time)
|
|||||||
{
|
{
|
||||||
struct time_object *tobj;
|
struct time_object *tobj;
|
||||||
unsigned long p, s;
|
unsigned long p, s;
|
||||||
char buf[base_dump_size];
|
char buf[base_dump_size + sizeof(long) + 1];
|
||||||
int i;
|
int i;
|
||||||
VALUE str;
|
VALUE str;
|
||||||
|
|
||||||
@ -5083,7 +5083,6 @@ time_mdump(VALUE time)
|
|||||||
s = RSHIFT(s, 8);
|
s = RSHIFT(s, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
str = rb_str_new(buf, 8);
|
|
||||||
if (!NIL_P(year_extend)) {
|
if (!NIL_P(year_extend)) {
|
||||||
/*
|
/*
|
||||||
* Append extended year distance from 1900..(1900+0xffff). In
|
* Append extended year distance from 1900..(1900+0xffff). In
|
||||||
@ -5092,18 +5091,22 @@ time_mdump(VALUE time)
|
|||||||
* binary (like as Fixnum and Bignum).
|
* binary (like as Fixnum and Bignum).
|
||||||
*/
|
*/
|
||||||
size_t ysize = rb_absint_size(year_extend, NULL);
|
size_t ysize = rb_absint_size(year_extend, NULL);
|
||||||
char *p, buf_year_extend[sizeof(long)+1];
|
char *p, *const buf_year_extend = buf + base_dump_size;
|
||||||
if (ysize > LONG_MAX ||
|
if (ysize > LONG_MAX ||
|
||||||
(i = ruby_marshal_write_long((long)ysize, buf_year_extend)) < 0) {
|
(i = ruby_marshal_write_long((long)ysize, buf_year_extend)) < 0) {
|
||||||
rb_raise(rb_eArgError, "year too %s to marshal: %"PRIsVALUE" UTC",
|
rb_raise(rb_eArgError, "year too %s to marshal: %"PRIsVALUE" UTC",
|
||||||
(year == 1900 ? "small" : "big"), vtm.year);
|
(year == 1900 ? "small" : "big"), vtm.year);
|
||||||
}
|
}
|
||||||
rb_str_resize(str, sizeof(buf) + i + ysize);
|
i += base_dump_size;
|
||||||
p = RSTRING_PTR(str) + sizeof(buf);
|
str = rb_str_new(NULL, i + ysize);
|
||||||
memcpy(p, buf_year_extend, i);
|
p = RSTRING_PTR(str);
|
||||||
|
memcpy(p, buf, i);
|
||||||
p += i;
|
p += i;
|
||||||
rb_integer_pack(year_extend, p, ysize, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
|
rb_integer_pack(year_extend, p, ysize, 1, 0, INTEGER_PACK_LITTLE_ENDIAN);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
str = rb_str_new(buf, base_dump_size);
|
||||||
|
}
|
||||||
rb_copy_generic_ivar(str, time);
|
rb_copy_generic_ivar(str, time);
|
||||||
if (!rb_equal(nano, INT2FIX(0))) {
|
if (!rb_equal(nano, INT2FIX(0))) {
|
||||||
if (RB_TYPE_P(nano, T_RATIONAL)) {
|
if (RB_TYPE_P(nano, T_RATIONAL)) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user