Removal of assert() in alsa-lib
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
Index: alsa-lib/src/pcm/pcm.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm.c,v retrieving revision 1.161
src/pcm/pcm.c +248 -66
@@ -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
src/pcm/pcm_hw.c +8 -4
@@ -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
src/pcm/pcm_mmap.c +30 -16
106 lines not on this page · raw message
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
src/pcm/pcm_plugin.c +14 -3
@@ -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
src/pcm/pcm_rate.c +30 -8
@@ -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
src/pcm/pcm_share.c +33 -9
80 lines not on this page · raw message
Quoted 3 lines · Takashi Iwai
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.
I think that we should add new macros like SNDERR_VERBOSE etc. to not compile error messages to small version of ALSA library (for embedded devices etc). Almost all current assert()s meet this rule.
Jaroslav
-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs
-------------------------------------------------------
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/
At Wed, 1 Dec 2004 18:51:44 +0100 (CET),
Quoted 10 lines · Jaroslav
Quoted 3 lines · Takashi Iwai
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.I think that we should add new macros like
SNDERR_VERBOSEetc. to not compile error messages to small version of ALSA library (for embedded devices etc). Almost all current assert()s meet this rule.
Or, let the compiler optimize it.
For example, define like
#if YES_WE_CHECK_SANITY
#define SND_SANITY_CHECK(x) x
#else
#define SND_SANITY_CHECK(x) 0
#endif
so that the code like the following will be removed automatically by the compiler in the optimized case.
if (SND_SANITY_CHECK(frames > size)) {
SNDERR("buffer overflow");
return -EPIPE;
}Takashi
-------------------------------------------------------
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/
At Wed, 01 Dec 2004 19:01:37 +0100,
Quoted 24 lines · I wrote:
At Wed, 1 Dec 2004 18:51:44 +0100 (CET),
Quoted 10 lines · Jaroslav
Quoted 3 lines · Takashi Iwai
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.I think that we should add new macros like
SNDERR_VERBOSEetc. to not compile error messages to small version of ALSA library (for embedded devices etc). Almost all current assert()s meet this rule.Or, let the compiler optimize it.
For example, define like
#if
YES_WE_CHECK_SANITY
#defineSND_SANITY_CHECK(x)x
#else
#defineSND_SANITY_CHECK(x)0
#endif
Quoted 7 lines
so that the code like the following will be removed automatically by the compiler in the optimized case.
if (SND_SANITY_CHECK(frames > size)) { SNDERR("buffer overflow"); return -EPIPE; }
After rethoght, I think both should be introduced.
In many places, the check should be done but the error messages are not needed (always). For example, the check in snd_pcm_mmap_commit() looks mandatory. Such checks should simply return errors without showing error messages unless the debug condition is given. The macro can check it either at the compile time or via an environment variable like LIB_ASOUND_DEBUG=1.
Takashi
-------------------------------------------------------
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/
Now the second try.
New macros SNDMSG() and SYSMSG() are introduced. They are shown only when the environment variable "LIBASOUND_VERBOSE" is set. When nodebug option is set, they will be not compiled in.
Could you check whether it's OK?
Takashi
Index: alsa-lib/NOTES
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-lib/NOTES,v
retrieving revision 1.1
NOTES +8 -0
@@ -14,3 +14,11 @@ #define ALSA_PCM_NEW_HW_PARAMS_API #define ALSA_PCM_NEW_SW_PARAMS_API + + +Verbose Error Messages +====================== + +In the recent version, some of error messages are not shown to stderr +as default. They can be shown only when the environment variable +LIBASOUND_VERBOSE is set.
Index: alsa-lib/include/local.h
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-lib/include/local.h,v retrieving revision 1.35
include/local.h +16 -0
@@ -155,6 +155,22 @@int snd_send_fd (int sock,void *data,size_t len,int fd);int snd_receive_fd (int sock,void *data,size_t len,int *fd); +/* +* error messages +*/ +#ifndef NDEBUG +#define CHECK_SANITY (x) x +void snd_err_msg (const char *file,int line,const char *function,int err,const char *fmt, ...) __attribute__ ((format (printf,5 ,6 ))); +#define SNDMSG(args...) snd_err_msg(__FILE__, __LINE__, __FUNCTION__, 0, ##args) +#define SYSMSG(args...) snd_err_msg(__FILE__, __LINE__, __FUNCTION__, errno, ##args) +#else +#define CHECK_SANITY (x) 0/* not evaluated */ +#define SNDMSG(args...) /* nop */ +#define SYSMSG(args...) /* nop */ +#endif + +/* +*/ #define HAVE_GNU_LD #define HAVE_ELF #define HAVE_ASM_PREVIOUS_DIRECTIVE
Index: alsa-lib/src/error.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/error.c,v retrieving revision 1.13
src/error.c +22 -0
@@ -111,3 +111,25 @@ {return SND_LIB_VERSION_STR ; } + +#ifndef NDEBUG +/* +* internal error handling +*/ +void snd_err_msg (const char *file,int line,const char *function,int err,const char *fmt, ...) +{ +va_list arg; +const char *verbose; + + verbose =getenv ("LIBASOUND_DEBUG" ); +if (! verbose || ! *verbose) +return ; +va_start (arg, fmt); +fprintf (stderr,"ALSA lib %s:%i:(%s) " , file, line, function); +vfprintf (stderr, fmt, arg); +if (err) +fprintf (stderr,": %s" ,snd_strerror (err)); +putc ('\n' , stderr); +va_end (arg); +} +#endif
Index: alsa-lib/src/pcm/pcm.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-lib/src/pcm/pcm.c,v retrieving revision 1.162
src/pcm/pcm.c +250 -69
669 lines not on this page · raw message
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
src/pcm/pcm_hw.c +44 -60
348 lines not on this page · raw message
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
src/pcm/pcm_mmap.c +30 -16
106 lines not on this page · raw message
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
src/pcm/pcm_plugin.c +14 -3
@@ -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 (CHECK_SANITY (slave_frames >snd_pcm_mmap_playback_avail (slave))) { +SNDMSG ("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 (CHECK_SANITY (slave_frames >snd_pcm_mmap_capture_avail (slave))) { +SNDMSG ("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 (CHECK_SANITY (size)) { +SNDMSG ("short commit: %ld" , 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
src/pcm/pcm_rate.c +30 -8
@@ -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 (CHECK_SANITY (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 (CHECK_SANITY (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 (CHECK_SANITY (rate->states || rate->pareas)) { +SNDMSG ("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
src/pcm/pcm_share.c +33 -9
80 lines not on this page · raw message
Quoted 1 line · Takashi Iwai
+void
snd_err_msg(const char *file, int line, const char *function, int err, const char *fmt, ...)__attribute__((format (printf, 5, 6)));
Can this be a weak symbol, so it can be overridden by user code?
If my application causes errors on some version of ALSA, having diagnostic information going to stderr is inconvenient: I centralize all diagnostics for bug reporting in a single file, so I don't have to pull teeth from users to have them copy bits of information from other places.
As long as snd_err_msg() is only called on "unexpected" errors (eg. what used to be assertion failures), and made a weak symbol, I can redirect it to my logs.
I currently do this with __assert_fail() and __assert_perror_fail(), and I'd like to continue to be able to catch all important diagnostic output automatically.
Signature · 8 lines
Glenn Maynard ------------------------------------------------------- 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/
Quoted 4 lines · Glenn Maynard
Quoted 1 line · Takashi Iwai
+void
snd_err_msg(const char *file, int line, const char *function, int err, const char *fmt, ...)__attribute__((format (printf, 5, 6)));Can this be a weak symbol, so it can be overridden by user code?
snd_lib_error_set_handler() (or something similar) will do it for you.
Jaroslav
-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs
-------------------------------------------------------
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/
At Fri, 10 Dec 2004 08:49:36 +0100 (CET),
Quoted 9 lines · Jaroslav
Quoted 4 lines · Glenn Maynard
Quoted 1 line · Takashi Iwai
+void
snd_err_msg(const char *file, int line, const char *function, int err, const char *fmt, ...)__attribute__((format (printf, 5, 6)));Can this be a weak symbol, so it can be overridden by user code?
snd_lib_error_set_handler()(or something similar) will do it for you.
In my patch, it doesn't work so. These verbose message is handled in a static function. I'll change it so that you can set assert() in your own error handler.
Takashi
-------------------------------------------------------
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/