Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/audio/mux/mux.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,20 +209,18 @@ static void mux_free(struct comp_dev *dev)
rfree(dev);
}

static uint8_t get_stream_index(struct comp_data *cd, uint32_t pipe_id)
static int get_stream_index(struct comp_data *cd, uint32_t pipe_id)
{
int i;
int idx;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use idx instead of i.

for (i = 0; i < MUX_MAX_STREAMS; i++) {
comp_cl_err(&comp_mux, "got %u", cd->config.streams[i].pipeline_id);
if (cd->config.streams[i].pipeline_id == pipe_id)
return i;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keyonjie the change looks good. But I wonder if the return 0 at the of the function is correct? If a match is not found, we should return a negative value isn't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keyonjie the change looks good. But I wonder if the return 0 at the of the function is correct? If a match is not found, we should return a negative value isn't it?

That's a good point. I think the intention of the original author just wanted to return and use the first index (0) if the matched one not found, please check in the caller side, they don't check the return value and just use it as index blindly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is unsafe as it's used for an array lookup and may assign a source/sink for usage that is not in the correct state (and may crash etc). Can we check the result and return an error here (and handle it in the caller.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is unsafe as it's used for an array lookup and may assign a source/sink for usage that is not in the correct state (and may crash etc). Can we check the result and return an error here (and handle it in the caller.)

Sure, let me add a commit to address this.

for (idx = 0; idx < MUX_MAX_STREAMS; idx++)
if (cd->config.streams[idx].pipeline_id == pipe_id)
return idx;

comp_cl_err(&comp_mux, "get_stream_index(): couldn't find configuration for connected pipeline %u",
pipe_id);

return 0;
return -EINVAL;
}

static struct mux_look_up *get_lookup_table(struct comp_data *cd,
Expand Down Expand Up @@ -423,12 +421,12 @@ static int demux_copy(struct comp_dev *dev)
struct mux_look_up *look_up;
struct list_item *clist;
uint32_t num_sinks = 0;
uint32_t i = 0;
uint32_t frames = -1;
uint32_t source_bytes;
uint32_t avail;
uint32_t sinks_bytes[MUX_MAX_STREAMS] = { 0 };
uint32_t flags = 0;
int i;

comp_dbg(dev, "demux_copy()");

Expand All @@ -445,6 +443,9 @@ static int demux_copy(struct comp_dev *dev)
if (sink->sink->state == dev->state) {
num_sinks++;
i = get_stream_index(cd, sink->pipeline_id);
/* return if index wrong */
if (i < 0)
return i;
look_up = get_lookup_table(cd, sink->pipeline_id);
sinks[i] = sink;
look_ups[i] = look_up;
Expand Down Expand Up @@ -521,11 +522,11 @@ static int mux_copy(struct comp_dev *dev)
const struct audio_stream *sources_stream[MUX_MAX_STREAMS] = { NULL };
struct list_item *clist;
uint32_t num_sources = 0;
uint32_t i = 0;
uint32_t frames = -1;
uint32_t sources_bytes[MUX_MAX_STREAMS] = { 0 };
uint32_t sink_bytes;
uint32_t flags = 0;
int i;

comp_dbg(dev, "mux_copy()");

Expand All @@ -542,6 +543,9 @@ static int mux_copy(struct comp_dev *dev)
if (source->source->state == dev->state) {
num_sources++;
i = get_stream_index(cd, source->pipeline_id);
/* return if index wrong */
if (i < 0)
return i;
sources[i] = source;
sources_stream[i] = &source->stream;
} else {
Expand Down