Skip to content

Commit

Permalink
Merge pull request #113 from siv2r/reflect-exception-create
Browse files Browse the repository at this point in the history
completed copy for exception_create
  • Loading branch information
viferga authored Apr 19, 2021
2 parents cc9d793 + 0fcdbff commit 4baa356
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions source/reflect/source/reflect_exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,84 @@ exception exception_create(const char *message, const char *label, int code, con

if (ex == NULL)
{
return NULL;
goto exception_bad_alloc;
}

if (message != NULL)
{
size_t message_size = strlen(message) + 1;

ex->message = malloc(sizeof(char) * message_size);

if (ex->message == NULL)
{
goto message_bad_alloc;
}

memcpy(ex->message, message, message_size);
}
else
{
ex->message = NULL;
}

// TODO: Copy
if (label != NULL)
{
size_t label_size = strlen(label) + 1;

ex->label = malloc(sizeof(char) * label_size);

if (ex->label == NULL)
{
goto label_bad_alloc;
}

memcpy(ex->label, label, label_size);
}
else
{
ex->label = NULL;
}

if (stacktrace != NULL)
{
size_t stacktrace_size = strlen(stacktrace) + 1;

ex->stacktrace = malloc(sizeof(char) * stacktrace_size);

if (ex->stacktrace == NULL)
{
goto stacktrace_bad_alloc;
}

memcpy(ex->stacktrace, stacktrace, stacktrace_size);
}
else
{
ex->stacktrace = NULL;
}

ex->code = code;

ex->id = thread_id_get_current();

return ex;

stacktrace_bad_alloc:

free(ex->label);

label_bad_alloc:

free(ex->message);

message_bad_alloc:

free(ex);

exception_bad_alloc:

return NULL;
}

const char *exception_message(exception ex)
Expand Down

0 comments on commit 4baa356

Please sign in to comment.