Skip to content
Open
Show file tree
Hide file tree
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
51 changes: 48 additions & 3 deletions src/runtime/rc_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ void* rc_alloc_scratch(rc_parse_state_t* parse_state, uint32_t size, uint32_t sc
{
void* ptr;

/* after an error the offset is an error code, adding sizes to it would hide the error */
if (parse_state->offset < 0)
return NULL;

/* if we have a real buffer, then allocate the data there */
if (parse_state->buffer)
return rc_alloc(parse_state, size, scratch_object_pointer_offset);
Expand All @@ -41,6 +45,10 @@ void* rc_alloc(rc_parse_state_t* parse_state, uint32_t size, uint32_t scratch_ob
{
void* ptr;

/* after an error the offset is an error code, not a position in the buffer */
if (parse_state->offset < 0)
return NULL;

parse_state->offset = RC_ALIGN(parse_state->offset);

if (parse_state->buffer) {
Expand All @@ -62,6 +70,11 @@ void* rc_alloc(rc_parse_state_t* parse_state, uint32_t size, uint32_t scratch_ob
/* make sure the scratch pointer array is available */
if (!parse_state->scratch) {
parse_state->scratch = (rc_scratch_t*)rc_buffer_alloc(&parse_state->scratch_buffer, sizeof(rc_scratch_t));
if (!parse_state->scratch) {
parse_state->offset = RC_OUT_OF_MEMORY;
return NULL;
}

memset(parse_state->scratch, 0, sizeof(rc_scratch_t));
}

Expand Down Expand Up @@ -111,6 +124,8 @@ const char* rc_alloc_str(rc_parse_state_t* parse, const char* text, size_t lengt
if (parse->offset >= 0)
parse->offset = RC_OUT_OF_MEMORY;

/* don't leave a node without a value in the tree */
*next = NULL;
return NULL;
}

Expand Down Expand Up @@ -203,12 +218,25 @@ void rc_preparse_reserve_memrefs(rc_preparse_state_t* preparse, rc_memrefs_t* me
available = memref_list->capacity - memref_list->count;
if (available < num_memrefs) {
rc_memref_list_t* new_memref_list = (rc_memref_list_t*)calloc(1, sizeof(rc_memref_list_t));
if (!new_memref_list)
if (!new_memref_list) {
preparse->parse.offset = RC_OUT_OF_MEMORY;
return;
}

new_memref_list->capacity = (uint16_t)rc_preparse_array_size(num_memrefs - available, 16);
new_memref_list->items = (rc_memref_t*)malloc(new_memref_list->capacity * sizeof(rc_memref_t));
if (!new_memref_list->items) {
free(new_memref_list);
preparse->parse.offset = RC_OUT_OF_MEMORY;
return;
}

new_memref_list->allocated = 1;

/* link it at the end: a list in the middle may have room left after an
* activation failed, and the lists after it must not be lost */
while (memref_list->next)
memref_list = memref_list->next;
memref_list->next = new_memref_list;
}
}
Expand All @@ -225,12 +253,24 @@ void rc_preparse_reserve_memrefs(rc_preparse_state_t* preparse, rc_memrefs_t* me
available = modified_memref_list->capacity - modified_memref_list->count;
if (available < num_modified_memrefs) {
rc_modified_memref_list_t* new_modified_memref_list = (rc_modified_memref_list_t*)calloc(1, sizeof(rc_modified_memref_list_t));
if (!new_modified_memref_list)
if (!new_modified_memref_list) {
preparse->parse.offset = RC_OUT_OF_MEMORY;
return;
}

new_modified_memref_list->capacity = (uint16_t)rc_preparse_array_size(num_modified_memrefs - available, 8);
new_modified_memref_list->items = (rc_modified_memref_t*)malloc(new_modified_memref_list->capacity * sizeof(rc_modified_memref_t));
if (!new_modified_memref_list->items) {
free(new_modified_memref_list);
preparse->parse.offset = RC_OUT_OF_MEMORY;
return;
}

new_modified_memref_list->allocated = 1;

/* link it at the end, see above */
while (modified_memref_list->next)
modified_memref_list = modified_memref_list->next;
modified_memref_list->next = new_modified_memref_list;
}
}
Expand Down Expand Up @@ -290,8 +330,11 @@ void rc_preparse_copy_memrefs(rc_parse_state_t* parse, const rc_memrefs_t* memre
const rc_memref_t* memref = memref_list->items;
const rc_memref_t* memref_end = memref + memref_list->count;

for (; memref < memref_end; ++memref)
for (; memref < memref_end; ++memref) {
rc_alloc_memref(parse, memref->address, memref->value.size);
if (parse->offset < 0)
return;
}
}

for (; modified_memref_list; modified_memref_list = modified_memref_list->next) {
Expand All @@ -301,6 +344,8 @@ void rc_preparse_copy_memrefs(rc_parse_state_t* parse, const rc_memrefs_t* memre
for (; modified_memref < modified_memref_end; ++modified_memref) {
rc_preparse_sync_operand(&modified_memref->parent, parse, memrefs);
rc_preparse_sync_operand(&modified_memref->modifier, parse, memrefs);
if (parse->offset < 0)
return;

rc_alloc_modified_memref(parse, modified_memref->memref.value.size,
&modified_memref->parent, modified_memref->modifier_type, &modified_memref->modifier);
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/rc_condition.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ static void rc_condition_convert_to_operand(const rc_condition_t* condition, rc_

rc_condition_t* rc_parse_condition(const char** memaddr, rc_parse_state_t* parse) {
rc_condition_t * self = RC_ALLOC(rc_condition_t, parse);
if (!self)
return NULL;

rc_parse_condition_internal(self, memaddr, parse);
return (parse->offset < 0) ? NULL : self;
}
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/rc_condset.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ rc_condset_t* rc_parse_condset(const char** memaddr, rc_parse_state_t* parse) {
if (**memaddr == 'S' || **memaddr == 's' || !**memaddr) {
/* empty group - editor allows it, so we have to support it */
self = RC_ALLOC(rc_condset_t, parse);
memset(self, 0, sizeof(*self));
if (self)
memset(self, 0, sizeof(*self));
return self;
}

Expand Down
9 changes: 7 additions & 2 deletions src/runtime/rc_lboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ void rc_parse_lboard_internal(rc_lboard_t* self, const char* memaddr, rc_parse_s
found |= RC_LBOARD_PROGRESS;

self->progress = RC_ALLOC(rc_value_t, parse);
if (!self->progress)
return;

rc_parse_value_internal(self->progress, &memaddr, parse);
}
}
Expand Down Expand Up @@ -149,7 +152,8 @@ int rc_lboard_size(const char* memaddr) {
rc_init_preparse_state(&preparse);

lboard = RC_ALLOC(rc_lboard_with_memrefs_t, &preparse.parse);
rc_parse_lboard_internal(&lboard->lboard, memaddr, &preparse.parse);
if (lboard)
rc_parse_lboard_internal(&lboard->lboard, memaddr, &preparse.parse);
rc_preparse_alloc_memrefs(NULL, &preparse);

rc_destroy_preparse_state(&preparse);
Expand All @@ -168,7 +172,8 @@ rc_lboard_t* rc_parse_lboard(void* buffer, const char* memaddr, void* unused_L,

rc_init_preparse_state(&preparse);
lboard = RC_ALLOC(rc_lboard_with_memrefs_t, &preparse.parse);
rc_parse_lboard_internal(&lboard->lboard, memaddr, &preparse.parse);
if (lboard)
rc_parse_lboard_internal(&lboard->lboard, memaddr, &preparse.parse);
rc_preparse_alloc_memrefs(NULL, &preparse); /* allocate space for the needed memrefs */

rc_reset_parse_state(&preparse.parse, buffer, (size_t)preparse.parse.offset);
Expand Down
22 changes: 20 additions & 2 deletions src/runtime/rc_memref.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,20 @@ rc_memref_t* rc_alloc_memref(rc_parse_state_t* parse, uint32_t address, uint8_t
const int32_t old_offset = parse->offset;

if (memref_list->capacity != 0) {
memref_list = memref_list->next = RC_ALLOC_SCRATCH(rc_memref_list_t, parse);
rc_memref_list_t* new_memref_list = RC_ALLOC_SCRATCH(rc_memref_list_t, parse);
if (!new_memref_list)
return NULL;

memref_list = memref_list->next = new_memref_list;
memref_list->next = NULL;
}

memref_list->items = RC_ALLOC_ARRAY_SCRATCH(rc_memref_t, 8, parse);
if (!memref_list->items) {
memref_list->count = memref_list->capacity = 0;
return NULL;
}

memref_list->count = 1;
memref_list->capacity = 8;
memref_list->allocated = 0;
Expand Down Expand Up @@ -144,11 +153,20 @@ rc_modified_memref_t* rc_alloc_modified_memref(rc_parse_state_t* parse, uint8_t
const int32_t old_offset = parse->offset;

if (modified_memref_list->capacity != 0) {
modified_memref_list = modified_memref_list->next = RC_ALLOC_SCRATCH(rc_modified_memref_list_t, parse);
rc_modified_memref_list_t* new_modified_memref_list = RC_ALLOC_SCRATCH(rc_modified_memref_list_t, parse);
if (!new_modified_memref_list)
return NULL;

modified_memref_list = modified_memref_list->next = new_modified_memref_list;
modified_memref_list->next = NULL;
}

modified_memref_list->items = RC_ALLOC_ARRAY_SCRATCH(rc_modified_memref_t, 8, parse);
if (!modified_memref_list->items) {
modified_memref_list->count = modified_memref_list->capacity = 0;
return NULL;
}

modified_memref_list->count = 1;
modified_memref_list->capacity = 8;
modified_memref_list->allocated = 0;
Expand Down
44 changes: 38 additions & 6 deletions src/runtime/rc_richpresence.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ static void rc_alloc_helper_variable_memref_value(rc_richpresence_display_part_t
rc_init_preparse_state(&preparse);
preparse.parse.existing_memrefs = parse->memrefs;
value = RC_ALLOC(rc_value_t, &preparse.parse);
rc_parse_value_internal(value, &test_memaddr, &preparse.parse);
if (value)
rc_parse_value_internal(value, &test_memaddr, &preparse.parse);

size = preparse.parse.offset;
if (size < 0) {
Expand All @@ -60,7 +61,16 @@ static void rc_alloc_helper_variable_memref_value(rc_richpresence_display_part_t
}

/* parse the value into the scratch buffer so we can look at it */
rc_reset_parse_state(&preparse.parse, rc_buffer_alloc(&preparse.parse.scratch_buffer, (size_t)size), (size_t)size);
{
void* buffer = rc_buffer_alloc(&preparse.parse.scratch_buffer, (size_t)size);
if (!buffer) {
parse->offset = RC_OUT_OF_MEMORY;
rc_destroy_preparse_state(&preparse);
return;
}

rc_reset_parse_state(&preparse.parse, buffer, (size_t)size);
}
preparse.parse.memrefs = parse->memrefs;
preparse.parse.existing_memrefs = parse->existing_memrefs;
value = RC_ALLOC(rc_value_t, &preparse.parse);
Expand Down Expand Up @@ -157,6 +167,9 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c

{
self = RC_ALLOC(rc_richpresence_display_t, parse);
if (!self)
return NULL;

memset(self, 0, sizeof(rc_richpresence_display_t));
next = &self->display;
}
Expand All @@ -173,6 +186,9 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c

if (ptr > line) {
part = RC_ALLOC(rc_richpresence_display_part_t, parse);
if (!part)
return 0;

memset(part, 0, sizeof(rc_richpresence_display_part_t));
*next = part;
next = &part->next;
Expand Down Expand Up @@ -214,6 +230,9 @@ static rc_richpresence_display_t* rc_parse_richpresence_display_internal(const c
macro_len = ptr - line;

part = RC_ALLOC(rc_richpresence_display_part_t, parse);
if (!part)
return 0;

memset(part, 0, sizeof(rc_richpresence_display_part_t));
*next = part;
next = &part->next;
Expand Down Expand Up @@ -401,6 +420,9 @@ static void rc_insert_richpresence_lookup_item(rc_richpresence_lookup_t* lookup,
}

item = RC_ALLOC_SCRATCH(rc_richpresence_lookup_item_t, parse);
if (!item)
return;

item->first = first;
item->last = last;
item->label = rc_alloc_str(parse, label, label_len);
Expand Down Expand Up @@ -546,6 +568,9 @@ void rc_parse_richpresence_internal(rc_richpresence_t* self, const char* script,
line += 7;

lookup = RC_ALLOC_SCRATCH(rc_richpresence_lookup_t, parse);
if (!lookup)
return;

lookup->name = rc_alloc_str(parse, line, (int)(endline - line));
lookup->format = RC_FORMAT_RP_LOOKUP;
lookup->root = NULL;
Expand All @@ -568,6 +593,9 @@ void rc_parse_richpresence_internal(rc_richpresence_t* self, const char* script,
}

lookup = RC_ALLOC_SCRATCH(rc_richpresence_lookup_t, parse);
if (!lookup)
return;

lookup->name = rc_alloc_str(parse, line, (int)(endline - line));
lookup->root = NULL;
lookup->default_label = "";
Expand Down Expand Up @@ -685,8 +713,10 @@ int rc_richpresence_size_lines(const char* script, int* lines_read) {
rc_init_preparse_state(&preparse);

richpresence = RC_ALLOC(rc_richpresence_with_memrefs_t, &preparse.parse);
preparse.parse.variables = &richpresence->richpresence.values;
rc_parse_richpresence_internal(&richpresence->richpresence, script, &preparse.parse);
if (richpresence) {
preparse.parse.variables = &richpresence->richpresence.values;
rc_parse_richpresence_internal(&richpresence->richpresence, script, &preparse.parse);
}
rc_preparse_alloc_memrefs(NULL, &preparse);

if (lines_read)
Expand All @@ -712,8 +742,10 @@ rc_richpresence_t* rc_parse_richpresence(void* buffer, const char* script, void*

rc_init_preparse_state(&preparse);
richpresence = RC_ALLOC(rc_richpresence_with_memrefs_t, &preparse.parse);
preparse.parse.variables = &richpresence->richpresence.values;
rc_parse_richpresence_internal(&richpresence->richpresence, script, &preparse.parse);
if (richpresence) {
preparse.parse.variables = &richpresence->richpresence.values;
rc_parse_richpresence_internal(&richpresence->richpresence, script, &preparse.parse);
}
rc_preparse_alloc_memrefs(NULL, &preparse); /* allocate space for the needed memrefs */

rc_reset_parse_state(&preparse.parse, buffer, (size_t)preparse.parse.offset);
Expand Down
Loading
Loading