Skip to content

Commit ade7956

Browse files
committed
ALSA: control-led: Replace sprintf() with sysfs_emit()
For sysfs outputs, it's safer to use a new helper, sysfs_emit(), instead of the raw sprintf() & co. This patch replaces such sprintf() calls with sysfs_emit() while simplifying the open code in list_show(). Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 0980bb1 commit ade7956

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

sound/core/control_led.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static ssize_t mode_show(struct device *dev,
405405
case MODE_ON: str = "on"; break;
406406
case MODE_OFF: str = "off"; break;
407407
}
408-
return sprintf(buf, "%s\n", str);
408+
return sysfs_emit(buf, "%s\n", str);
409409
}
410410

411411
static ssize_t mode_store(struct device *dev,
@@ -443,7 +443,7 @@ static ssize_t brightness_show(struct device *dev,
443443
{
444444
struct snd_ctl_led *led = container_of(dev, struct snd_ctl_led, dev);
445445

446-
return sprintf(buf, "%u\n", ledtrig_audio_get(led->trigger_type));
446+
return sysfs_emit(buf, "%u\n", ledtrig_audio_get(led->trigger_type));
447447
}
448448

449449
static DEVICE_ATTR_RW(mode);
@@ -618,32 +618,27 @@ static ssize_t list_show(struct device *dev,
618618
struct snd_ctl_led_card *led_card = container_of(dev, struct snd_ctl_led_card, dev);
619619
struct snd_card *card;
620620
struct snd_ctl_led_ctl *lctl;
621-
char *buf2 = buf;
622-
size_t l;
621+
size_t l = 0;
623622

624623
card = snd_card_ref(led_card->number);
625624
if (!card)
626625
return -ENXIO;
627626
down_read(&card->controls_rwsem);
628627
mutex_lock(&snd_ctl_led_mutex);
629628
if (snd_ctl_led_card_valid[led_card->number]) {
630-
list_for_each_entry(lctl, &led_card->led->controls, list)
631-
if (lctl->card == card) {
632-
if (buf2 - buf > PAGE_SIZE - 16)
633-
break;
634-
if (buf2 != buf)
635-
*buf2++ = ' ';
636-
l = scnprintf(buf2, 15, "%u",
637-
lctl->kctl->id.numid +
638-
lctl->index_offset);
639-
buf2[l] = '\0';
640-
buf2 += l + 1;
641-
}
629+
list_for_each_entry(lctl, &led_card->led->controls, list) {
630+
if (lctl->card != card)
631+
continue;
632+
if (l)
633+
l += sysfs_emit_at(buf, l, " ");
634+
l += sysfs_emit_at(buf, l, "%u",
635+
lctl->kctl->id.numid + lctl->index_offset);
636+
}
642637
}
643638
mutex_unlock(&snd_ctl_led_mutex);
644639
up_read(&card->controls_rwsem);
645640
snd_card_unref(card);
646-
return buf2 - buf;
641+
return l;
647642
}
648643

649644
static DEVICE_ATTR_WO(attach);

0 commit comments

Comments
 (0)