axfer: coverity fixes

- container-voc.c - out of array access
- container-voc.c - handle correctly eof
- frame_cache.c - correct memory allocation
- container.c - byte_count might be used uninitialized
- xfer-libasound-irq-mmap.c - fix avail signess
- xfer-options.c - fix potential 32-bit wrap for duration

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2019-03-13 14:19:12 +01:00
parent bc42704c96
commit 819e04c7a1
5 changed files with 43 additions and 39 deletions

View file

@ -234,7 +234,7 @@ static int build_time_constant(unsigned int frames_per_second,
frames_per_second) frames_per_second)
break; break;
} }
if (i < ARRAY_SIZE(ex_v110_time_consts) || if (i < ARRAY_SIZE(ex_v110_time_consts) &&
frames_per_second <= 192000) { frames_per_second <= 192000) {
*code = ex_v110_time_consts[i].code; *code = ex_v110_time_consts[i].code;
} else { } else {
@ -520,14 +520,15 @@ static int detect_format_block(struct container_context *cntr)
{ {
struct parser_state *state = cntr->private_data; struct parser_state *state = cntr->private_data;
struct block_header header; struct block_header header;
void *buf = NULL; void *buf;
int err; int err;
again: again:
buf = NULL;
err = cache_data_block(cntr, &header, &buf); err = cache_data_block(cntr, &header, &buf);
if (err < 0) if (err < 0)
return err; return err;
if (buf) {
if (header.type == BLOCK_TYPE_EXTENDED_V110_FORMAT) { if (header.type == BLOCK_TYPE_EXTENDED_V110_FORMAT) {
err = parse_extended_v110_format(state, buf); err = parse_extended_v110_format(state, buf);
} else if (header.type == BLOCK_TYPE_V120_DATA) { } else if (header.type == BLOCK_TYPE_V120_DATA) {
@ -543,6 +544,7 @@ again:
if (err < 0) if (err < 0)
return err; return err;
}
// Expect to detect block_v110_data. // Expect to detect block_v110_data.
if (header.type == BLOCK_TYPE_EXTENDED_V110_FORMAT) if (header.type == BLOCK_TYPE_EXTENDED_V110_FORMAT)

View file

@ -296,7 +296,7 @@ int container_context_pre_process(struct container_context *cntr,
unsigned int *frames_per_second, unsigned int *frames_per_second,
uint64_t *frame_count) uint64_t *frame_count)
{ {
uint64_t byte_count; uint64_t byte_count = 0;
unsigned int bytes_per_frame; unsigned int bytes_per_frame;
int err; int err;

View file

@ -50,13 +50,18 @@ int frame_cache_init(struct frame_cache *cache, snd_pcm_access_t access,
unsigned int samples_per_frame, unsigned int samples_per_frame,
unsigned int frames_per_cache) unsigned int frames_per_cache)
{ {
cache->access = access;
cache->remained_count = 0;
cache->bytes_per_sample = bytes_per_sample;
cache->samples_per_frame = samples_per_frame;
cache->frames_per_cache = frames_per_cache;
if (access == SND_PCM_ACCESS_RW_INTERLEAVED) if (access == SND_PCM_ACCESS_RW_INTERLEAVED)
cache->align_frames = align_frames_in_i; cache->align_frames = align_frames_in_i;
else if (access == SND_PCM_ACCESS_RW_NONINTERLEAVED) else if (access == SND_PCM_ACCESS_RW_NONINTERLEAVED)
cache->align_frames = align_frames_in_n; cache->align_frames = align_frames_in_n;
else else
return -EINVAL; return -EINVAL;
cache->access = access;
if (access == SND_PCM_ACCESS_RW_INTERLEAVED) { if (access == SND_PCM_ACCESS_RW_INTERLEAVED) {
char *buf; char *buf;
@ -64,44 +69,41 @@ int frame_cache_init(struct frame_cache *cache, snd_pcm_access_t access,
buf = calloc(frames_per_cache, buf = calloc(frames_per_cache,
bytes_per_sample * samples_per_frame); bytes_per_sample * samples_per_frame);
if (buf == NULL) if (buf == NULL)
return -ENOMEM; goto nomem;
cache->buf = buf; cache->buf = buf;
cache->buf_ptr = buf; cache->buf_ptr = buf;
} else { } else {
char **bufs; char **bufs = calloc(samples_per_frame, sizeof(*bufs));
char **buf_ptrs; char **buf_ptrs = calloc(samples_per_frame, sizeof(*buf_ptrs));
int i; int i;
bufs = calloc(samples_per_frame, sizeof(*bufs)); cache->buf = bufs;
if (bufs == NULL) cache->buf_ptr = buf_ptrs;
return -ENOMEM; if (bufs == NULL || buf_ptrs == NULL)
buf_ptrs = calloc(samples_per_frame, sizeof(*buf_ptrs)); goto nomem;
if (buf_ptrs == NULL)
return -ENOMEM;
for (i = 0; i < samples_per_frame; ++i) { for (i = 0; i < samples_per_frame; ++i) {
bufs[i] = calloc(frames_per_cache, bytes_per_sample); bufs[i] = calloc(frames_per_cache, bytes_per_sample);
if (bufs[i] == NULL) if (bufs[i] == NULL)
return -ENOMEM; goto nomem;
buf_ptrs[i] = bufs[i]; buf_ptrs[i] = bufs[i];
} }
cache->buf = bufs;
cache->buf_ptr = buf_ptrs;
} }
cache->remained_count = 0;
cache->bytes_per_sample = bytes_per_sample;
cache->samples_per_frame = samples_per_frame;
cache->frames_per_cache = frames_per_cache;
return 0; return 0;
nomem:
frame_cache_destroy(cache);
return -ENOMEM;
} }
void frame_cache_destroy(struct frame_cache *cache) void frame_cache_destroy(struct frame_cache *cache)
{ {
if (cache->access == SND_PCM_ACCESS_RW_NONINTERLEAVED) { if (cache->access == SND_PCM_ACCESS_RW_NONINTERLEAVED) {
int i;
for (i = 0; i < cache->samples_per_frame; ++i) {
char **bufs = cache->buf; char **bufs = cache->buf;
if (bufs) {
int i;
for (i = 0; i < cache->samples_per_frame; ++i)
free(bufs[i]); free(bufs[i]);
} }
free(cache->buf_ptr); free(cache->buf_ptr);

View file

@ -75,7 +75,7 @@ static int irq_mmap_process_frames(struct libasound_state *state,
struct map_layout *layout = state->private_data; struct map_layout *layout = state->private_data;
const snd_pcm_channel_area_t *areas; const snd_pcm_channel_area_t *areas;
snd_pcm_uframes_t frame_offset; snd_pcm_uframes_t frame_offset;
snd_pcm_uframes_t avail; snd_pcm_sframes_t avail;
unsigned int avail_count; unsigned int avail_count;
void *frame_buf; void *frame_buf;
snd_pcm_sframes_t consumed_count; snd_pcm_sframes_t consumed_count;

View file

@ -395,7 +395,7 @@ void xfer_options_calculate_duration(struct xfer_context *xfer,
uint64_t frame_count; uint64_t frame_count;
if (xfer->duration_seconds > 0) { if (xfer->duration_seconds > 0) {
frame_count = xfer->duration_seconds * xfer->frames_per_second; frame_count = (uint64_t)xfer->duration_seconds * (uint64_t)xfer->frames_per_second;
if (frame_count < *total_frame_count) if (frame_count < *total_frame_count)
*total_frame_count = frame_count; *total_frame_count = frame_count;
} }