Skip to content

Commit bfa514c

Browse files
tobluxtiwai
authored andcommitted
ALSA: jack: Improve string handling in jack_kctl_name_gen
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> Link: https://patch.msgid.link/20260125155159.98720-1-thorsten.blum@linux.dev Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent cc051fb commit bfa514c

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

sound/core/ctljack.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <linux/kernel.h>
99
#include <linux/export.h>
10+
#include <linux/string.h>
1011
#include <sound/core.h>
1112
#include <sound/control.h>
1213

@@ -46,17 +47,20 @@ static int get_available_index(struct snd_card *card, const char *name)
4647
return sid.index;
4748
}
4849

49-
static void jack_kctl_name_gen(char *name, const char *src_name, int size)
50+
static void jack_kctl_name_gen(char *name, const char *src_name, size_t size)
5051
{
5152
size_t count = strlen(src_name);
52-
bool need_cat = true;
53+
const char *suf = " Jack";
54+
size_t suf_len = strlen(suf);
55+
bool append_suf = true;
5356

54-
/* remove redundant " Jack" from src_name */
55-
if (count >= 5)
56-
need_cat = strncmp(&src_name[count - 5], " Jack", 5) ? true : false;
57-
58-
snprintf(name, size, need_cat ? "%s Jack" : "%s", src_name);
57+
if (count >= suf_len)
58+
append_suf = strncmp(&src_name[count - suf_len], suf, suf_len) != 0;
5959

60+
if (append_suf)
61+
snprintf(name, size, "%s%s", src_name, suf);
62+
else
63+
strscpy(name, src_name, size);
6064
}
6165

6266
struct snd_kcontrol *

0 commit comments

Comments
 (0)