Skip to contentSkip to conversation

ALSA: control: Fix parsing of numeric fields

PATCH Superseded Sreeraj S Kurup 4 participants 17 messages Nov 6 – Nov 11 Sound

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: control: Fix parsing of numeric fields

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

---
 sound/core/control_led.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
sound/core/control_led.c +13 -5
index 501387287ae6..3f54af2cb505 100644
@@ -458,11 +458,19 @@ static char *find_eos(char *s)
 
 static char *parse_uint(char *s, unsigned int *val)
 {
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = s;
+	char delimiter;
+
+	while (*end && *end != ',')
+		end++;
+
+	delimiter = *end;
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+
+	return delimiter ? end + 1 : end;
 }
 
 static char *parse_string(char *s, char *val, size_t val_size)
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ALSA: control: missing Fixes trailer for parse_uint() bug fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ALSA: control: missing Fixes trailer for parse_uint() bug fix --

commit 1bd2d3e81b83fc5553191929745f26253824ba8d
Author: Sreeraj S Kurup <[email protected]>

Quoted 14 lines

ALSA: control: Fix parsing of numeric fields

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Quoted 7 lines

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

[Severity: Low]
This fixes a bug in the sysfs kcontrol LED parser. Should it include:

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

PATCH v2 1/1 ALSA: control: Fix parsing of numeric fields

In reply to Sreeraj S Kurup

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")

---
v2:
- Add the Fixes trailer.

 sound/core/control_led.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
sound/core/control_led.c +13 -5
index 501387287ae6..3f54af2cb505 100644
@@ -458,11 +458,19 @@ static char *find_eos(char *s)
 
 static char *parse_uint(char *s, unsigned int *val)
 {
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = s;
+	char delimiter;
+
+	while (*end && *end != ',')
+		end++;
+
+	delimiter = *end;
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+
+	return delimiter ? end + 1 : end;
 }
 
 static char *parse_string(char *s, char *val, size_t val_size)
Signature · 1 line
2.55.0
#3 lore raw

Hi Takashi,

I looked into using simple_strtoul(), but noticed that it is deprecated. kstrtoul() does not provide an end pointer either.

Would it be reasonable to retain the delimiter-first approach with kstrtouint(), as in v2?

Thanks,
Sreeraj

#4 lore raw
Quoted 5 lines · Sreeraj S Kurup

Hi Takashi,

I looked into using simple_strtoul(), but noticed that it is deprecated. kstrtoul() does not provide an end pointer either.

Yes, that's also my concern about that approach.

Quoted 2 lines

Would it be reasonable to retain the delimiter-first approach with kstrtouint(), as in v2?

I depends on the result :) We can compare.

thanks,

Takashi

#5 lore raw

Hi Takashi,

I compared the approaches.

Using simple_strtoul() would provide an end pointer, but it is deprecated.

Another option is to retain kstrtoull() from the original implementation and apply the delimiter-first approach. This would minimize changes to the existing conversion logic.

However, since the destination is an unsigned int, kstrtouint() better matches the destination type and also detects values outside the unsigned int range.

I currently prefer the v2 approach, but wanted to point out this difference in overflow handling.

Thanks,
Sreeraj

#6 lore raw
Quoted 11 lines · Sreeraj S Kurup

Hi Takashi,

I compared the approaches.

Using simple_strtoul() would provide an end pointer, but it is deprecated.

Another option is to retain kstrtoull() from the original implementation and apply the delimiter-first approach. This would minimize changes to the existing conversion logic.

Quoted 5 lines

However, since the destination is an unsigned int, kstrtouint() better matches the destination type and also detects values outside the unsigned int range.

I currently prefer the v2 approach, but wanted to point out this difference in overflow handling.

OK, thanks. Feel free to resubmit whatever you find better :)

Takashi

#8 lore raw

PATCH v3 1/1 ALSA: control: Fix parsing of numeric fields

In reply to Takashi Iwai

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")

---
v3:
- No code changes. Retain the v2 implementation after comparing the alternative approaches.
v2:
- Add the Fixes trailer.

 sound/core/control_led.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
sound/core/control_led.c +13 -5
index 501387287ae6..3f54af2cb505 100644
@@ -458,11 +458,19 @@ static char *find_eos(char *s)
 
 static char *parse_uint(char *s, unsigned int *val)
 {
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = s;
+	char delimiter;
+
+	while (*end && *end != ',')
+		end++;
+
+	delimiter = *end;
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+
+	return delimiter ? end + 1 : end;
 }
 
 static char *parse_string(char *s, char *val, size_t val_size)
