Skip to content

Commit d5dcfeb

Browse files
committed
Add allocator back but only for copying the contents of deserializer.
1 parent e7fb6ed commit d5dcfeb

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

source/serials/rapid_json_serial/source/rapid_json_serial_impl.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@
2121

2222
/* -- Type Definitions -- */
2323

24+
typedef struct rapid_json_document_type
25+
{
26+
rapidjson::Document impl;
27+
memory_allocator allocator;
28+
29+
} * rapid_json_document;
30+
2431
/* -- Private Methods -- */
2532

2633
static void rapid_json_serial_impl_serialize_value(value v, rapidjson::Value * json_v);
2734

28-
static char * rapid_json_serial_impl_document_stringify(rapidjson::Document * document, size_t * size);
35+
static char * rapid_json_serial_impl_document_stringify(rapid_json_document document, size_t * size);
2936

3037
static value rapid_json_serial_impl_deserialize_value(const rapidjson::Value * v);
3138

@@ -45,20 +52,17 @@ const char * rapid_json_serial_impl_extension()
4552

4653
serial_impl_handle rapid_json_serial_impl_initialize(memory_allocator allocator, serial_host host)
4754
{
48-
rapidjson::Document * document;
49-
50-
// Aparently, using your own memory allocator generates a heap buffer overflow
51-
(void)allocator;
55+
rapid_json_document document = new rapid_json_document_type();
5256

5357
log_copy(host->log);
5458

55-
document = new rapidjson::Document();
56-
5759
if (document == nullptr)
5860
{
5961
return NULL;
6062
}
6163

64+
document->allocator = allocator;
65+
6266
return (serial_impl_handle)document;
6367
}
6468

@@ -269,14 +273,14 @@ void rapid_json_serial_impl_serialize_value(value v, rapidjson::Value * json_v)
269273
}
270274
}
271275

272-
char * rapid_json_serial_impl_document_stringify(rapidjson::Document * document, size_t * size)
276+
char * rapid_json_serial_impl_document_stringify(rapid_json_document document, size_t * size)
273277
{
274278
rapidjson::StringBuffer buffer;
275279
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
276-
document->Accept(writer);
280+
document->impl.Accept(writer);
277281
size_t buffer_size = buffer.GetSize();
278282
size_t buffer_str_size = buffer_size + 1;
279-
char * buffer_str = static_cast<char *>(malloc(sizeof(char) * buffer_str_size));
283+
char * buffer_str = static_cast<char *>(memory_allocator_allocate(document->allocator, sizeof(char) * buffer_str_size));
280284

281285
if (buffer_str == NULL)
282286
{
@@ -295,7 +299,7 @@ char * rapid_json_serial_impl_document_stringify(rapidjson::Document * document,
295299

296300
char * rapid_json_serial_impl_serialize(serial_impl_handle handle, value v, size_t * size)
297301
{
298-
rapidjson::Document * document = (rapidjson::Document *)handle;
302+
rapid_json_document document = static_cast<rapid_json_document>(handle);
299303

300304
if (handle == NULL || v == NULL || size == NULL)
301305
{
@@ -304,7 +308,7 @@ char * rapid_json_serial_impl_serialize(serial_impl_handle handle, value v, size
304308
return NULL;
305309
}
306310

307-
rapid_json_serial_impl_serialize_value(v, document);
311+
rapid_json_serial_impl_serialize_value(v, &document->impl);
308312

309313
return rapid_json_serial_impl_document_stringify(document, size);
310314
}
@@ -435,7 +439,7 @@ value rapid_json_serial_impl_deserialize_value(const rapidjson::Value * v)
435439

436440
value rapid_json_serial_impl_deserialize(serial_impl_handle handle, const char * buffer, size_t size)
437441
{
438-
rapidjson::Document * document = (rapidjson::Document *)handle;
442+
rapid_json_document document = static_cast<rapid_json_document>(handle);
439443

440444
if (handle == NULL || buffer == NULL || size == 0)
441445
{
@@ -444,7 +448,7 @@ value rapid_json_serial_impl_deserialize(serial_impl_handle handle, const char *
444448
return NULL;
445449
}
446450

447-
rapidjson::ParseResult parse_result = document->Parse(buffer, size - 1);
451+
rapidjson::ParseResult parse_result = document->impl.Parse(buffer, size - 1);
448452

449453
if (parse_result.IsError() == true)
450454
{
@@ -456,14 +460,14 @@ value rapid_json_serial_impl_deserialize(serial_impl_handle handle, const char *
456460
return NULL;
457461
}
458462

459-
return rapid_json_serial_impl_deserialize_value(document);
463+
return rapid_json_serial_impl_deserialize_value(&document->impl);
460464
}
461465

462466
int rapid_json_serial_impl_destroy(serial_impl_handle handle)
463467
{
464-
rapidjson::Document * document = (rapidjson::Document *)handle;
468+
rapid_json_document document = static_cast<rapid_json_document>(handle);
465469

466-
if (document != nullptr)
470+
if (document != NULL)
467471
{
468472
delete document;
469473
}

0 commit comments

Comments
 (0)