ext/ox/dump.c in ox-1.2.6 vs ext/ox/dump.c in ox-1.2.7

- old
+ new

@@ -218,14 +218,14 @@ size_t size = out->end - out->buf; long pos = out->cur - out->buf; char *buf; size *= 2; - if (size < len + pos) { + if (size <= len * 2 + pos) { size += len; } - if (0 == (buf = (char*)realloc(out->buf, size))) { + if (0 == (buf = (char*)realloc(out->buf, size + 10))) { // 1 extra for terminator character plus extra (paranoid) rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC)); } out->buf = buf; out->end = buf + size; out->cur = out->buf + pos; @@ -242,11 +242,11 @@ size += e->clas.len + 5; } if (0 < e->id) { // i="id" size += 24; // over estimate, 19 digits } - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } if (out->buf < out->cur) { fill_indent(out, e->indent); } @@ -274,11 +274,11 @@ static void dump_end(Out out, Element e) { size_t size = e->indent + 5; - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } fill_indent(out, e->indent); *out->cur++ = '<'; *out->cur++ = '/'; @@ -287,11 +287,11 @@ *out->cur = '\0'; } inline static void dump_value(Out out, const char *value, size_t size) { - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } if (6 < size) { memcpy(out->cur, value, size); out->cur += size; @@ -325,11 +325,11 @@ b++; } } else { *b = '0'; } - if (out->end - out->cur < (long)(sizeof(buf) - (b - buf))) { + if (out->end - out->cur <= (long)(sizeof(buf) - (b - buf))) { grow(out, sizeof(buf) - (b - buf)); } for (; '\0' != *b; b++) { *out->cur++ = *b; } @@ -353,11 +353,11 @@ for (; 0 < sec; b--, sec /= 10) { *b = '0' + (sec % 10); } b++; size = sizeof(buf) - (b - buf) - 1; - if (out->end - out->cur < size) { + if (out->end - out->cur <= size) { grow(out, size); } memcpy(out->cur, b, size); out->cur += size; } @@ -368,11 +368,11 @@ time_t sec = NUM2LONG(rb_funcall2(obj, tv_sec_id, 0, 0)); long usec = NUM2LONG(rb_funcall2(obj, tv_usec_id, 0, 0)); int tzhour, tzmin; char tzsign = '+'; - if (out->end - out->cur < 33) { + if (out->end - out->cur <= 33) { grow(out, 33); } // 2010-07-09T10:47:45.895826+09:00 tm = localtime(&sec); if (0 > tm->tm_gmtoff) { @@ -798,11 +798,11 @@ indent = 0; } else { indent = depth * out->indent; } size = indent + 4 + nlen; - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } fill_indent(out, indent); *out->cur++ = '<'; fill_value(out, name, nlen); @@ -812,11 +812,11 @@ if (Qnil != nodes) { int do_indent; *out->cur++ = '>'; do_indent = dump_gen_nodes(nodes, depth, out); - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } if (do_indent) { fill_indent(out, indent); } @@ -865,11 +865,11 @@ dump_gen_attr(VALUE key, VALUE value, Out out) { const char *ks = (RUBY_T_SYMBOL == rb_type(key)) ? rb_id2name(SYM2ID(key)) : StringValuePtr(key); size_t klen = strlen(ks); size_t size = 4 + klen + RSTRING_LEN(value); - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } *out->cur++ = ' '; fill_value(out, ks, klen); *out->cur++ = '='; @@ -901,11 +901,11 @@ indent = 0; } else { indent = depth * out->indent; } size = indent + plen + slen + vlen; - if (out->end - out->cur < (long)size) { + if (out->end - out->cur <= (long)size) { grow(out, size); } fill_indent(out, indent); fill_value(out, pre, plen); fill_value(out, val, vlen); @@ -943,10 +943,9 @@ char* write_obj_to_str(VALUE obj, Options copts) { struct _Out out; dump_obj_to_xml(obj, copts, &out); - return out.buf; } void write_obj_to_file(VALUE obj, const char *path, Options copts) {