Skip to content

[CRITICAL BUG] dynarray_get() returns pointer address as size_t #3

Description

@seesee010

Bug Description

dynarray_get() returns the memory address as a size_t instead of returning a pointer to the actual data.

Location

src/access.c lines 11-20

Current Code

size_t dynarray_get(DynArray *array, size_t index) {
    if (array == NULL) {
        return -1;
    }

    void *ptr = (array->data + index * array->element_size);  // Wrong arithmetic
    return (size_t)ptr;  // ← Returns ADDRESS, not data!
}

Problem

  1. Returns pointer address as size_t (e.g., 0x7ffd5a3c1040)
  2. User cannot retrieve actual value
  3. Pointer arithmetic is wrong (void* should be cast to char* first)
  4. Return type should be void*, not size_t

Expected Behavior

Should return a void* pointer that user can cast to their type.

Proposed Fix

void *dynarray_get(DynArray *array, size_t index) {
    if (array == NULL || index >= array->length) {
        return NULL;
    }
    
    return (char*)array->data + index * array->element_size;
}

Impact

  • Severity: Critical
  • Impossible to retrieve values from the array
  • Makes the entire library unusable for reading data

Usage Example

// After fix:
DynArray *arr = dynarray_create(sizeof(int), 10);
int val = 42;
dynarray_set(arr, 0, &val);

int *ptr = (int*)dynarray_get(arr, 0);
printf("Value: %d\n", *ptr);  // Should print 42

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions