Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
52 changes: 48 additions & 4 deletions src/runtime/rc_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ void* rc_alloc_scratch(void* pointer, int32_t* offset, uint32_t size, uint32_t a
{
void* data;

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

/* if we have a real buffer, then allocate the data there */
if (pointer)
return rc_alloc(pointer, offset, size, alignment, NULL, scratch_object_pointer_offset);
Expand All @@ -31,6 +35,10 @@ void* rc_alloc_scratch(void* pointer, int32_t* offset, uint32_t size, uint32_t a
void* rc_alloc(void* pointer, int32_t* offset, uint32_t size, uint32_t alignment, rc_scratch_t* scratch, uint32_t scratch_object_pointer_offset) {
void* ptr;

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

*offset = (*offset + alignment - 1) & ~(alignment - 1);

if (pointer != 0) {
Expand All @@ -42,8 +50,12 @@ void* rc_alloc(void* pointer, int32_t* offset, uint32_t size, uint32_t alignment
void** scratch_object_pointer = (void**)((uint8_t*)&scratch->objs + scratch_object_pointer_offset);
ptr = *scratch_object_pointer;
if (!ptr) {
int32_t used;
int32_t used = 0;
ptr = *scratch_object_pointer = rc_alloc_scratch(NULL, &used, size, alignment, scratch, (uint32_t)-1);
if (!ptr) {
*offset = RC_OUT_OF_MEMORY;
return NULL;
}
}
}
else {
Expand Down Expand Up @@ -81,6 +93,8 @@ char* rc_alloc_str(rc_parse_state_t* parse, const char* text, size_t length) {
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 @@ -174,12 +188,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 @@ -196,12 +223,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 @@ -261,8 +300,11 @@ void rc_preparse_copy_memrefs(rc_parse_state_t* parse, rc_memrefs_t* memrefs)
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 @@ -272,6 +314,8 @@ void rc_preparse_copy_memrefs(rc_parse_state_t* parse, rc_memrefs_t* memrefs)
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 @@ -190,6 +190,9 @@ void rc_condition_convert_to_operand(const rc_condition_t* condition, rc_operand

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 @@ -222,7 +222,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
3 changes: 3 additions & 0 deletions src/runtime/rc_lboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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
22 changes: 20 additions & 2 deletions src/runtime/rc_memref.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,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; /* parse->offset is RC_OUT_OF_MEMORY */

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; /* parse->offset is RC_OUT_OF_MEMORY */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be in the if, not the return:

   if (!memref_list->items) { /* parse->offset is RC_OUT_OF_MEMORY */
      memref_list->count = memref_list->capacity = 0;
      return NULL;
   }

But I'd lean towards not having the comment at all. It might also be RC_INSUFFICIENT_BUFFER

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment applies to several other locations in this PR.

}

memref_list->count = 1;
memref_list->capacity = 8;
memref_list->allocated = 0;
Expand Down Expand Up @@ -137,11 +146,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; /* parse->offset is RC_OUT_OF_MEMORY */

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; /* parse->offset is RC_OUT_OF_MEMORY */
}

modified_memref_list->count = 1;
modified_memref_list->capacity = 8;
modified_memref_list->allocated = 0;
Expand Down
29 changes: 28 additions & 1 deletion src/runtime/rc_richpresence.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,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));
{
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);
}
preparse.parse.memrefs = parse->memrefs;
preparse.parse.existing_memrefs = parse->existing_memrefs;
value = RC_ALLOC(rc_value_t, &preparse.parse);
Expand Down Expand Up @@ -141,6 +150,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 @@ -157,6 +169,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 @@ -198,6 +213,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 @@ -385,6 +403,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 @@ -530,6 +551,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_LOOKUP;
lookup->root = NULL;
Expand All @@ -552,6 +576,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
Loading