Signature · 1 line
2.55.0
Reviewed-by on v3 1/1
#9 lore raw
Quoted 12 lines · Sreeraj S Kurup

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Quoted 8 lines

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")
Reviewed-by Manuel Ebner
Quoted 39 lines

---
v3:
 - No code changes. Retain the v2 implementation after comparing the alternative approaches.
v2:
 - Add the Fixes trailer.

 sound/core/control_led.c | 18 +++++++++++++-----

 1 file changed, 13 insertions(+), 5 deletions(-)
sound/core/control_led.c +0 -0
index 501387287ae6..3f54af2cb505 100644
@@ -458,11 +458,19 @@ static char *find_eos(char *s)


 static char *parse_uint(char *s, unsigned int *val) {

sound/core/control_led.c +13 -5
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = s;
+	char delimiter;
+
+	while (*end && *end != ',')
+		end++;
+
+	delimiter = *end;
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+
+	return delimiter ? end + 1 : end;

 }

 static char *parse_string(char *s, char *val, size_t val_size)

#10 lore raw
In reply to Sreeraj S Kurup
Quoted 19 lines · Sreeraj S Kurup

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

Quoted 8 lines
Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")

---
v3:
- No code changes. Retain the v2 implementation after comparing the alternative approaches.
v2:
- Add the Fixes trailer.

I think a bit more easier to understand the code flow would to make each parse_*() function returning the substring end position, instead of the next substring. (BTW, this fits more with the function name find_eos(). too).

The main loop in set_led_id() already treats it,

	while (*s) {
		os = s;	
		....
		if (*s == ',')
			s++;
		if (s == os)
			break;
	}

Both parse_uint() and parse_iface() return find_eos() value as is. parse_string() has an extra increment for the next substring, so we just need to drop it.

That is, what we need to change is something like:

sound/core/control_led.c +9 -9
@@ -451,22 +451,24 @@ static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
 	NULL,
 };
 
+/* return the end of substring pointing to either comma or NUL */
 static char *find_eos(char *s)
 {
 	while (*s && *s != ',')
 		s++;
-	if (*s)
-		s++;
 	return s;
 }
 
 static char *parse_uint(char *s, unsigned int *val)
 {
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = find_eos(s);
+	char delimiter = *end;
+
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+	return end;
 }
 
 static char *parse_string(char *s, char *val, size_t val_size)
@@ -491,8 +493,6 @@ static char *parse_string(char *s, char *val, size_t val_size)
 		}
 	}
 	*val = '\0';
-	if (*s)
-		s++;
 	return s;
 }
 

thanks,

Takashi

#11 lore raw

Hi Takashi,

Thanks for the suggestion.

I've updated the patch accordingly. All parse_*() functions now return the end of the current substring, leaving comma handling to set_led_id().

I've also updated parse_uint() to temporarily terminate the numeric substring before calling kstrtouint().

Please find the updated v4 patch.

Thanks,
Sreeraj

#12 lore raw

PATCH v4 1/1 ALSA: control: Fix parsing of numeric fields

In reply to Sreeraj S Kurup

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

Make all parse_*() functions return the end of the current substring, leaving delimiter handling to set_led_id(). Temporarily terminate the numeric substring in parse_uint() before calling kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")

---
v4:
- Make parse_*() functions consistently return the end of the current substring.
- Let set_led_id() handle comma advancement.
- Use kstrtouint() after temporarily terminating the numeric substring.
v3:
- No code changes. Retain the v2 implementation after comparing the alternative approaches.
v2:
- Add the Fixes trailer.

 sound/core/control_led.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
sound/core/control_led.c +8 -9
index 501387287ae6..1b0473b71c3c 100644
@@ -447,22 +447,23 @@ static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
 	NULL,
 };
 
+/* return the end of substring pointing to either comma or NUL */
 static char *find_eos(char *s)
 {
 	while (*s && *s != ',')
 		s++;
-	if (*s)
-		s++;
 	return s;
 }
 
 static char *parse_uint(char *s, unsigned int *val)
 {
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = find_eos(s);
+	char delimiter = *end;
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+	return end;
 }
 
 static char *parse_string(char *s, char *val, size_t val_size)
@@ -487,8 +488,6 @@ static char *parse_string(char *s, char *val, size_t val_size)
 		}
 	}
 	*val = '\0';
-	if (*s)
-		s++;
 	return s;
 }
 
Signature · 1 line
2.55.0
#13 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] ALSA: control_led: unconsumed closing quote in parse_string() stops parsing --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] ALSA: control_led: unconsumed closing quote in parse_string() stops parsing
--

commit ca72582941d6fb9701c15b50af83f1b948e9bd29
Author: Sreeraj S Kurup <[email protected]>

ALSA: control: Fix parsing of numeric fields

