All of lore.kernel.org
 help / color / mirror / Atom feed
* Removal of assert() in alsa-lib
@ 2004-12-01 16:31 Takashi Iwai
  2004-12-01 17:51 ` Jaroslav Kysela
  0 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2004-12-01 16:31 UTC (permalink / raw)
  To: alsa-devel

[-- Attachment #1: Type: text/plain, Size: 845 bytes --]

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

[-- Attachment #2: Type: text/plain, Size: 28917 bytes --]

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);
 	}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-01 16:31 Removal of assert() in alsa-lib Takashi Iwai
@ 2004-12-01 17:51 ` Jaroslav Kysela
  2004-12-01 18:01   ` Takashi Iwai
  0 siblings, 1 reply; 8+ messages in thread
From: Jaroslav Kysela @ 2004-12-01 17:51 UTC (permalink / raw)
  To: Takashi Iwai; +Cc: alsa-devel

On Wed, 1 Dec 2004, Takashi Iwai wrote:

> 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 <perex@suse.cz>
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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-01 17:51 ` Jaroslav Kysela
@ 2004-12-01 18:01   ` Takashi Iwai
  2004-12-02 12:03     ` Takashi Iwai
  0 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2004-12-01 18:01 UTC (permalink / raw)
  To: Jaroslav Kysela; +Cc: alsa-devel

At Wed, 1 Dec 2004 18:51:44 +0100 (CET),
Jaroslav wrote:
> 
> On Wed, 1 Dec 2004, Takashi Iwai wrote:
> 
> > 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.

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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-01 18:01   ` Takashi Iwai
@ 2004-12-02 12:03     ` Takashi Iwai
  2004-12-09 16:23       ` Takashi Iwai
  0 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2004-12-02 12:03 UTC (permalink / raw)
  To: Jaroslav Kysela; +Cc: alsa-devel

At Wed, 01 Dec 2004 19:01:37 +0100,
I wrote:
> 
> At Wed, 1 Dec 2004 18:51:44 +0100 (CET),
> Jaroslav wrote:
> > 
> > On Wed, 1 Dec 2004, Takashi Iwai wrote:
> > 
> > > 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.
> 
> 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;
> 	}

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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-02 12:03     ` Takashi Iwai
@ 2004-12-09 16:23       ` Takashi Iwai
  2004-12-10  5:46         ` Glenn Maynard
  0 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2004-12-09 16:23 UTC (permalink / raw)
  To: Jaroslav Kysela; +Cc: alsa-devel

[-- Attachment #1: Type: text/plain, Size: 252 bytes --]

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

[-- Attachment #2: Type: text/plain, Size: 41513 bytes --]

Index: alsa-lib/NOTES
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-lib/NOTES,v
retrieving revision 1.1
diff -u -r1.1 NOTES
--- alsa-lib/NOTES	7 Nov 2003 16:38:48 -0000	1.1
+++ alsa-lib/NOTES	9 Dec 2004 16:20:51 -0000
@@ -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
diff -u -r1.35 local.h
--- alsa-lib/include/local.h	8 Jul 2003 10:03:09 -0000	1.35
+++ alsa-lib/include/local.h	9 Dec 2004 16:06:41 -0000
@@ -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
diff -u -r1.13 error.c
--- alsa-lib/src/error.c	23 Apr 2004 14:42:04 -0000	1.13
+++ alsa-lib/src/error.c	9 Dec 2004 16:19:23 -0000
@@ -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
diff -u -r1.162 pcm.c
--- alsa-lib/src/pcm/pcm.c	8 Dec 2004 20:34:26 -0000	1.162
+++ alsa-lib/src/pcm/pcm.c	9 Dec 2004 16:16:27 -0000
@@ -797,7 +797,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)
@@ -820,17 +821,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 (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("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");
+		SNDMSG("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");
+		SNDMSG("params->xfer_align problem for start_threshold");
 		return -EINVAL;
 	}
 	err = pcm->ops->sw_params(pcm->op_arg, params);
@@ -887,7 +892,10 @@
 int snd_pcm_hwsync(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->hwsync(pcm->fast_op_arg);
 }
 
@@ -910,7 +918,10 @@
 int snd_pcm_delay(snd_pcm_t *pcm, snd_pcm_sframes_t *delayp)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->delay(pcm->fast_op_arg, delayp);
 }
 
@@ -929,7 +940,10 @@
 int snd_pcm_resume(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->resume(pcm->fast_op_arg);
 }
 
