Skip to content

Commit

Permalink
context: Don't create channels mask when IIO device has 0 channels
Browse files Browse the repository at this point in the history
When a IIO device has 0 channels, there's no point in creating a
channels mask.

This fixes an undefined behaviour, as the call to
iio_create_channels_mask() could result in calling malloc(0).

To prevent this from happening again, iio_create_channels_mask() will
now check the validity of its parameter.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Aug 23, 2022
1 parent 3e7d3b4 commit 9958d44
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 5 additions & 3 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,11 @@ int iio_context_init(struct iio_context *ctx)
int ret;

for (i = 0; i < ctx->nb_devices; i++) {
ret = iio_device_alloc_mask(ctx->devices[i]);
if (ret)
return ret;
if (ctx->devices[i]->nb_channels) {
ret = iio_device_alloc_mask(ctx->devices[i]);
if (ret)
return ret;
}
}

for (i = 0; i < ctx->nb_devices; i++)
Expand Down
3 changes: 3 additions & 0 deletions mask.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ struct iio_channels_mask * iio_create_channels_mask(unsigned int nb_channels)
struct iio_channels_mask *mask;
size_t nb_words = (nb_channels + 31) / 32;

if (!nb_words)
return NULL;

mask = zalloc(sizeof(*mask) + nb_words * sizeof(uint32_t));
if (mask)
mask->words = nb_words;
Expand Down

0 comments on commit 9958d44

Please sign in to comment.