diff --git a/tools/json2cbor/json2cbor.c b/tools/json2cbor/json2cbor.c index 090ee158..82b0a2c3 100644 --- a/tools/json2cbor/json2cbor.c +++ b/tools/json2cbor/json2cbor.c @@ -328,15 +328,16 @@ CborError decode_json(cJSON *json, CborEncoder *encoder) err = cbor_encode_double(encoder, json->valuedouble); if (err == CborErrorOutOfMemory) { - buffersize += 1024; - uint8_t *newbuffer = realloc(buffer, buffersize); + ptrdiff_t offset = cbor_encoder_get_buffer_size(&container, buffer); + uint8_t *newbuffer = realloc(buffer, buffersize + 1024); if (newbuffer == NULL) return err; *encoder = container; // restore state - encoder->data.ptr = newbuffer + (container.data.ptr - buffer); + encoder->data.ptr = newbuffer + offset; encoder->end = newbuffer + buffersize; buffer = newbuffer; + buffersize += 1024; goto encode_double; } return err;