Skip to content

Result of zpl_alloc() is often not checked #104

@rheatley-pervasid

Description

@rheatley-pervasid

Sorry to bombard you with all the issues! It's honestly because I am enjoying using the library so much for all sorts of projects :)

The following code seg faults

char buffer[256];
zpl_arena arena;
zpl_arena_init_from_memory(&arena, buffer, sizeof(buffer));
zpl_allocator allocator = zpl_arena_allocator(&arena);
zpl_adt_node root;
zpl_adt_make_branch(&root, allocator, "#", 0);

This is because zpl_alloc fails in zpl_array_init_reserve (from zpl_array_init, from zpl_adt_make_branch)

#define zpl_array_init_reserve(x, allocator_, cap)                                                                     \
     do {                                                                                                               \
         void **zpl__array_ = cast(void **) & (x);                                                                      \
         zpl_array_header *zpl__ah =                                                                                    \
         cast(zpl_array_header *) zpl_alloc(allocator_, zpl_size_of(zpl_array_header) + zpl_size_of(*(x)) * (cap)); \
         zpl__ah->allocator = allocator_;                                                                               \
         zpl__ah->count = 0;                                                                                            \
         zpl__ah->data = (char *)x;                                                                                     \
         zpl__ah->capacity = cap;                                                                                       \
         *zpl__array_ = cast(void *)(zpl__ah + 1);                                                                      \
     } while (0)

As you can see zpl__ah is used without a null check.

There seem to be quite a few instances where the failure is not checked. I'm hoping you agree it should return an error rather than segmentation fault!

I'm happy to try and fix the related issues and submit a PR if you like.
Let me know your thoughts.

(In my use case I am on an embedded device so prefer static allocation, hence zpl_alloc can fail without the heap being exhausted)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions