All of lore.kernel.org
 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 10:55:11 +0200	[thread overview]
Message-ID: <1427705712-18083-3-git-send-email-ricardo.ribalda@gmail.com> (raw)
In-Reply-To: <1427705712-18083-1-git-send-email-ricardo.ribalda@gmail.com>

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

  parent reply	other threads:[~2015-03-30  8:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-30  8:55 [PATCH 0/3] leds/class: Fix string handling Ricardo Ribalda Delgado
2015-03-30  8:55 ` [PATCH 1/3] leds/class: Use strlcpy instead of strncpy Ricardo Ribalda Delgado
2015-03-30  8:55 ` Ricardo Ribalda Delgado [this message]
2015-03-30  8:59   ` [PATCH 2/3] leds/class: Check snprintf return value Geert Uytterhoeven
2015-03-30  8:55 ` [PATCH 3/3] leds/class: Set naming index as unsigned Ricardo Ribalda Delgado
2015-03-30  8:59   ` Geert Uytterhoeven
2015-03-30 17:15 ` [PATCH 0/3] leds/class: Fix string handling Bryan Wu
2015-03-30 17:46   ` Ricardo Ribalda Delgado
2015-03-30 18:11     ` Bryan Wu
2015-03-30  9:08 [PATCH 2/3] leds/class: Check snprintf return value Ricardo Ribalda Delgado
2015-03-30  9:08 Ricardo Ribalda Delgado
2015-07-24  3:17 ` Alan Tull

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=1427705712-18083-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.