Bug 29964

Summary: Setting iotcl SNDCTL_DSP_CHANNELS to an unaccepted value sets it to another unaccepted value
Product: Base System Reporter: AnarCat <AnarCat>
Component: kernAssignee: sound
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description AnarCat 2001-08-22 21:00:01 UTC
Using a test program (rec:
http://anarcat.dyndns.org/ftp/pub/FreeBSD/local/src/rec.tar.gz), setting
the number of read channels to 4 on a soundcard that supports only 2 (my
soundblaster) sets the number of channels to 0, which is non-sensical.

Fix: I tried to dig around in the newpcm driver a bit, but it's a bit harsh
for me, as I have no knowledge of FreeBSD kernel drivers internals.

The SNDCTL_DSP_CHANNELS (or SOUND_PCM_WRITE_CHANNELS) ioctl is handled
in:  $FreeBSD: src/sys/dev/sound/pcm/dsp.c,v 1.15.2.9 2001/08/15
00:34:59 cg Exp $, line 673. Now there, a few things happen that i do
not understand.

Problem 1:
		if (*arg_i == 1 || *arg_i == 2) {
                    ...
		} else
			*arg_i = 0;

That means that we do not support channel counts != 1 or 2, right? Then
we should instead change *arg_i to a sane value, *not* 0!

Fix 1: if arg_i is not 1 or 2, make it 2.



Problem 2:

Now this might be the core of the problem, and obviously, the most
uncomprehensible. :)

From what I can understand here, 

                tmp = 0;
                *arg_i = (*arg_i == 2)? AFMT_STEREO : 0;
                if (wrch) {
			CHN_LOCK(wrch);
			ret = chn_setformat(wrch, (wrch->format &
~AFMT_STEREO) | *arg_i);
			tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
			CHN_UNLOCK(wrch);
		}
		if (rdch && ret == 0) {
			CHN_LOCK(rdch);
			ret = chn_setformat(rdch, (rdch->format &
~AFMT_STEREO) | *arg_i);
			if (tmp == 0)
				tmp = (rdch->format & AFMT_STEREO)? 2 :
1;
			CHN_UNLOCK(rdch);
		}
		*arg_i = tmp;

if !wrch, then the second if will rely on an undefine value of ret. If
that value is not 0, the second if will not be executed, and therefore
tmp = *arg_i will be 0!

Shouldn't ret be initialized (to 0) at the beginning of the case? I
think there are many of these around the switch, so I don't know if I'm
wrong here.

So my "fix" here would be to add ret = 0; at the beginning of the case.--5gECbrTjDaKeAcHNdaNeCDx7nSiI3UaaeNsWswY1d21dDqS2
Content-Type: text/plain; name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="file.diff"

--- /sys/dev/sound/pcm/dsp.c	Tue Aug 21 13:14:47 2001
+++ /home/anarcat/dsp.c.new	Wed Aug 22 15:31:12 2001
@@ -672,25 +672,25 @@
 
     	case SOUND_PCM_WRITE_CHANNELS:
 /*	case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
-		if (*arg_i == 1 || *arg_i == 2) {
-			tmp = 0;
-			*arg_i = (*arg_i == 2)? AFMT_STEREO : 0;
-	  		if (wrch) {
-				CHN_LOCK(wrch);
-				ret = chn_setformat(wrch, (wrch->format
 				& ~AFMT_STEREO) | *arg_i);
-				tmp = (wrch->format & AFMT_STEREO)? 2 :
 				1;
-				CHN_UNLOCK(wrch);
-			}
-			if (rdch && ret == 0) {
-				CHN_LOCK(rdch);
-				ret = chn_setformat(rdch, (rdch->format
 				& ~AFMT_STEREO) | *arg_i);
-				if (tmp == 0)
-					tmp = (rdch->format &
 					AFMT_STEREO)? 2 : 1;
-				CHN_UNLOCK(rdch);
-			}
-			*arg_i = tmp;
-		} else
-			*arg_i = 0;
+		if (*arg_i != 1 && *arg_i != 2) {
+                        *arg_i = 2;
+                }
+                tmp = 0;
+                *arg_i = (*arg_i == 2)? AFMT_STEREO : 0;
+                if (wrch) {
+			CHN_LOCK(wrch);
+			ret = chn_setformat(wrch, (wrch->format &
~AFMT_STEREO) | *arg_i);
+			tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
+			CHN_UNLOCK(wrch);
+		}
+		if (rdch && ret == 0) {
+			CHN_LOCK(rdch);
+			ret = chn_setformat(rdch, (rdch->format &
~AFMT_STEREO) | *arg_i);
+			if (tmp == 0)
+				tmp = (rdch->format & AFMT_STEREO)? 2 :
1;
+			CHN_UNLOCK(rdch);
+		}
+		*arg_i = tmp;
 		break;
 
     	case SOUND_PCM_READ_CHANNELS:
How-To-Repeat: 
anarcat@shall [rec]$ ./rec -c 4 test.raw
recording until INT (control-c).
number of channels (4) not supported, falling back to 0
Device: /dev/dsp
Output format: 44100 Hz, 0 channels, 16 bits signed little-endian
Floating point exception (core dumped)
Comment 1 greid freebsd_committer freebsd_triage 2001-08-27 19:34:17 UTC
State Changed
From-To: open->closed

Fix committed (r 1.47 of dsp.c), thanks! 


Comment 2 greid freebsd_committer freebsd_triage 2001-08-27 19:34:17 UTC
Responsible Changed
From-To: freebsd-bugs->sound

Sound issue