Skip to content

Commit

Permalink
lib: check null pointer before taking lock for bitarray
Browse files Browse the repository at this point in the history
In some function in bitarray.c, we should check the pointer
of bitarry before taking bitarray lock.

In this patch, we move the null-check assert before the use of
the struct to provide better validation than a crash.

Signed-off-by: TaiJu Wu <[email protected]>
  • Loading branch information
TaiJuWu authored and aescolar committed Jun 14, 2024
1 parent bfba19d commit a2e9740
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/utils/bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,11 @@ int sys_bitarray_popcount_region(sys_bitarray_t *bitarray, size_t num_bits, size
struct bundle_data bd;
int ret;

key = k_spin_lock(&bitarray->lock);

__ASSERT_NO_MSG(bitarray != NULL);
__ASSERT_NO_MSG(bitarray->num_bits > 0);

key = k_spin_lock(&bitarray->lock);

if (num_bits == 0 || offset + num_bits > bitarray->num_bits) {
ret = -EINVAL;
goto out;
Expand Down Expand Up @@ -266,14 +266,15 @@ int sys_bitarray_xor(sys_bitarray_t *dst, sys_bitarray_t *other, size_t num_bits
size_t idx;
struct bundle_data bd;

key_dst = k_spin_lock(&dst->lock);
key_other = k_spin_lock(&other->lock);

__ASSERT_NO_MSG(dst != NULL);
__ASSERT_NO_MSG(dst->num_bits > 0);
__ASSERT_NO_MSG(other != NULL);
__ASSERT_NO_MSG(other->num_bits > 0);

key_dst = k_spin_lock(&dst->lock);
key_other = k_spin_lock(&other->lock);


if (dst->num_bits != other->num_bits) {
ret = -EINVAL;
goto out;
Expand Down Expand Up @@ -322,11 +323,11 @@ int sys_bitarray_set_bit(sys_bitarray_t *bitarray, size_t bit)
int ret;
size_t idx, off;

key = k_spin_lock(&bitarray->lock);

__ASSERT_NO_MSG(bitarray != NULL);
__ASSERT_NO_MSG(bitarray->num_bits > 0);

key = k_spin_lock(&bitarray->lock);

if (bit >= bitarray->num_bits) {
ret = -EINVAL;
goto out;
Expand Down

0 comments on commit a2e9740

Please sign in to comment.