@@ -941,7 +955,10 @@
 int snd_pcm_prepare(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->prepare(pcm->fast_op_arg);
 }
 
@@ -955,7 +972,10 @@
 int snd_pcm_reset(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->reset(pcm->fast_op_arg);
 }
 
@@ -967,7 +987,10 @@
 int snd_pcm_start(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->start(pcm->fast_op_arg);
 }
 
@@ -985,7 +1008,10 @@
 int snd_pcm_drop(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->drop(pcm->fast_op_arg);
 }
 
@@ -1005,7 +1031,10 @@
 int snd_pcm_drain(snd_pcm_t *pcm)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->drain(pcm->fast_op_arg);
 }
 
@@ -1022,7 +1051,10 @@
 int snd_pcm_pause(snd_pcm_t *pcm, int enable)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return pcm->fast_ops->pause(pcm->fast_op_arg, enable);
 }
 
@@ -1036,8 +1068,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 (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
+	if (frames == 0)
+		return 0;
 	return pcm->fast_ops->rewind(pcm->fast_op_arg, frames);
 }
 
@@ -1055,8 +1091,12 @@
 #endif
 {
 	assert(pcm);
-	assert(pcm->setup);
-	assert(frames > 0);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("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);
@@ -1082,8 +1122,14 @@
 {
 	assert(pcm);
 	assert(size == 0 || buffer);
-	assert(pcm->setup);
-	assert(pcm->access == SND_PCM_ACCESS_RW_INTERLEAVED);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
+	if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) {
+		SNDMSG("invalid access type %s", snd_pcm_access_name(pcm->access));
+		return -EINVAL;
+	}
 	return _snd_pcm_writei(pcm, buffer, size);
 }
 
@@ -1108,8 +1154,14 @@
 {
 	assert(pcm);
 	assert(size == 0 || bufs);
-	assert(pcm->setup);
-	assert(pcm->access == SND_PCM_ACCESS_RW_NONINTERLEAVED);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
+	if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
+		SNDMSG("invalid access type %s", snd_pcm_access_name(pcm->access));
+		return -EINVAL;
+	}
 	return _snd_pcm_writen(pcm, bufs, size);
 }
 
@@ -1134,8 +1186,14 @@
 {
 	assert(pcm);
 	assert(size == 0 || buffer);
-	assert(pcm->setup);
-	assert(pcm->access == SND_PCM_ACCESS_RW_INTERLEAVED);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
+	if (pcm->access != SND_PCM_ACCESS_RW_INTERLEAVED) {
+		SNDMSG("invalid access type %s", snd_pcm_access_name(pcm->access));
+		return -EINVAL;
+	}
 	return _snd_pcm_readi(pcm, buffer, size);
 }
 
@@ -1160,8 +1218,14 @@
 {
 	assert(pcm);
 	assert(size == 0 || bufs);
-	assert(pcm->setup);
-	assert(pcm->access == SND_PCM_ACCESS_RW_NONINTERLEAVED);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
+	if (pcm->access != SND_PCM_ACCESS_RW_NONINTERLEAVED) {
+		SNDMSG("invalid access type %s", snd_pcm_access_name(pcm->access));
+		return -EINVAL;
+	}
 	return _snd_pcm_readn(pcm, bufs, size);
 }
 
@@ -1180,7 +1244,7 @@
 	if (fd1 < 0 || fd2 < 0)
 		return -ENOSYS;
 	if (ioctl(fd1, SNDRV_PCM_IOCTL_LINK, fd2) < 0) {
-		SYSERR("SNDRV_PCM_IOCTL_LINK failed");
+		SYSMSG("SNDRV_PCM_IOCTL_LINK failed");
 		return -errno;
 	}
 	return 0;
@@ -1196,7 +1260,7 @@
 	int fd;
 	fd = _snd_pcm_link_descriptor(pcm);
 	if (ioctl(fd, SNDRV_PCM_IOCTL_UNLINK) < 0) {
-		SYSERR("SNDRV_PCM_IOCTL_UNLINK failed");
+		SYSMSG("SNDRV_PCM_IOCTL_UNLINK failed");
 		return -errno;
 	}
 	return 0;
@@ -1248,7 +1312,10 @@
 		if (err < 0)
 			return err;
 	}
-	assert(pcm->poll_fd >= 0);
+	if (! pcm->poll_fd < 0) {
+		SNDMSG("poll_fd < 0");
+		return -EIO;
+	}
 	if (space >= 1 && pfds) {
 		pfds->fd = pcm->poll_fd;
 		pfds->events = pcm->poll_events | POLLERR | POLLNVAL;
@@ -1470,7 +1537,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];
 }
 
