Skip to content

Fix BUFcreate #4

Open
Open
@ravi688

Description

@ravi688

function_signature(BUFFER*, BUFcreate, BUFFER* buffer, buf_ucount_t element_size, buf_ucount_t capacity, buf_ucount_t offset)
{
CALLTRACE_BEGIN();
GOOD_ASSERT(((int64_t)element_size) > 0, "element_size cannot be negative or zero");
GOOD_ASSERT(((int64_t)capacity) >= 0, "capacity cannot be negative");
GOOD_ASSERT(((int64_t)offset) >= 0, "offset cannot be negative");
if(buffer == NULL)
{
buffer = malloc(sizeof(BUFFER));
GOOD_ASSERT(buffer != NULL, "Memory Allocation Failure Exception");
buffer->info = 0x00;
buffer->info |= HEAP_ALLOCATED_OBJECT;
buffer->auto_managed_empty_blocks = NULL;
buffer->is_auto_managed = false;
buffer->on_pre_resize = NULL;
buffer->on_post_resize = NULL;
buffer->free = NULL;
}
buffer->bytes = (void*)malloc(element_size * capacity + offset);
GOOD_ASSERT(buffer->bytes != NULL, "Memory Allocation Failure Exception");
buffer->element_size = element_size;
buffer->capacity = capacity;
buffer->element_count = 0;
buffer->offset = offset;
CALLTRACE_RETURN(buffer);
}

if we pass capacity = 0 and offset = 0 then it shouldn't allocate any kind of heap memory given that the BUFFER object is also allocated on the stack.
But here it is not check if the total size of the buffer is zero or not, if it is zero then the buffer->bytes should remain NULL.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions