From mboxrd@z Thu Jan 1 00:00:00 1970 From: Takashi Iwai Subject: Removal of assert() in alsa-lib Date: Wed, 01 Dec 2004 17:31:15 +0100 Message-ID: Mime-Version: 1.0 (generated by SEMI 1.14.5 - "Awara-Onsen") Content-Type: multipart/mixed; boundary="Multipart_Wed_Dec__1_17:31:15_2004-1" Return-path: Sender: alsa-devel-admin@lists.sourceforge.net Errors-To: alsa-devel-admin@lists.sourceforge.net List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , List-Archive: To: alsa-devel@lists.sourceforge.net List-Id: alsa-devel@alsa-project.org --Multipart_Wed_Dec__1_17:31:15_2004-1 Content-Type: text/plain; charset=US-ASCII Hi, the current alsa-lib codes include many assert() in them. In some cases, alsa-lib uses assert() for the internal sanity check, and this results in the forced exit of application e.g. when a significant drift happens between linked PCM streams. I believe this should be avoided. Instead, alsa-lib should return the error in such a case. We did have a discussion about this quite ago, but no changes had been done at that time. So, let's whip again. The patch attached below is a part of changes to replace the excessive assert() with if & error-return. It's for some PCM codes only. Any comments are appreciated. The one regression by this is that the check is done unconditionally while assert() is compiled only when the debug option is enabled. We may introduce a macro if this really matters (although I don't think so). Takashi --Multipart_Wed_Dec__1_17:31:15_2004-1 Content-Type: text/plain; charset=US-ASCII Index: alsa-lib/src/pcm/pcm.c =================================================================== RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm.c,v retrieving revision 1.161 diff -u -r1.161 pcm.c --- alsa-lib/src/pcm/pcm.c 15 Nov 2004 12:06:35 -0000 1.161 +++ alsa-lib/src/pcm/pcm.c 29 Nov 2004 17:54:39 -0000 @@ -795,7 +795,8 @@ int snd_pcm_hw_free(snd_pcm_t *pcm) { int err; - assert(pcm->setup); + if (! pcm->setup) + return 0; if (pcm->mmap_channels) { err = snd_pcm_munmap(pcm); if (err < 0) @@ -818,17 +819,21 @@ int snd_pcm_sw_params(snd_pcm_t *pcm, snd_pcm_sw_params_t *params) { int err; - assert(pcm->setup); /* the hw_params must be set at first!!! */ + /* the hw_params must be set at first!!! */ + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } if (! params->avail_min || ! params->xfer_align) return -EINVAL; if (params->start_threshold <= pcm->buffer_size && params->start_threshold > (pcm->buffer_size / params->avail_min) * params->avail_min) { - SNDERR("snd_pcm_sw_params: params->avail_min problem for start_threshold"); + SNDERR("params->avail_min problem for start_threshold"); return -EINVAL; } if (params->start_threshold <= pcm->buffer_size && params->start_threshold > (pcm->buffer_size / params->xfer_align) * params->xfer_align) { - SNDERR("snd_pcm_sw_params: params->xfer_align problem for start_threshold"); + SNDERR("params->xfer_align problem for start_threshold"); return -EINVAL; } err = pcm->ops->sw_params(pcm->op_arg, params); @@ -885,7 +890,10 @@ int snd_pcm_hwsync(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->hwsync(pcm->fast_op_arg); } @@ -908,7 +916,10 @@ int snd_pcm_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->delay(pcm->fast_op_arg, delayp); } @@ -927,7 +938,10 @@ int snd_pcm_resume(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->resume(pcm->fast_op_arg); } @@ -939,7 +953,10 @@ int snd_pcm_prepare(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->prepare(pcm->fast_op_arg); } @@ -953,7 +970,10 @@ int snd_pcm_reset(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->reset(pcm->fast_op_arg); } @@ -965,7 +985,10 @@ int snd_pcm_start(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->start(pcm->fast_op_arg); } @@ -983,7 +1006,10 @@ int snd_pcm_drop(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->drop(pcm->fast_op_arg); } @@ -1003,7 +1029,10 @@ int snd_pcm_drain(snd_pcm_t *pcm) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->drain(pcm->fast_op_arg); } @@ -1020,7 +1049,10 @@ int snd_pcm_pause(snd_pcm_t *pcm, int enable) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return pcm->fast_ops->pause(pcm->fast_op_arg, enable); } @@ -1034,8 +1066,12 @@ snd_pcm_sframes_t snd_pcm_rewind(snd_pcm_t *pcm, snd_pcm_uframes_t frames) { assert(pcm); - assert(pcm->setup); - assert(frames > 0); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (frames == 0) + return 0; return pcm->fast_ops->rewind(pcm->fast_op_arg, frames); } @@ -1053,8 +1089,12 @@ #endif { assert(pcm); - assert(pcm->setup); - assert(frames > 0); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (frames == 0) + return 0; return pcm->fast_ops->forward(pcm->fast_op_arg, frames); } use_default_symbol_version(__snd_pcm_forward, snd_pcm_forward, ALSA_0.9.0rc8); @@ -1080,8 +1120,14 @@ { assert(pcm); assert(size == 0 || buffer); - assert(pcm->setup); - assert(pcm->access == SND_PCM_ACCESS_RW_INTERLEAVED); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) { + SNDERR("invalid access type"); + return -EINVAL; + } return _snd_pcm_writei(pcm, buffer, size); } @@ -1106,8 +1152,14 @@ { assert(pcm); assert(size == 0 || bufs); - assert(pcm->setup); - assert(pcm->access == SND_PCM_ACCESS_RW_NONINTERLEAVED); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) { + SNDERR("invalid access type"); + return -EINVAL; + } return _snd_pcm_writen(pcm, bufs, size); } @@ -1132,8 +1184,14 @@ { assert(pcm); assert(size == 0 || buffer); - assert(pcm->setup); - assert(pcm->access == SND_PCM_ACCESS_RW_INTERLEAVED); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) { + SNDERR("invalid access type"); + return -EINVAL; + } return _snd_pcm_readi(pcm, buffer, size); } @@ -1158,8 +1216,14 @@ { assert(pcm); assert(size == 0 || bufs); - assert(pcm->setup); - assert(pcm->access == SND_PCM_ACCESS_RW_NONINTERLEAVED); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) { + SNDERR("invalid access type"); + return -EINVAL; + } return _snd_pcm_readn(pcm, bufs, size); } @@ -1246,7 +1310,10 @@ if (err < 0) return err; } - assert(pcm->poll_fd >= 0); + if (! pcm->poll_fd < 0) { + SNDERR("poll_fd < 0"); + return -EIO; + } if (space >= 1 && pfds) { pfds->fd = pcm->poll_fd; pfds->events = pcm->poll_events | POLLERR | POLLNVAL; @@ -1468,7 +1535,8 @@ */ const char *snd_pcm_stream_name(snd_pcm_stream_t stream) { - assert(stream <= SND_PCM_STREAM_LAST); + if (stream > SND_PCM_STREAM_LAST) + return NULL; return snd_pcm_stream_names[stream]; } @@ -1562,7 +1630,8 @@ */ const char *snd_pcm_start_mode_name(snd_pcm_start_t mode) { - assert(mode <= SND_PCM_START_LAST); + if (mode > SND_PCM_START_LAST) + return NULL; return snd_pcm_start_mode_names[mode]; } @@ -1577,7 +1646,8 @@ */ const char *snd_pcm_xrun_mode_name(snd_pcm_xrun_t mode) { - assert(mode <= SND_PCM_XRUN_LAST); + if (mode > SND_PCM_XRUN_LAST) + return NULL; return snd_pcm_xrun_mode_names[mode]; } @@ -1636,7 +1706,10 @@ { assert(pcm); assert(out); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } snd_output_printf(out, "stream : %s\n", snd_pcm_stream_name(pcm->stream)); snd_output_printf(out, "access : %s\n", snd_pcm_access_name(pcm->access)); snd_output_printf(out, "format : %s\n", snd_pcm_format_name(pcm->format)); @@ -1662,7 +1735,10 @@ { assert(pcm); assert(out); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } snd_output_printf(out, "tstamp_mode : %s\n", snd_pcm_tstamp_mode_name(pcm->tstamp_mode)); snd_output_printf(out, "period_step : %d\n", pcm->period_step); snd_output_printf(out, "sleep_min : %d\n", pcm->sleep_min); @@ -1732,7 +1808,10 @@ snd_pcm_sframes_t snd_pcm_bytes_to_frames(snd_pcm_t *pcm, ssize_t bytes) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return bytes * 8 / pcm->frame_bits; } @@ -1745,7 +1824,10 @@ ssize_t snd_pcm_frames_to_bytes(snd_pcm_t *pcm, snd_pcm_sframes_t frames) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return frames * pcm->frame_bits / 8; } @@ -1758,7 +1840,10 @@ long snd_pcm_bytes_to_samples(snd_pcm_t *pcm, ssize_t bytes) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return bytes * 8 / pcm->sample_bits; } @@ -1771,7 +1856,10 @@ ssize_t snd_pcm_samples_to_bytes(snd_pcm_t *pcm, long samples) { assert(pcm); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } return samples * pcm->sample_bits / 8; } @@ -1817,7 +1905,10 @@ */ snd_pcm_t *snd_async_handler_get_pcm(snd_async_handler_t *handler) { - assert(handler->type == SND_ASYNC_HANDLER_PCM); + if (handler->type == SND_ASYNC_HANDLER_PCM) { + SNDERR("invalid handler type %d", handler->type); + return NULL; + } return handler->u.pcm; } @@ -2096,7 +2187,10 @@ err = snd_pcm_poll_descriptors(pcm, &pfd, 1); if (err < 0) return err; - assert(err == 1); + if (err != 1) { + SNDERR("invalid poll descriptors %d\n", err); + return -EIO; + } __retry: err_poll = poll(&pfd, 1, timeout); if (err_poll < 0) @@ -2247,7 +2341,8 @@ break; } default: - assert(0); + SNDERR("invalid format width %d", width); + return -EINVAL; } return 0; } @@ -2414,7 +2509,8 @@ break; } default: - assert(0); + SNDERR("invalid format width %d", width); + return -EINVAL; } return 0; } @@ -2437,8 +2533,14 @@ int width = snd_pcm_format_physical_width(format); assert(dst_areas); assert(src_areas); - assert(channels > 0); - assert(frames > 0); + if (! channels) { + SNDERR("invalid channels %d", channels); + return -EINVAL; + } + if (! frames) { + SNDERR("invalid frames %d", frames); + return -EINVAL; + } while (channels > 0) { unsigned int step = src_areas->step; void *src_addr = src_areas->addr; @@ -2521,7 +2623,11 @@ */ int snd_pcm_hw_params_can_mmap_sample_resolution(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_MMAP_VALID); } @@ -2538,7 +2644,11 @@ */ int snd_pcm_hw_params_is_double(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_DOUBLE); } @@ -2555,7 +2665,11 @@ */ int snd_pcm_hw_params_is_batch(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_BATCH); } @@ -2572,7 +2686,11 @@ */ int snd_pcm_hw_params_is_block_transfer(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_BLOCK_TRANSFER); } @@ -2589,7 +2707,11 @@ */ int snd_pcm_hw_params_can_overrange(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_OVERRANGE); } @@ -2606,7 +2728,11 @@ */ int snd_pcm_hw_params_can_pause(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_PAUSE); } @@ -2623,7 +2749,11 @@ */ int snd_pcm_hw_params_can_resume(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_RESUME); } @@ -2640,7 +2770,11 @@ */ int snd_pcm_hw_params_is_half_duplex(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_HALF_DUPLEX); } @@ -2657,7 +2791,11 @@ */ int snd_pcm_hw_params_is_joint_duplex(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_JOINT_DUPLEX); } @@ -2674,7 +2812,11 @@ */ int snd_pcm_hw_params_can_sync_start(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return 0; /* FIXME: should be a negative error? */ + } return !!(params->info & SNDRV_PCM_INFO_SYNC_START); } @@ -2692,7 +2834,11 @@ int snd_pcm_hw_params_get_rate_numden(const snd_pcm_hw_params_t *params, unsigned int *rate_num, unsigned int *rate_den) { - assert(params && params->rate_den != 0); + assert(params); + if (params->rate_den == 0) { + SNDERR("invalid rate_den value"); + return -EINVAL; + } *rate_num = params->rate_num; *rate_den = params->rate_den; return 0; @@ -2709,7 +2855,11 @@ */ int snd_pcm_hw_params_get_sbits(const snd_pcm_hw_params_t *params) { - assert(params && params->msbits != 0); + assert(params); + if (params->msbits == 0) { + SNDERR("invalid msbits value"); + return -EINVAL; + } return params->msbits; } @@ -2724,7 +2874,11 @@ */ int snd_pcm_hw_params_get_fifo_size(const snd_pcm_hw_params_t *params) { - assert(params && params->info != ~0U); + assert(params); + if (params->info == ~0U) { + SNDERR("invalid PCM info field"); + return -EINVAL; + } return params->fifo_size; } @@ -4975,7 +5129,10 @@ int snd_pcm_sw_params_current(snd_pcm_t *pcm, snd_pcm_sw_params_t *params) { assert(pcm && params); - assert(pcm->setup); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } params->tstamp_mode = pcm->tstamp_mode; params->period_step = pcm->period_step; params->sleep_min = pcm->sleep_min; @@ -5084,8 +5241,8 @@ params->start_threshold = pcm->boundary; break; default: - assert(0); - break; + SNDERR("invalid start mode value %d\n", val); + return -EINVAL; } return 0; } @@ -5132,7 +5289,7 @@ params->stop_threshold = pcm->boundary; break; default: - assert(0); + SNDERR("invalid xrun mode value %d\n", val); break; } return 0; @@ -5172,7 +5329,10 @@ #endif { assert(pcm && params); - assert(val <= SND_PCM_TSTAMP_LAST); + if (val > SND_PCM_TSTAMP_LAST) { + SNDERR("invalid tstamp_mode value %d", val); + return -EINVAL; + } params->tstamp_mode = val; return 0; } @@ -5288,7 +5448,10 @@ #endif { assert(pcm && params); - assert(val % pcm->min_align == 0); + if (val % pcm->min_align) { + SNDERR("xfer_align (%ld) is not aligned to min_align (%ld)", val, pcm->min_align); + return -EINVAL; + } params->xfer_align = val; return 0; } @@ -5417,7 +5580,11 @@ #endif { assert(pcm && params); - assert(val < pcm->buffer_size); + if (val >= pcm->buffer_size) { + SNDERR("invalid silent_threshold value %ld (buffer_size = %ld)", + val, pcm->buffer_size); + return -EINVAL; + } params->silence_threshold = val; return 0; } @@ -5467,7 +5634,11 @@ #endif { assert(pcm && params); - assert(val >= pcm->boundary || val <= pcm->buffer_size); + if (val < pcm->boundary && val > pcm->buffer_size) { + SNDERR("invalid silence_size %ld (boundary %ld, buffer_size %ld)", + val, pcm->boundary, pcm->buffer_size); + return -EINVAL; + } params->silence_size = val; return 0; } @@ -5957,9 +6128,18 @@ snd_pcm_uframes_t offset, snd_pcm_uframes_t frames) { + snd_pcm_uframes_t avail; + assert(pcm); - assert(offset == *pcm->appl.ptr % pcm->buffer_size); - assert(frames <= snd_pcm_mmap_avail(pcm)); + if (offset != *pcm->appl.ptr % pcm->buffer_size) { + SNDERR("commit offset (%ld) doesn't match with appl_ptr (%ld) %% buf_size (%ld)", + offset, *pcm->appl.ptr, pcm->buffer_size); + return -EINVAL; + } + if (frames > (avail = snd_pcm_mmap_avail(pcm))) { + SNDERR("commit frames (%ld) overflow (avail = %ld)", frames, avail); + return -EINVAL; + } return pcm->fast_ops->mmap_commit(pcm->fast_op_arg, offset, frames); } @@ -6062,7 +6242,8 @@ frames = size; if (frames > (snd_pcm_uframes_t) avail) frames = avail; - assert(frames != 0); + if (! frames) + break; err = func(pcm, areas, offset, frames); if (err < 0) break; @@ -6133,7 +6314,8 @@ frames = size; if (frames > (snd_pcm_uframes_t) avail) frames = avail; - assert(frames != 0); + if (! frames) + break; err = func(pcm, areas, offset, frames); if (err < 0) break; Index: alsa-lib/src/pcm/pcm_hw.c =================================================================== RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm_hw.c,v retrieving revision 1.97 diff -u -r1.97 pcm_hw.c --- alsa-lib/src/pcm/pcm_hw.c 17 Nov 2004 14:11:14 -0000 1.97 +++ alsa-lib/src/pcm/pcm_hw.c 29 Nov 2004 19:40:53 -0000 @@ -888,7 +888,6 @@ } while (size > 0); return result; } else { - snd_pcm_hw_t *hw = pcm->private_data; assert(hw->shadow_appl_ptr); } } @@ -917,7 +916,8 @@ hw->avail_update_flag = 0; if (err < 0) return err; - assert((snd_pcm_uframes_t)err == avail); + if (err != avail) + SNDERR("short read %ld for avail %ld", err, avail); return err; } } @@ -946,7 +946,10 @@ snd_pcm_hw_t *hw = pcm->private_data; char *name; int err = snd_card_get_name(hw->card, &name); - assert(err >= 0); + if (err < 0) { + SNDERR("cannot get card name"); + return; + } snd_output_printf(out, "Hardware PCM card %d '%s' device %d subdevice %d\n", hw->card, name, hw->device, hw->subdevice); free(name); @@ -1152,7 +1155,8 @@ filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE; break; default: - assert(0); + SNDERR("invalid stream %d", stream); + return -EINVAL; } sprintf(filename, filefmt, card, device); Index: alsa-lib/src/pcm/pcm_mmap.c =================================================================== RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm_mmap.c,v retrieving revision 1.49 diff -u -r1.49 pcm_mmap.c --- alsa-lib/src/pcm/pcm_mmap.c 9 Feb 2004 11:41:31 -0000 1.49 +++ alsa-lib/src/pcm/pcm_mmap.c 29 Nov 2004 19:47:47 -0000 @@ -105,7 +105,10 @@ { snd_pcm_uframes_t xfer = 0; - assert(snd_pcm_mmap_playback_avail(pcm) >= size); + if (snd_pcm_mmap_playback_avail(pcm) < size) { + SNDERR("too short avail %ld to size %ld", snd_pcm_mmap_playback_avail(pcm), size); + return -EPIPE; + } while (size > 0) { const snd_pcm_channel_area_t *pcm_areas; snd_pcm_uframes_t pcm_offset; @@ -134,7 +137,10 @@ { snd_pcm_uframes_t xfer = 0; - assert(snd_pcm_mmap_capture_avail(pcm) >= size); + if (snd_pcm_mmap_capture_avail(pcm) < size) { + SNDERR("too short avail %ld to size %ld", snd_pcm_mmap_capture_avail(pcm), size); + return -EPIPE; + } while (size > 0) { const snd_pcm_channel_area_t *pcm_areas; snd_pcm_uframes_t pcm_offset; @@ -275,8 +281,8 @@ info->step = pcm->sample_bits; break; default: - assert(0); - break; + SNDERR("invalid access type %d", pcm->access); + return -EINVAL; } info->addr = 0; info->type = SND_PCM_AREA_SHM; @@ -290,8 +296,14 @@ int err; unsigned int c; assert(pcm); - assert(pcm->setup); - assert(!pcm->mmap_channels); + if (! pcm->setup) { + SNDERR("PCM not set up"); + return -EIO; + } + if (pcm->mmap_channels || pcm->running_areas) { + SNDERR("Already mmapped"); + return -EBUSY; + } err = pcm->ops->mmap(pcm); if (err < 0) return err; @@ -300,7 +312,6 @@ pcm->mmap_channels = calloc(pcm->channels, sizeof(pcm->mmap_channels[0])); if (!pcm->mmap_channels) return -ENOMEM; - assert(!pcm->running_areas); pcm->running_areas = calloc(pcm->channels, sizeof(pcm->running_areas[0])); if (!pcm->running_areas) { free(pcm->mmap_channels); @@ -433,7 +444,10 @@ int err; unsigned int c; assert(pcm); - assert(pcm->mmap_channels); + if (! pcm->mmap_channels) { + SNDERR("Not mmapped"); + return -ENXIO; + } if (pcm->mmap_shadow) return pcm->ops->munmap(pcm); for (c = 0; c < pcm->channels; ++c) { @@ -499,7 +513,8 @@ { snd_pcm_uframes_t xfer = 0; snd_pcm_sframes_t err = 0; - assert(size > 0); + if (! size) + return 0; while (xfer < size) { snd_pcm_uframes_t frames = size - xfer; snd_pcm_uframes_t offset = snd_pcm_mmap_hw_offset(pcm); @@ -532,9 +547,8 @@ break; } default: - assert(0); - err = -EINVAL; - break; + SNDERR("invalid access type %d", pcm->access); + return -EINVAL; } if (err < 0) break; @@ -549,7 +563,8 @@ { snd_pcm_uframes_t xfer = 0; snd_pcm_sframes_t err = 0; - assert(size > 0); + if (! size) + return 0; while (xfer < size) { snd_pcm_uframes_t frames = size - xfer; snd_pcm_uframes_t offset = snd_pcm_mmap_hw_offset(pcm); @@ -581,9 +596,8 @@ frames = err; } default: - assert(0); - err = -EINVAL; - break; + SNDERR("invalid access type %d", pcm->access); + return -EINVAL; } if (err < 0) break; Index: alsa-lib/src/pcm/pcm_plugin.c =================================================================== RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm_plugin.c,v retrieving revision 1.47 diff -u -r1.47 pcm_plugin.c --- alsa-lib/src/pcm/pcm_plugin.c 19 Mar 2004 11:09:42 -0000 1.47 +++ alsa-lib/src/pcm/pcm_plugin.c 1 Dec 2004 16:19:25 -0000 @@ -373,7 +373,11 @@ break; frames = plugin->write(pcm, areas, offset, frames, slave_areas, slave_offset, &slave_frames); - assert(slave_frames <= snd_pcm_mmap_playback_avail(slave)); + if (slave_frames > snd_pcm_mmap_playback_avail(slave)) { + SNDERR("write overflow %ld > %ld", slave_frames, + snd_pcm_mmap_playback_avail(slave)); + return -EPIPE; + } snd_atomic_write_begin(&plugin->watom); snd_pcm_mmap_appl_forward(pcm, frames); result = snd_pcm_mmap_commit(slave, slave_offset, slave_frames); @@ -415,7 +419,11 @@ break; frames = plugin->read(pcm, areas, offset, frames, slave_areas, slave_offset, &slave_frames); - assert(slave_frames <= snd_pcm_mmap_capture_avail(slave)); + if (slave_frames > snd_pcm_mmap_capture_avail(slave)) { + SNDERR("read overflow %ld > %ld", slave_frames, + snd_pcm_mmap_playback_avail(slave)); + return -EPIPE; + } snd_atomic_write_begin(&plugin->watom); snd_pcm_mmap_appl_forward(pcm, frames); result = snd_pcm_mmap_commit(slave, slave_offset, slave_frames); @@ -531,7 +539,10 @@ slave_size -= frames; xfer += frames; } - assert(size == 0); + if (size) { + SNDERR("short commit: %d", size); + return -EPIPE; + } return xfer; } Index: alsa-lib/src/pcm/pcm_rate.c =================================================================== RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm_rate.c,v retrieving revision 1.60 diff -u -r1.60 pcm_rate.c --- alsa-lib/src/pcm/pcm_rate.c 22 Nov 2004 13:17:13 -0000 1.60 +++ alsa-lib/src/pcm/pcm_rate.c 1 Dec 2004 16:16:53 -0000 @@ -166,7 +166,10 @@ src_frames1++; states->u.linear.init = 2; /* get a new sample */ //printf("new_src_pos = %i\n", (src - (char *)snd_pcm_channel_area_addr(src_area, src_offset)) / src_step); - assert(src_frames1 <= src_frames); + if (src_frames1 > src_frames) { + SNDERR("src_frames overflow"); + break; + } } } if (dst_frames != dst_frames1) { @@ -243,7 +246,10 @@ after_put: dst += dst_step; dst_frames1++; - assert(dst_frames1 <= dst_frames); + if (dst_frames1 > dst_frames) { + SNDERR("dst_frames overflow"); + break; + } } old_sample = new_sample; } @@ -501,8 +507,10 @@ /* pitch is get_increment */ } rate->pitch = (((u_int64_t)dst_rate * LINEAR_DIV) + (src_rate / 2)) / src_rate; - assert(!rate->states); - assert(!rate->pareas); + if (rate->states || rate->pareas) { + SNDERR("rate plugin already in use"); + return -EBUSY; + } rate->states = malloc(channels * sizeof(*rate->states)); if (rate->states == NULL) return -ENOMEM; @@ -616,7 +624,11 @@ } } } while (1); - assert((snd_pcm_uframes_t)snd_pcm_rate_client_frames(pcm, slave->period_size ) == pcm->period_size ); + if ((snd_pcm_uframes_t)snd_pcm_rate_client_frames(pcm, slave->period_size ) != pcm->period_size) { + SNDERR("invalid slave period_size %ld for pcm period_size %ld", + slave->period_size, pcm->period_size); + return -EIO; + } } else { rate->pitch = (((u_int64_t)pcm->period_size * LINEAR_DIV) + (slave->period_size/2) ) / slave->period_size; do { @@ -639,7 +651,11 @@ } } } while (1); - assert((snd_pcm_uframes_t)snd_pcm_rate_slave_frames(pcm, pcm->period_size ) == slave->period_size ); + if ((snd_pcm_uframes_t)snd_pcm_rate_slave_frames(pcm, pcm->period_size ) != slave->period_size) { + SNDERR("invalid pcm period_size %ld for slave period_size", + pcm->period_size, slave->period_size); + return -EIO; + } } recalc(pcm, &sparams->avail_min); rate->orig_avail_min = sparams->avail_min; @@ -1044,7 +1060,10 @@ result = snd_pcm_mmap_begin(rate->slave, &slave_areas, &slave_offset, &slave_frames); if (result < 0) return result; - assert(slave_offset == 0); + if (slave_offset) { + SNDERR("non-zero slave_offset %ld", slave_offset); + return -EIO; + } snd_pcm_areas_copy(slave_areas, slave_offset, rate->sareas, xfer, pcm->channels, cont, @@ -1124,7 +1143,10 @@ result = snd_pcm_mmap_begin(rate->slave, &slave_areas, &slave_offset, &slave_frames); if (result < 0) return result; - assert(slave_offset == 0); + if (slave_offset) { + SNDERR("non-zero slave_offset %ld", slave_offset); + return -EIO; + } snd_pcm_areas_copy(rate->sareas, xfer, slave_areas, slave_offset, pcm->channels, cont, Index: alsa-lib/src/pcm/pcm_share.c =================================================================== RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm_share.c,v retrieving revision 1.55 diff -u -r1.55 pcm_share.c --- alsa-lib/src/pcm/pcm_share.c 24 May 2004 14:55:58 -0000 1.55 +++ alsa-lib/src/pcm/pcm_share.c 29 Nov 2004 19:57:37 -0000 @@ -238,8 +238,13 @@ missing = 1; } err = snd_pcm_mmap_commit(spcm, snd_pcm_mmap_offset(spcm), frames); - assert(err == frames); - slave_avail -= frames; + if (err < 0) { + SYSERR("snd_pcm_mmap_commit error"); + return INT_MAX; + } + if (err != frames) + SYSERR("commit returns %ld for size %ld", err, frames); + slave_avail -= err; } else { if (safety_missing == 0) missing = 1; @@ -278,8 +283,8 @@ running = 1; break; default: - assert(0); - break; + SNDERR("invalid shared PCM state %d", share->state); + return INT_MAX; } update_poll: @@ -357,10 +362,16 @@ pfd[0].fd = slave->poll[0]; pfd[0].events = POLLIN; err = snd_pcm_poll_descriptors(spcm, &pfd[1], 1); - assert(err == 1); + if (err != 1) { + SNDERR("invalid poll descriptors %d", err); + return NULL; + } Pthread_mutex_lock(&slave->mutex); err = pipe(slave->poll); - assert(err >= 0); + if (err < 0) { + SYSERR("can't create a pipe"); + return NULL; + } while (slave->open_count > 0) { snd_pcm_uframes_t missing; // printf("begin min_missing\n"); @@ -383,7 +394,10 @@ if ((snd_pcm_uframes_t)avail_min != spcm->avail_min) { snd_pcm_sw_params_set_avail_min(spcm, &slave->sw_params, avail_min); err = snd_pcm_sw_params(spcm, &slave->sw_params); - assert(err >= 0); + if (err < 0) { + SYSERR("snd_pcm_sw_params error"); + return NULL; + } } slave->polling = 1; Pthread_mutex_unlock(&slave->mutex); @@ -433,7 +447,10 @@ int err; snd_pcm_sw_params_set_avail_min(spcm, &slave->sw_params, avail_min); err = snd_pcm_sw_params(spcm, &slave->sw_params); - assert(err >= 0); + if (err < 0) { + SYSERR("snd_pcm_sw_params error"); + return; + } } } } @@ -818,7 +835,14 @@ if (frames > 0) { snd_pcm_sframes_t err; err = snd_pcm_mmap_commit(spcm, snd_pcm_mmap_offset(spcm), frames); - assert(err == frames); + if (err < 0) { + SYSERR("snd_pcm_mmap_commit error"); + return err; + } + if (err != frames) { + SYSERR("commit returns %ld for size %ld", err, frames); + return err; + } } _snd_pcm_share_update(pcm); } --Multipart_Wed_Dec__1_17:31:15_2004-1-- ------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://productguide.itmanagersjournal.com/