@@ -1564,7 +1632,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];
 }
 
@@ -1579,7 +1648,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];
 }
 
@@ -1638,7 +1708,10 @@
 {
 	assert(pcm);
 	assert(out);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("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));
@@ -1664,7 +1737,10 @@
 {
 	assert(pcm);
 	assert(out);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("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);
@@ -1734,7 +1810,10 @@
 snd_pcm_sframes_t snd_pcm_bytes_to_frames(snd_pcm_t *pcm, ssize_t bytes)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return bytes * 8 / pcm->frame_bits;
 }
 
@@ -1747,7 +1826,10 @@
 ssize_t snd_pcm_frames_to_bytes(snd_pcm_t *pcm, snd_pcm_sframes_t frames)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return frames * pcm->frame_bits / 8;
 }
 
@@ -1760,7 +1842,10 @@
 long snd_pcm_bytes_to_samples(snd_pcm_t *pcm, ssize_t bytes)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return bytes * 8 / pcm->sample_bits;
 }
 
@@ -1773,7 +1858,10 @@
 ssize_t snd_pcm_samples_to_bytes(snd_pcm_t *pcm, long samples)
 {
 	assert(pcm);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	return samples * pcm->sample_bits / 8;
 }
 
@@ -1819,7 +1907,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) {
+		SNDMSG("invalid handler type %d", handler->type);
+		return NULL;
+	}
 	return handler->u.pcm;
 }
 
@@ -2099,7 +2190,10 @@
 	err = snd_pcm_poll_descriptors(pcm, &pfd, 1);
 	if (err < 0)
 		return err;
-	assert(err == 1);
+	if (err != 1) {
+		SNDMSG("invalid poll descriptors %d\n", err);
+		return -EIO;
+	}
       __retry:
 	err_poll = poll(&pfd, 1, timeout);
 	if (err_poll < 0)
@@ -2250,7 +2344,8 @@
 		break;
 	}
 	default:
-		assert(0);
+		SNDMSG("invalid format width %d", width);
+		return -EINVAL;
 	}
 	return 0;
 }
@@ -2417,7 +2512,8 @@
 		break;
 	}
 	default:
-		assert(0);
+		SNDMSG("invalid format width %d", width);
+		return -EINVAL;
 	}
 	return 0;
 }
@@ -2440,8 +2536,14 @@
 	int width = snd_pcm_format_physical_width(format);
 	assert(dst_areas);
 	assert(src_areas);
-	assert(channels > 0);
-	assert(frames > 0);
+	if (! channels) {
+		SNDMSG("invalid channels %d", channels);
+		return -EINVAL;
+	}
+	if (! frames) {
+		SNDMSG("invalid frames %ld", frames);
+		return -EINVAL;
+	}
 	while (channels > 0) {
 		unsigned int step = src_areas->step;
 		void *src_addr = src_areas->addr;
@@ -2524,7 +2626,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 (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_MMAP_VALID);
 }
 
@@ -2541,7 +2647,11 @@
  */
 int snd_pcm_hw_params_is_double(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_DOUBLE);
 }
 
@@ -2558,7 +2668,11 @@
  */
 int snd_pcm_hw_params_is_batch(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_BATCH);
 }
 
@@ -2575,7 +2689,11 @@
  */
 int snd_pcm_hw_params_is_block_transfer(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_BLOCK_TRANSFER);
 }
 
@@ -2592,7 +2710,11 @@
  */
 int snd_pcm_hw_params_can_overrange(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_OVERRANGE);
 }
 
@@ -2609,7 +2731,11 @@
  */
 int snd_pcm_hw_params_can_pause(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_PAUSE);
 }
 
@@ -2626,7 +2752,11 @@
  */
 int snd_pcm_hw_params_can_resume(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_RESUME);
 }
 
@@ -2643,7 +2773,11 @@
  */
 int snd_pcm_hw_params_is_half_duplex(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_HALF_DUPLEX);
 }
 
@@ -2660,7 +2794,11 @@
  */
 int snd_pcm_hw_params_is_joint_duplex(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_JOINT_DUPLEX);
 }
 
@@ -2677,7 +2815,11 @@
  */
 int snd_pcm_hw_params_can_sync_start(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return 0; /* FIXME: should be a negative error? */
+	}
 	return !!(params->info & SNDRV_PCM_INFO_SYNC_START);
 }
 
