Skip to content

Commit

Permalink
Prevent index 0 in ecs_field functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroErrors authored and SanderMertens committed Jul 12, 2023
1 parent 70f70c0 commit 061a5e0
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 112 deletions.
85 changes: 29 additions & 56 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -59465,68 +59465,53 @@ bool flecs_iter_next_instanced(
void* ecs_field_w_size(
const ecs_iter_t *it,
size_t size,
int32_t term)
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);
ecs_check(it->ptrs != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(!size || ecs_field_size(it, term) == size ||
(!ecs_field_size(it, term) && (!it->ptrs[term - 1])),
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);

ecs_check(!size || ecs_field_size(it, index) == size ||
(!ecs_field_size(it, index) && (!it->ptrs[index - 1])),
ECS_INVALID_PARAMETER, NULL);
(void)size;

if (!term) {
return it->entities;
}

return it->ptrs[term - 1];
return it->ptrs[index - 1];
error:
return NULL;
}

bool ecs_field_is_readonly(
const ecs_iter_t *it,
int32_t term_index)
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);
ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
ecs_term_t *term = &it->terms[index - 1];

ecs_term_t *term = &it->terms[term_index - 1];
ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL);

if (term->inout == EcsIn) {
return true;
} else {
ecs_term_id_t *src = &term->src;

if (term->inout == EcsInOutDefault) {
if (!(ecs_term_match_this(term))) {
return true;
}
} else if (term->inout == EcsInOutDefault) {
if (!ecs_term_match_this(term)) {
return true;
}

if (!(src->flags & EcsSelf)) {
return true;
}
ecs_term_id_t *src = &term->src;
if (!(src->flags & EcsSelf)) {
return true;
}
}

error:
return false;
}

bool ecs_field_is_writeonly(
const ecs_iter_t *it,
int32_t term_index)
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);
ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL);

ecs_term_t *term = &it->terms[term_index - 1];
ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL);

if (term->inout == EcsOut) {
return true;
}

ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
ecs_term_t *term = &it->terms[index - 1];
return term->inout == EcsOut;
error:
return false;
}
Expand All @@ -59536,7 +59521,7 @@ bool ecs_field_is_set(
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);

ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
int32_t column = it->columns[index - 1];
if (!column) {
return false;
Expand All @@ -59549,7 +59534,6 @@ bool ecs_field_is_set(
return true;
}
}

return true;
error:
return false;
Expand All @@ -59559,41 +59543,36 @@ bool ecs_field_is_self(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
return it->sources == NULL || it->sources[index - 1] == 0;
}

ecs_id_t ecs_field_id(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
return it->ids[index - 1];
}

int32_t ecs_field_column_index(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);

ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
int32_t result = it->columns[index - 1];
if (result <= 0) {
return -1;
} else {
return result - 1;
}

return result - 1;
}

ecs_entity_t ecs_field_src(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
if (it->sources) {
return it->sources[index - 1];
} else {
Expand All @@ -59605,14 +59584,8 @@ size_t ecs_field_size(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);

if (index == 0) {
return sizeof(ecs_entity_t);
} else {
return (size_t)it->sizes[index - 1];
}
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
return (size_t)it->sizes[index - 1];
}

char* ecs_iter_str(
Expand Down
85 changes: 29 additions & 56 deletions src/iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,68 +348,53 @@ bool flecs_iter_next_instanced(
void* ecs_field_w_size(
const ecs_iter_t *it,
size_t size,
int32_t term)
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);
ecs_check(it->ptrs != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_check(!size || ecs_field_size(it, term) == size ||
(!ecs_field_size(it, term) && (!it->ptrs[term - 1])),
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);

ecs_check(!size || ecs_field_size(it, index) == size ||
(!ecs_field_size(it, index) && (!it->ptrs[index - 1])),
ECS_INVALID_PARAMETER, NULL);
(void)size;

if (!term) {
return it->entities;
}

return it->ptrs[term - 1];
return it->ptrs[index - 1];
error:
return NULL;
}

bool ecs_field_is_readonly(
const ecs_iter_t *it,
int32_t term_index)
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);
ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
ecs_term_t *term = &it->terms[index - 1];

ecs_term_t *term = &it->terms[term_index - 1];
ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL);

if (term->inout == EcsIn) {
return true;
} else {
ecs_term_id_t *src = &term->src;

if (term->inout == EcsInOutDefault) {
if (!(ecs_term_match_this(term))) {
return true;
}
} else if (term->inout == EcsInOutDefault) {
if (!ecs_term_match_this(term)) {
return true;
}

if (!(src->flags & EcsSelf)) {
return true;
}
ecs_term_id_t *src = &term->src;
if (!(src->flags & EcsSelf)) {
return true;
}
}

error:
return false;
}

bool ecs_field_is_writeonly(
const ecs_iter_t *it,
int32_t term_index)
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);
ecs_check(term_index > 0, ECS_INVALID_PARAMETER, NULL);

ecs_term_t *term = &it->terms[term_index - 1];
ecs_check(term != NULL, ECS_INVALID_PARAMETER, NULL);

if (term->inout == EcsOut) {
return true;
}

ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
ecs_term_t *term = &it->terms[index - 1];
return term->inout == EcsOut;
error:
return false;
}
Expand All @@ -419,7 +404,7 @@ bool ecs_field_is_set(
int32_t index)
{
ecs_check(it->flags & EcsIterIsValid, ECS_INVALID_PARAMETER, NULL);

ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
int32_t column = it->columns[index - 1];
if (!column) {
return false;
Expand All @@ -432,7 +417,6 @@ bool ecs_field_is_set(
return true;
}
}

return true;
error:
return false;
Expand All @@ -442,41 +426,36 @@ bool ecs_field_is_self(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
return it->sources == NULL || it->sources[index - 1] == 0;
}

ecs_id_t ecs_field_id(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
return it->ids[index - 1];
}

int32_t ecs_field_column_index(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);

ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
int32_t result = it->columns[index - 1];
if (result <= 0) {
return -1;
} else {
return result - 1;
}

return result - 1;
}

ecs_entity_t ecs_field_src(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
if (it->sources) {
return it->sources[index - 1];
} else {
Expand All @@ -488,14 +467,8 @@ size_t ecs_field_size(
const ecs_iter_t *it,
int32_t index)
{
ecs_assert(it != NULL, ECS_INVALID_PARAMETER, NULL);
ecs_assert(index >= 0, ECS_INVALID_PARAMETER, NULL);

if (index == 0) {
return sizeof(ecs_entity_t);
} else {
return (size_t)it->sizes[index - 1];
}
ecs_assert(index >= 1, ECS_INVALID_PARAMETER, NULL);
return (size_t)it->sizes[index - 1];
}

char* ecs_iter_str(
Expand Down

0 comments on commit 061a5e0

Please sign in to comment.