Skip to contentSkip to conversation

ALSA: mixer_oss: Replace deprecated strcpy() with strscpy()

PATCH In review Thorsten Blum 3 participants 5 messages Jun 18, 2025 – Jun 23, 2025 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: mixer_oss: Replace deprecated strcpy() with strscpy()

strcpy() is deprecated; use strscpy() instead.

No functional changes intended.

---
 sound/core/oss/mixer_oss.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
sound/core/oss/mixer_oss.c +3 -3
index 05fc8911479c..d3d993da3269 100644
@@ -1014,11 +1014,11 @@ static int snd_mixer_oss_build_input(struct snd_mixer_oss *mixer,
 			
 		if (kctl->info(kctl, uinfo))
 			return 0;
-		strcpy(str, ptr->name);
+		strscpy(str, ptr->name);
 		if (!strcmp(str, "Master"))
-			strcpy(str, "Mix");
+			strscpy(str, "Mix");
 		if (!strcmp(str, "Master Mono"))
-			strcpy(str, "Mix Mono");
+			strscpy(str, "Mix Mono");
 		slot.capture_item = 0;
 		if (!strcmp(uinfo->value.enumerated.name, str)) {
 			slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;
Signature · 1 line
2.49.0
#1 lore raw
Quoted 3 lines · Thorsten Blum

strcpy() is deprecated; use strscpy() instead.

No functional changes intended.

Have you actually read the damn thing? Seriously, look at the uses of 'str' downstream. The only thing it is ever passed to is strcmp().

In other words, why do we need to copy it anywhere? What's wrong with having char *str instead of that array and replacing strcpy() with plain and simple pointer assignment?

#2 lore raw
Quoted 11 lines · Al Viro
Quoted 3 lines · Thorsten Blum

strcpy() is deprecated; use strscpy() instead.

No functional changes intended.

Have you actually read the damn thing? Seriously, look at the uses of 'str' downstream. The only thing it is ever passed to is strcmp().

In other words, why do we need to copy it anywhere? What's wrong with having char *str instead of that array and replacing strcpy() with plain and simple pointer assignment?

I read it, but didn't question whether copying was actually necessary.

However, it looks like 'ptr->name' can originate from userland (via proc file - see the function comment), which could make using 'char *str' directly unsafe, unless I'm missing something.

Something like this would skip one copy while keeping it safe:

char tmp_str[64];
char *str;

strscpy(tmp_str, ptr->name);
if (!strcmp(tmp_str, "Master"))

	str = "Mix";
else if (!strcmp(tmp_str, "Master Mono"))
	str = "Mix Mono";
else
	str = tmp_str;

Thanks,
Thorsten

#3 lore raw
Quoted 25 lines · Thorsten Blum
Quoted 11 lines · Al Viro
Quoted 3 lines · Thorsten Blum

strcpy() is deprecated; use strscpy() instead.

No functional changes intended.

Have you actually read the damn thing? Seriously, look at the uses of 'str' downstream. The only thing it is ever passed to is strcmp().

In other words, why do we need to copy it anywhere? What's wrong with having char *str instead of that array and replacing strcpy() with plain and simple pointer assignment?

I read it, but didn't question whether copying was actually necessary.

However, it looks like 'ptr->name' can originate from userland (via proc file - see the function comment), which could make using 'char *str' directly unsafe, unless I'm missing something.

Something like this would skip one copy while keeping it safe:

char tmp_str[64];
char *str;

Quoted 7 lines

strscpy(tmp_str, ptr->name);
if (!strcmp(tmp_str, "Master"))

	str = "Mix";
else if (!strcmp(tmp_str, "Master Mono"))
	str = "Mix Mono";
else
	str = tmp_str;

Al is right, we should optimize it instead. As it's been already a string copied to a kernel, and the string is certainly NUL-terminated, hence there is no need to worry about using the pointer. It'd be something like:

sound/core/oss/mixer_oss.c +5 -5
@@ -991,7 +991,7 @@ static int snd_mixer_oss_build_input(struct snd_mixer_oss *mixer,
 	struct slot *pslot;
 	struct snd_kcontrol *kctl;
 	struct snd_mixer_oss_slot *rslot;
-	char str[64];	
+	const char *str;
 	
 	/* check if already assigned */
 	if (mixer->slots[ptr->oss_id].get_volume && ! replace_old)
@@ -1014,11 +1014,11 @@ static int snd_mixer_oss_build_input(struct snd_mixer_oss *mixer,
 			
 		if (kctl->info(kctl, uinfo))
 			return 0;
-		strcpy(str, ptr->name);
+		str = ptr->name;
 		if (!strcmp(str, "Master"))
-			strcpy(str, "Mix");
-		if (!strcmp(str, "Master Mono"))
-			strcpy(str, "Mix Mono");
+			str = "Mix";
+		else if (!strcmp(str, "Master Mono"))
+			str = "Mix Mono";
 		slot.capture_item = 0;
 		if (!strcmp(uinfo->value.enumerated.name, str)) {
 			slot.present |= SNDRV_MIXER_OSS_PRESENT_CAPTURE;

thanks,

Takashi

#4 lore raw
Quoted 3 lines · Takashi Iwai

Al is right, we should optimize it instead. As it's been already a string copied to a kernel, and the string is certainly NUL-terminated, hence there is no need to worry about using the pointer.

Thanks - I'll submit a v2.