@@ -2695,7 +2837,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 (CHECK_SANITY(params->rate_den == 0)) {
+		SNDMSG("invalid rate_den value");
+		return -EINVAL;
+	}
 	*rate_num = params->rate_num;
 	*rate_den = params->rate_den;
 	return 0;
@@ -2712,7 +2858,11 @@
  */
 int snd_pcm_hw_params_get_sbits(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->msbits != 0);
+	assert(params);
+	if (CHECK_SANITY(params->msbits == 0)) {
+		SNDMSG("invalid msbits value");
+		return -EINVAL;
+	}
 	return params->msbits;
 }
 
@@ -2727,7 +2877,11 @@
  */
 int snd_pcm_hw_params_get_fifo_size(const snd_pcm_hw_params_t *params)
 {
-	assert(params && params->info != ~0U);
+	assert(params);
+	if (CHECK_SANITY(params->info == ~0U)) {
+		SNDMSG("invalid PCM info field");
+		return -EINVAL;
+	}
 	return params->fifo_size;
 }
 
@@ -4978,7 +5132,10 @@
 int snd_pcm_sw_params_current(snd_pcm_t *pcm, snd_pcm_sw_params_t *params)
 {
 	assert(pcm && params);
-	assert(pcm->setup);
+	if (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
 	params->tstamp_mode = pcm->tstamp_mode;
 	params->period_step = pcm->period_step;
 	params->sleep_min = pcm->sleep_min;
@@ -5087,8 +5244,8 @@
 		params->start_threshold = pcm->boundary;
 		break;
 	default:
-		assert(0);
-		break;
+		SNDMSG("invalid start mode value %d\n", val);
+		return -EINVAL;
 	}
 	return 0;
 }
@@ -5135,8 +5292,8 @@
 		params->stop_threshold = pcm->boundary;
 		break;
 	default:
-		assert(0);
-		break;
+		SNDMSG("invalid xrun mode value %d\n", val);
+		return -EINVAL;
 	}
 	return 0;
 }
@@ -5175,7 +5332,10 @@
 #endif
 {
 	assert(pcm && params);
-	assert(val <= SND_PCM_TSTAMP_LAST);
+	if (CHECK_SANITY(val > SND_PCM_TSTAMP_LAST)) {
+		SNDMSG("invalid tstamp_mode value %d", val);
+		return -EINVAL;
+	}
 	params->tstamp_mode = val;
 	return 0;
 }
@@ -5291,7 +5451,10 @@
 #endif
 {
 	assert(pcm && params);
-	assert(val % pcm->min_align == 0);
+	if (CHECK_SANITY(val % pcm->min_align)) {
+		SNDMSG("xfer_align (%ld) is not aligned to min_align (%ld)", val, pcm->min_align);
+		return -EINVAL;
+	}
 	params->xfer_align = val;
 	return 0;
 }
@@ -5420,7 +5583,11 @@
 #endif
 {
 	assert(pcm && params);
-	assert(val < pcm->buffer_size);
+	if (CHECK_SANITY(val >= pcm->buffer_size)) {
+		SNDMSG("invalid silent_threshold value %ld (buffer_size = %ld)",
+		       val, pcm->buffer_size);
+		return -EINVAL;
+	}
 	params->silence_threshold = val;
 	return 0;
 }
@@ -5470,7 +5637,11 @@
 #endif
 {
 	assert(pcm && params);
-	assert(val >= pcm->boundary || val <= pcm->buffer_size);
+	if (CHECK_SANITY(val < pcm->boundary && val > pcm->buffer_size)) {
+		SNDMSG("invalid silence_size %ld (boundary %ld, buffer_size %ld)",
+		       val, pcm->boundary, pcm->buffer_size);
+		return -EINVAL;
+	}
 	params->silence_size = val;
 	return 0;
 }
@@ -5961,8 +6132,16 @@
 				      snd_pcm_uframes_t frames)
 {
 	assert(pcm);
-	assert(offset == *pcm->appl.ptr % pcm->buffer_size);
-	assert(frames <= snd_pcm_mmap_avail(pcm));
+	if (CHECK_SANITY(offset != *pcm->appl.ptr % pcm->buffer_size)) {
+		SNDMSG("commit offset (%ld) doesn't match with appl_ptr (%ld) %% buf_size (%ld)",
+		       offset, *pcm->appl.ptr, pcm->buffer_size);
+		return -EPIPE;
+	}
+	if (CHECK_SANITY(frames > snd_pcm_mmap_avail(pcm))) {
+		SNDMSG("commit frames (%ld) overflow (avail = %ld)", frames,
+		       snd_pcm_mmap_avail(pcm));
+		return -EPIPE;
+	}
 	return pcm->fast_ops->mmap_commit(pcm->fast_op_arg, offset, frames);
 }
 
