Skip to content

Commit

Permalink
Fix memory dup free issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ohler55 committed May 20, 2022
1 parent 2d89ffb commit e17886f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
@@ -1,9 +1,10 @@
# CHANGELOG

## 3.13.12 - <not released>
## 3.13.12 - 2022-05-20

- Fixed crash on no arguments to pretty_generate. Now raises an exception.
- Register all classes and globals.
- Fixed memory issue with dumping.

## 3.13.11 - 2022-01-05

Expand Down
12 changes: 6 additions & 6 deletions ext/oj/dump.c
Expand Up @@ -562,7 +562,6 @@ void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int arg
if (0 == out->buf) {
oj_out_init(out);
}
out->cur = out->buf;
out->circ_cnt = 0;
out->opts = copts;
out->hash_cnt = 0;
Expand Down Expand Up @@ -685,7 +684,6 @@ void oj_write_obj_to_stream(VALUE obj, VALUE stream, Options copts) {
oj_out_free(&out);
rb_raise(rb_eArgError, "to_stream() expected an IO Object.");
}

oj_out_free(&out);
}

Expand Down Expand Up @@ -936,14 +934,15 @@ void oj_dump_raw(const char *str, size_t cnt, Out out) {
}

void oj_out_init(Out out) {
out->buf = out->stack_buffer;
out->end = out->buf + sizeof(out->stack_buffer) - BUFFER_EXTRA;
out->allocated = false;
out->buf = out->stack_buffer;
out->cur = out->buf;
out->end = out->buf + sizeof(out->stack_buffer) - BUFFER_EXTRA;
out->allocated = false;
}

void oj_out_free(Out out) {
if (out->allocated) {
xfree(out->buf);
xfree(out->buf); // TBD
}
}

Expand All @@ -952,6 +951,7 @@ void oj_grow_out(Out out, size_t len) {
long pos = out->cur - out->buf;
char *buf = out->buf;

printf("*** grow %ld\n", len);
size *= 2;
if (size <= len * 2 + pos) {
size += len;
Expand Down
14 changes: 11 additions & 3 deletions ext/oj/string_writer.c
Expand Up @@ -48,10 +48,18 @@ void oj_str_writer_init(StrWriter sw, int buf_size) {
*sw->types = '\0';
sw->keyWritten = 0;

oj_out_init(&sw->out);
assure_size(&sw->out, buf_size);

if (0 == buf_size) {
buf_size = 4096;
} else if (buf_size < 1024) {
buf_size = 1024;
}
// Must be allocated. Using the out.stack_buffer results in double frees
// and I haven't figured out why yet.
sw->out.buf = ALLOC_N(char, buf_size);
sw->out.cur = sw->out.buf;
sw->out.end = sw->out.buf + buf_size - BUFFER_EXTRA;
sw->out.allocated = true;

*sw->out.cur = '\0';
sw->out.circ_cache = NULL;
sw->out.circ_cnt = 0;
Expand Down

0 comments on commit e17886f

Please sign in to comment.