This commit updates find_eos(), parse_uint(), and parse_string() so they return the end of the current substring rather than advancing past the delimiter, and NUL-terminates the numeric substring before calling kstrtouint().

Quoted 4 lines
sound/core/control_led.c +0 -0
index 3d55da3af2a05..8cfd305fc1d2a 100644

[ ... ]

sound/core/control_led.c static char *parse_string(char *s, char *val, size_t val_size) · line 496

Quoted 8 lines
sound/core/control_led.c +0 -2
@@ -491,8 +492,6 @@ static char *parse_string(char *s, char *val, size_t val_size)
 		}
 	}
 	*val = '\0';
-	if (*s)
-		s++;
 	return s;
 }

[Severity: Medium]
Does removing "if (*s) s++;" at the end of parse_string() leave s pointing at the closing quote when parsing a quoted string?

In the quoted-string branch, the loop stops when *s matches the opening quote character:

sound/core/control_led.c:parse_string() {
	if (*s == '"' || *s == '\'') {
		char c = *s;
		s++;
		while (*s && *s != c) {
			...
			s++;
		}
	} else {

...

Previously, the trailing "if (*s) s++;" stepped past the closing quote so that s pointed at the following comma (or NUL), and set_led_id() then stepped past that comma. With it removed, a quoted name followed by another field (such as "name='Master Playback Switch',index=1") returns a pointer to the closing quote, causing set_led_id() to miss the comma and break out of its loop without parsing any remaining fields. Advancing past the closing quote inside the quoted-string branch would preserve the expected return position.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#14 lore raw

PATCH v5 1/1 ALSA: control: Fix parsing of numeric fields

In reply to Sreeraj S Kurup

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

Make all parse_*() functions return the end of the current substring, leaving delimiter handling to set_led_id(). Temporarily terminate the numeric substring in parse_uint() before calling kstrtouint().

This allows numeric fields to be parsed correctly in comma-separated input.

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")

---
v5:
- Advance past the closing quote in the quoted-string branch of parse_string().
- Ensure parse_string() returns the end of the current substring for both quoted and unquoted strings.
v4:
- Make parse_*() functions consistently return the end of the current substring.
- Let set_led_id() handle comma advancement.
- Use kstrtouint() after temporarily terminating the numeric substring.
v3:
- No code changes. Retain the v2 implementation after comparing the alternative approaches.
v2:
- Add the Fixes trailer.

 sound/core/control_led.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
sound/core/control_led.c +10 -9
index 501387287ae6..1aadacfbd3c4 100644
@@ -447,22 +447,23 @@ static const struct attribute_group *snd_ctl_led_dev_attr_groups[] = {
 	NULL,
 };
 
+/* return the end of substring pointing to either comma or NUL */
 static char *find_eos(char *s)
 {
 	while (*s && *s != ',')
 		s++;
-	if (*s)
-		s++;
 	return s;
 }
 
 static char *parse_uint(char *s, unsigned int *val)
 {
-	unsigned long long res;
-	if (kstrtoull(s, 10, &res))
-		res = 0;
-	*val = res;
-	return find_eos(s);
+	char *end = find_eos(s);
+	char delimiter = *end;
+	*end = '\0';
+	if (kstrtouint(s, 10, val))
+		*val = 0;
+	*end = delimiter;
+	return end;
 }
 
 static char *parse_string(char *s, char *val, size_t val_size)
@@ -477,6 +478,8 @@ static char *parse_string(char *s, char *val, size_t val_size)
 			}
 			s++;
 		}
+		if (*s)
+			s++;
 	} else {
 		while (*s && *s != ',') {
 			if (val_size > 1) {
@@ -487,8 +490,6 @@ static char *parse_string(char *s, char *val, size_t val_size)
 		}
 	}
 	*val = '\0';
-	if (*s)
-		s++;
 	return s;
 }
 
Signature · 1 line
2.55.0
Applied
#15 lore raw
Quoted 21 lines · Sreeraj S Kurup

parse_uint() passes the remaining input string to kstrtoull().

When the input contains comma-separated fields, the string contains both the numeric value and the following fields.

For example, with:

device=1,subdevice=0,name=Master

kstrtoull() receives "1,subdevice=0,name=Master" and fails to parse the numeric value.

Stop parsing at the next comma and pass only the numeric field to kstrtouint().

Make all parse_*() functions return the end of the current substring, leaving delimiter handling to set_led_id(). Temporarily terminate the numeric substring in parse_uint() before calling kstrtouint().

Quoted 5 lines

This allows numeric fields to be parsed correctly in comma-separated input.

Fixes a135dfb5de15 ("ALSA: led control - add sysfs kcontrol LED marking layer")

Applied to for-next branch now. Thanks.

Takashi