@@ -6065,7 +6244,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;
@@ -6136,7 +6316,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	9 Dec 2004 16:16:47 -0000
@@ -113,19 +113,6 @@
 	((hw)->mmap_status->tstamp)
 #endif /* DOC_HIDDEN */
 
-static inline int check_std_error(int error)
-{
-	switch (error) {
-	case -EAGAIN:
-	case -EPIPE:
-	case -ESTRPIPE:
-	case -ENXIO:
-	case -ENOSYS:
-		return 0;
-	}
-	return 1;
-}
-
 struct timespec snd_pcm_hw_fast_tstamp(snd_pcm_t *pcm)
 {
 	struct timespec res;
@@ -143,7 +130,7 @@
 	err = ioctl((hw)->fd, SNDRV_PCM_IOCTL_SYNC_PTR, (hw)->sync_ptr);
 	if (err < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_SYNC_PTR failed");
+		SYSMSG("SNDRV_PCM_IOCTL_SYNC_PTR failed");
 		return err;
 	}
 	return 0;
@@ -162,7 +149,7 @@
 
 	if ((flags = fcntl(fd, F_GETFL)) < 0) {
 		err = -errno;
-		SYSERR("F_GETFL failed");
+		SYSMSG("F_GETFL failed");
 		return err;
 	}
 	if (nonblock)
@@ -171,7 +158,7 @@
 		flags &= ~O_NONBLOCK;
 	if (fcntl(fd, F_SETFL, flags) < 0) {
 		err = -errno;
-		SYSERR("F_SETFL for O_NONBLOCK failed");
+		SYSMSG("F_SETFL for O_NONBLOCK failed");
 		return err;
 	}
 	return 0;
@@ -185,7 +172,7 @@
 
 	if ((flags = fcntl(fd, F_GETFL)) < 0) {
 		err = -errno;
-		SYSERR("F_GETFL failed");
+		SYSMSG("F_GETFL failed");
 		return err;
 	}
 	if (sig >= 0)
@@ -194,19 +181,19 @@
 		flags &= ~O_ASYNC;
 	if (fcntl(fd, F_SETFL, flags) < 0) {
 		err = -errno;
-		SYSERR("F_SETFL for O_ASYNC failed");
+		SYSMSG("F_SETFL for O_ASYNC failed");
 		return err;
 	}
 	if (sig < 0)
 		return 0;
 	if (fcntl(fd, F_SETSIG, (long)sig) < 0) {
 		err = -errno;
-		SYSERR("F_SETSIG failed");
+		SYSMSG("F_SETSIG failed");
 		return err;
 	}
 	if (fcntl(fd, F_SETOWN, (long)pid) < 0) {
 		err = -errno;
-		SYSERR("F_SETOWN failed");
+		SYSMSG("F_SETOWN failed");
 		return err;
 	}
 	return 0;
@@ -218,7 +205,7 @@
 	int fd = hw->fd, err;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, info) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_INFO failed");
+		SYSMSG("SNDRV_PCM_IOCTL_INFO failed");
 		return err;
 	}
 	return 0;
@@ -300,7 +287,7 @@
 	} else {
 		if (hw_refine_call(hw, params) < 0) {
 			err = -errno;
-			// SYSERR("SNDRV_PCM_IOCTL_HW_REFINE failed");
+			// SYSMSG("SNDRV_PCM_IOCTL_HW_REFINE failed");
 			return err;
 		}
 	}
@@ -353,7 +340,7 @@
 		if (hw_params_call(hw, params) < 0) {
 		      _err:
 			err = -errno;
-			SYSERR("SNDRV_PCM_IOCTL_HW_PARAMS failed");
+			SYSMSG("SNDRV_PCM_IOCTL_HW_PARAMS failed");
 			return err;
 		}
 	}
@@ -379,7 +366,7 @@
 	int fd = hw->fd, err;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_HW_FREE) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_HW_FREE failed");
+		SYSMSG("SNDRV_PCM_IOCTL_HW_FREE failed");
 		return err;
 	}
 	return 0;
@@ -402,7 +389,7 @@
 	}
 	if (ioctl(fd, SNDRV_PCM_IOCTL_SW_PARAMS, params) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_SW_PARAMS failed");
+		SYSMSG("SNDRV_PCM_IOCTL_SW_PARAMS failed");
 		return err;
 	}
 	hw->mmap_control->avail_min = params->avail_min;
