Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions references/cpp/src/reference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,27 @@ void clear_context(context_t* context) {
}
}

uchar_t* create_string() {
return reinterpret_cast<uchar_t*>(calloc(MAX_FORMAT, sizeof(uchar_t)));
uchar_t* create_string(usize_t size = MAX_FORMAT) {
if (size == UINTPTR_MAX) {
abort();
}

auto value = reinterpret_cast<uchar_t*>(calloc(size + 1, sizeof(uchar_t)));
if (value == NULL) {
abort();
}
return value;
}

void append_output(const milo_parser::Parser* parser, uchar_t* message, const uchar_t* data, usize_t from,
usize_t size) {
if (data == NULL) {
printf("{ %s, \"data\": null }\n", message);
} else {
uchar_t* string_data = create_string();
strncpy(reinterpret_cast<char*>(string_data), reinterpret_cast<const char*>(data), size);
uchar_t* string_data = create_string(size);
memcpy(string_data, data, size);
printf("{ %s, \"data\": \"%s\" }\n", message, string_data);
free(string_data);
}

free(message);
Expand All @@ -69,8 +78,10 @@ void event(const milo_parser::Parser* parser, const char* name, const uchar_t* d

void show_span(const milo_parser::Parser* parser, const char* name, const uchar_t* data, usize_t from, usize_t size) {
auto context = reinterpret_cast<context_t*>(parser->context);
uchar_t* string_data = create_string();
strncpy(reinterpret_cast<char*>(string_data), reinterpret_cast<const char*>(data), size);
uchar_t* string_data = create_string(size);
if (size > 0) {
memcpy(string_data, data, size);
}

if (strcmp(name, "method") == 0) {
context->method = string_data;
Expand All @@ -95,8 +106,8 @@ uchar_t* copy_string(const char* source, usize_t size) {
size = strlen(source);
}

auto destination = reinterpret_cast<uchar_t*>(malloc(sizeof(uchar_t) * size));
strncpy(reinterpret_cast<char*>(destination), source, size);
auto destination = create_string(size);
memcpy(destination, source, size);
return destination;
}

Expand Down
Loading