All of lore.kernel.org
 help / color / mirror / Atom feed
From: Thorsten Blum <thorsten.blum@linux.dev>
To: Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>
Cc: Thorsten Blum <thorsten.blum@linux.dev>,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: jack: Improve string handling in jack_kctl_name_gen
Date: Sun, 25 Jan 2026 16:51:56 +0100	[thread overview]
Message-ID: <20260125155159.98720-1-thorsten.blum@linux.dev> (raw)

If appending " Jack" is not necessary, replace snprintf("%s", ...) with
the faster strscpy().

Additionally, rename 'need_cat' to the clearer 'append_suf', use local
variables for the suffix and its length, remove the confusing comment,
compare strncmp() to 0, and use 'size_t' for the 'size' function
parameter.

Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
 sound/core/ctljack.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/sound/core/ctljack.c b/sound/core/ctljack.c
index 709b1a9c2caa..6b6ab34fbde8 100644
--- a/sound/core/ctljack.c
+++ b/sound/core/ctljack.c
@@ -7,6 +7,7 @@
 
 #include <linux/kernel.h>
 #include <linux/export.h>
+#include <linux/string.h>
 #include <sound/core.h>
 #include <sound/control.h>
 
@@ -46,17 +47,20 @@ static int get_available_index(struct snd_card *card, const char *name)
 	return sid.index;
 }
 
-static void jack_kctl_name_gen(char *name, const char *src_name, int size)
+static void jack_kctl_name_gen(char *name, const char *src_name, size_t size)
 {
 	size_t count = strlen(src_name);
-	bool need_cat = true;
+	const char *suf = " Jack";
+	size_t suf_len = strlen(suf);
+	bool append_suf = true;
 
-	/* remove redundant " Jack" from src_name */
-	if (count >= 5)
-		need_cat = strncmp(&src_name[count - 5], " Jack", 5) ? true : false;
-
-	snprintf(name, size, need_cat ? "%s Jack" : "%s", src_name);
+	if (count >= suf_len)
+		append_suf = strncmp(&src_name[count - suf_len], suf, suf_len) != 0;
 
+	if (append_suf)
+		snprintf(name, size, "%s%s", src_name, suf);
+	else
+		strscpy(name, src_name, size);
 }
 
 struct snd_kcontrol *
-- 
Thorsten Blum <thorsten.blum@linux.dev>
GPG: 1D60 735E 8AEF 3BE4 73B6  9D84 7336 78FD 8DFE EAD4


             reply	other threads:[~2026-01-25 15:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-25 15:51 Thorsten Blum [this message]
2026-01-26  8:33 ` [PATCH] ALSA: jack: Improve string handling in jack_kctl_name_gen Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260125155159.98720-1-thorsten.blum@linux.dev \
    --to=thorsten.blum@linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.