@@ -417,7 +404,7 @@
 	i.channel = info->channel;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_CHANNEL_INFO, &i) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_CHANNEL_INFO failed");
+		SYSMSG("SNDRV_PCM_IOCTL_CHANNEL_INFO failed");
 		return err;
 	}
 	info->channel = i.channel;
@@ -439,7 +426,7 @@
 	int fd = hw->fd, err;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_STATUS, status) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_STATUS failed");
+		SYSMSG("SNDRV_PCM_IOCTL_STATUS failed");
 		return err;
 	}
 	if (SNDRV_PROTOCOL_VERSION(2, 0, 5) > hw->version) {
@@ -486,8 +473,7 @@
 	}
 	if (ioctl(fd, SNDRV_PCM_IOCTL_DELAY, delayp) < 0) {
 		err = -errno;
-		if (check_std_error(err))
-			SYSERR("SNDRV_PCM_IOCTL_DELAY failed");
+		SYSMSG("SNDRV_PCM_IOCTL_DELAY failed");
 		return err;
 	}
 	return 0;
@@ -505,8 +491,7 @@
 		} else {
 			if (ioctl(fd, SNDRV_PCM_IOCTL_HWSYNC) < 0) {
 				err = -errno;
-				if (check_std_error(err))
-					SYSERR("SNDRV_PCM_IOCTL_HWSYNC failed");
+				SYSMSG("SNDRV_PCM_IOCTL_HWSYNC failed");
 				return err;
 			}
 		}
@@ -532,7 +517,7 @@
 	int fd = hw->fd, err;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_PREPARE) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_PREPARE failed");
+		SYSMSG("SNDRV_PCM_IOCTL_PREPARE failed");
 		return err;
 	}
 	return sync_ptr(hw, SNDRV_PCM_SYNC_PTR_APPL);
@@ -544,7 +529,7 @@
 	int fd = hw->fd, err;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_RESET) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_RESET failed");
+		SYSMSG("SNDRV_PCM_IOCTL_RESET failed");
 		return err;
 	}
 	return sync_ptr(hw, SNDRV_PCM_SYNC_PTR_APPL);
@@ -560,7 +545,7 @@
 #endif
 	if (ioctl(hw->fd, SNDRV_PCM_IOCTL_START) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_START failed");
+		SYSMSG("SNDRV_PCM_IOCTL_START failed");
 #if 0
 		if (err == -EBADFD)
 			SNDERR("PCM state = %s", snd_pcm_state_name(snd_pcm_hw_state(pcm)));
@@ -576,7 +561,7 @@
 	int err;
 	if (ioctl(hw->fd, SNDRV_PCM_IOCTL_DROP) < 0) {
 		err = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_DROP failed");
+		SYSMSG("SNDRV_PCM_IOCTL_DROP failed");
 		return err;
 	}
 	return 0;
@@ -588,8 +573,7 @@
 	int err;
 	if (ioctl(hw->fd, SNDRV_PCM_IOCTL_DRAIN) < 0) {
 		err = -errno;
-		if (check_std_error(err))
-			SYSERR("SNDRV_PCM_IOCTL_DRAIN failed");
+		SYSMSG("SNDRV_PCM_IOCTL_DRAIN failed");
 		return err;
 	}
 	return 0;
@@ -601,8 +585,7 @@
 	int err;
 	if (ioctl(hw->fd, SNDRV_PCM_IOCTL_PAUSE, enable) < 0) {
 		err = -errno;
-		if (check_std_error(err))
-			SYSERR("SNDRV_PCM_IOCTL_PAUSE failed");
+		SYSMSG("SNDRV_PCM_IOCTL_PAUSE failed");
 		return err;
 	}
 	return 0;
@@ -614,8 +597,7 @@
 	int err;
 	if (ioctl(hw->fd, SNDRV_PCM_IOCTL_REWIND, &frames) < 0) {
 		err = -errno;
-		if (check_std_error(err))
-			SYSERR("SNDRV_PCM_IOCTL_REWIND failed");
+		SYSMSG("SNDRV_PCM_IOCTL_REWIND failed");
 		return err;
 	}
 	return frames;
@@ -628,8 +610,7 @@
 	if (SNDRV_PROTOCOL_VERSION(2, 0, 4) <= hw->version) {
 		if (ioctl(hw->fd, SNDRV_PCM_IOCTL_FORWARD, &frames) < 0) {
 			err = -errno;
-			if (check_std_error(err))
-				SYSERR("SNDRV_PCM_IOCTL_FORWARD failed");
+			SYSMSG("SNDRV_PCM_IOCTL_FORWARD failed");
 			return err;
 		}
 		return frames;
@@ -669,8 +650,7 @@
 	int fd = hw->fd, err;
 	if (ioctl(fd, SNDRV_PCM_IOCTL_RESUME) < 0) {
 		err = -errno;
-		if (check_std_error(err))
-			SYSERR("SNDRV_PCM_IOCTL_RESUME failed");
+		SYSMSG("SNDRV_PCM_IOCTL_RESUME failed");
 		return err;
 	}
 	return 0;
@@ -768,7 +748,7 @@
 		err = ioctl(hw->fd, SNDRV_PCM_IOCTL_SYNC_PTR, &sync_ptr);
 		if (err < 0) {
 			err = -errno;
-			SYSERR("SNDRV_PCM_IOCTL_SYNC_PTR failed");
+			SYSMSG("SNDRV_PCM_IOCTL_SYNC_PTR failed");
 			return err;
 		}
 		hw->sync_ptr = calloc(1, sizeof(struct sndrv_pcm_sync_ptr));
@@ -795,7 +775,7 @@
 			   hw->fd, SNDRV_PCM_MMAP_OFFSET_CONTROL);
 		if (ptr == MAP_FAILED || ptr == NULL) {
 			err = -errno;
-			SYSERR("control mmap failed");
+			SYSMSG("control mmap failed");
 			return err;
 		}
 		hw->mmap_control = ptr;
@@ -818,7 +798,7 @@
 	} else {
 		if (munmap((void*)hw->mmap_status, page_align(sizeof(*hw->mmap_status))) < 0) {
 			err = -errno;
-			SYSERR("status munmap failed");
+			SYSMSG("status munmap failed");
 			return err;
 		}
 	}
