linux-leds.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
To: Bryan Wu <cooloney@gmail.com>, Richard Purdie <rpurdie@rpsys.net>,
	linux-leds@vger.kernel.org, linux-kernel@vger.kernel.org,
	Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
Subject: [PATCH 2/3] leds/class: Check snprintf return value
Date: Mon, 30 Mar 2015 11:08:50 +0200	[thread overview]
Message-ID: <1427706531-22569-1-git-send-email-ricardo.ribalda@gmail.com> (raw)

If the final name does not fit the string there is a potential
endless-loop.

Reported-by: Geert Uytterhoeven <geert@linux-m68k.org>
Signed-off-by: Ricardo Ribalda Delgado <ricardo.ribalda@gmail.com>
---
 drivers/leds/led-class.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/drivers/leds/led-class.c b/drivers/leds/led-class.c
index 294d6d6..2ffe8b1 100644
--- a/drivers/leds/led-class.c
+++ b/drivers/leds/led-class.c
@@ -223,12 +223,17 @@ static int led_classdev_next_name(const char *init_name, char *name,
 				  size_t len)
 {
 	int i = 0;
+	int ret = 0;
 
 	strlcpy(name, init_name, len);
 
-	while (class_find_device(leds_class, NULL, name, match_name))
+	while (class_find_device(leds_class, NULL, name, match_name) &&
+	       (ret < len))
 		snprintf(name, len, "%s_%d", init_name, ++i);
 
+	if (ret >= len)
+		return -ENOMEM;
+
 	return i;
 }
 
@@ -243,6 +248,9 @@ int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 	int ret;
 
 	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
+	if (ret < 0)
+		return ret;
+
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
 				led_cdev, led_cdev->groups, "%s", name);
 	if (IS_ERR(led_cdev->dev))
-- 
2.1.4

             reply	other threads:[~2015-03-30  9:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-30  9:08 Ricardo Ribalda Delgado [this message]
2015-07-24  3:17 ` [PATCH 2/3] leds/class: Check snprintf return value Alan Tull
  -- strict thread matches above, loose matches on Subject: below --
2015-03-30  9:08 Ricardo Ribalda Delgado
2015-03-30  8:55 [PATCH 0/3] leds/class: Fix string handling Ricardo Ribalda Delgado
2015-03-30  8:55 ` [PATCH 2/3] leds/class: Check snprintf return value Ricardo Ribalda Delgado
2015-03-30  8:59   ` Geert Uytterhoeven

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=1427706531-22569-1-git-send-email-ricardo.ribalda@gmail.com \
    --to=ricardo.ribalda@gmail.com \
    --cc=cooloney@gmail.com \
    --cc=geert@linux-m68k.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=rpurdie@rpsys.net \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).