git: 3deae7b78674 - main - sound: Detect unsupported formats

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Sat, 21 Feb 2026 11:37:07 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=3deae7b786743193531f4e69527e789de52644c5

commit 3deae7b786743193531f4e69527e789de52644c5
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-02-21 11:36:58 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-02-21 11:36:58 +0000

    sound: Detect unsupported formats
    
    This way we can avoid edge-cases like
    8af6aee96ed609456900c6dd92dafabac5e89c0a ("virtual_oss(8): Remove
    floating point formats from preference list").
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Reviewed by:    emaste
    Differential Revision:  https://reviews.freebsd.org/D55403
---
 sys/dev/sound/pcm/channel.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/sys/dev/sound/pcm/channel.c b/sys/dev/sound/pcm/channel.c
index 011dc1427c2e..b74f76fd21ca 100644
--- a/sys/dev/sound/pcm/channel.c
+++ b/sys/dev/sound/pcm/channel.c
@@ -2092,9 +2092,23 @@ chn_setspeed(struct pcm_channel *c, uint32_t speed)
 int
 chn_setformat(struct pcm_channel *c, uint32_t format)
 {
-	uint32_t oldformat, oldspeed;
+	uint32_t oldformat, oldspeed, x;
 	int ret;
 
+	/*
+	 * Detect unsupported formats. This checks if the format is supported
+	 * in the first place, and that no more than 1 format is specified at
+	 * the same time.
+	 */
+	x = format & AFMT_CONVERTIBLE;
+	if ((x & (x - 1)) != 0) {
+		if (snd_verbose > 3) {
+			device_printf(c->dev, "%s(): Unsupported format: "
+			    "0x%08x\n", __func__, format);
+		}
+		return (EINVAL);
+	}
+
 	/* XXX force stereo */
 	if ((format & AFMT_PASSTHROUGH) && AFMT_CHANNEL(format) < 2) {
 		format = SND_FORMAT(format, AFMT_PASSTHROUGH_CHANNEL,