@@ -837,7 +817,7 @@
 	} else {
 		if (munmap(hw->mmap_control, page_align(sizeof(*hw->mmap_control))) < 0) {
 			err = -errno;
-			SYSERR("control munmap failed");
+			SYSMSG("control munmap failed");
 			return err;
 		}
 	}
@@ -860,7 +840,7 @@
 	int err;
 	if (close(hw->fd)) {
 		err = -errno;
-		SYSERR("close failed\n");
+		SYSMSG("close failed\n");
 		return err;
 	}
 	snd_pcm_hw_munmap_status(pcm);
@@ -888,7 +868,6 @@
 			} while (size > 0);
 			return result;
 		} else {
-			snd_pcm_hw_t *hw = pcm->private_data;
 			assert(hw->shadow_appl_ptr);
 		}
 	}
@@ -917,7 +896,8 @@
 			hw->avail_update_flag = 0;
 			if (err < 0)
 				return err;
-			assert((snd_pcm_uframes_t)err == avail);
+			if ((snd_pcm_uframes_t)err != avail)
+				SNDMSG("short read %ld for avail %ld", err, avail);
 			return err;
 		}
 	}
@@ -946,7 +926,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);
@@ -1021,7 +1004,7 @@
 	memset(&info, 0, sizeof(info));
 	if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info) < 0) {
 		ret = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_INFO failed");
+		SYSMSG("SNDRV_PCM_IOCTL_INFO failed");
 		close(fd);
 		return ret;
 
@@ -1044,7 +1027,7 @@
 	 */
 	if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
 		ret = -errno;
-		SYSERR("fcntl FD_CLOEXEC failed");
+		SYSMSG("fcntl FD_CLOEXEC failed");
 		close(fd);
 		return ret;
 	}
@@ -1052,7 +1035,7 @@
 
 	if (ioctl(fd, SNDRV_PCM_IOCTL_PVERSION, &ver) < 0) {
 		ret = -errno;
-		SYSERR("SNDRV_PCM_IOCTL_PVERSION failed");
+		SYSMSG("SNDRV_PCM_IOCTL_PVERSION failed");
 		close(fd);
 		return ret;
 	}
@@ -1063,7 +1046,7 @@
 		int on = 1;
 		if (ioctl(fd, SNDRV_PCM_IOCTL_TSTAMP, &on) < 0) {
 			ret = -errno;
-			SNDERR("TSTAMP failed\n");
+			SNDMSG("TSTAMP failed\n");
 			return ret;
 		}			
 	}
@@ -1152,7 +1135,8 @@
 		filefmt = SNDRV_FILE_PCM_STREAM_CAPTURE;
 		break;
 	default:
-		assert(0);
+		SNDERR("invalid stream %d", stream);
+		return -EINVAL;
 	}
 	sprintf(filename, filefmt, card, device);
 
@@ -1171,14 +1155,14 @@
 		fmode |= O_ASYNC;
 	if ((fd = open(filename, fmode)) < 0) {
 		ret = -errno;
-		SYSERR("open %s failed", filename);
+		SYSMSG("open %s failed", filename);
 		goto _err;
 	}
 	if (subdevice >= 0) {
 		memset(&info, 0, sizeof(info));
 		if (ioctl(fd, SNDRV_PCM_IOCTL_INFO, &info) < 0) {
 			ret = -errno;
-			SYSERR("SNDRV_PCM_IOCTL_INFO failed");
+			SYSMSG("SNDRV_PCM_IOCTL_INFO failed");
 			goto _err;
 		}
 		if (info.subdevice != (unsigned int) subdevice) {
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	9 Dec 2004 16:09:59 -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) {
+		SNDMSG("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) {
+		SNDMSG("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;
+		SNDMSG("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 (CHECK_SANITY(! pcm->setup)) {
+		SNDMSG("PCM not set up");
+		return -EIO;
+	}
+	if (CHECK_SANITY(pcm->mmap_channels || pcm->running_areas)) {
+		SNDMSG("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 (CHECK_SANITY(! pcm->mmap_channels)) {
+		SNDMSG("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;
+			SNDMSG("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;
+			SNDMSG("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	9 Dec 2004 16:14:41 -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 (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
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	9 Dec 2004 16:12:50 -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 (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
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	9 Dec 2004 16:13:57 -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) {
+				SYSMSG("snd_pcm_mmap_commit error");
+				return INT_MAX;
+			}
+			if (err != frames)
+				SYSMSG("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) {
+				SYSMSG("snd_pcm_mmap_commit error");
+				return err;
+			}
+			if (err != frames) {
+				SYSMSG("commit returns %ld for size %ld", err, frames);
+				return err;
+			}
 		}
 		_snd_pcm_share_update(pcm);
 	}

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-09 16:23       ` Takashi Iwai
@ 2004-12-10  5:46         ` Glenn Maynard
  2004-12-10  7:49           ` Jaroslav Kysela
  0 siblings, 1 reply; 8+ messages in thread
From: Glenn Maynard @ 2004-12-10  5:46 UTC (permalink / raw)
  To: alsa-devel

On Thu, Dec 09, 2004 at 05:23:06PM +0100, Takashi Iwai wrote:
> +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.

-- 
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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-10  5:46         ` Glenn Maynard
@ 2004-12-10  7:49           ` Jaroslav Kysela
  2004-12-10 11:03             ` Takashi Iwai
  0 siblings, 1 reply; 8+ messages in thread
From: Jaroslav Kysela @ 2004-12-10  7:49 UTC (permalink / raw)
  To: Glenn Maynard; +Cc: alsa-devel

On Fri, 10 Dec 2004, Glenn Maynard wrote:

> On Thu, Dec 09, 2004 at 05:23:06PM +0100, Takashi Iwai wrote:
> > +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 <perex@suse.cz>
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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Removal of assert() in alsa-lib
  2004-12-10  7:49           ` Jaroslav Kysela
@ 2004-12-10 11:03             ` Takashi Iwai
  0 siblings, 0 replies; 8+ messages in thread
From: Takashi Iwai @ 2004-12-10 11:03 UTC (permalink / raw)
  To: Jaroslav Kysela; +Cc: Glenn Maynard, alsa-devel

At Fri, 10 Dec 2004 08:49:36 +0100 (CET),
Jaroslav wrote:
> 
> On Fri, 10 Dec 2004, Glenn Maynard wrote:
> 
> > On Thu, Dec 09, 2004 at 05:23:06PM +0100, Takashi Iwai wrote:
> > > +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/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-12-10 11:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-01 16:31 Removal of assert() in alsa-lib Takashi Iwai
2004-12-01 17:51 ` Jaroslav Kysela
2004-12-01 18:01   ` Takashi Iwai
2004-12-02 12:03     ` Takashi Iwai
2004-12-09 16:23       ` Takashi Iwai
2004-12-10  5:46         ` Glenn Maynard
2004-12-10  7:49           ` Jaroslav Kysela
2004-12-10 11:03             ` Takashi Iwai

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.