Skip to contentSkip to conversation

gpio: mockup: bug fixes and tweaks

PATCH In review Bartosz Golaszewski 2 participants 7 messages Dec 20, 2016 – Dec 28, 2016

Subsystems (2)
lore
Share & tools
lore
#3 lore raw

PATCH 2/3 gpio: mockup: dynamically allocate memory for chip name

In reply to Bartosz Golaszewski

Currently the chip name buffer is allocated on the stack and the address of the buffer is passed to the gpio framework. It's invalid after probe() returns, so the sysfs label attribute displays garbage.

Use devm_kasprintf() for each string instead.

---
 drivers/gpio/gpio-mockup.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpio/gpio-mockup.c +7 -3
index af0c1e8..10f6bf6 100644
@@ -126,7 +126,7 @@ static int mockup_gpio_probe(struct platform_device *pdev)
 	int i;
 	int base;
 	int ngpio;
-	char chip_name[sizeof(GPIO_NAME) + 3];
+	char *chip_name;
 
 	if (gpio_mockup_params_nr < 2)
 		return -EINVAL;
@@ -146,8 +146,12 @@ static int mockup_gpio_probe(struct platform_device *pdev)
 			ngpio = gpio_mockup_ranges[i * 2 + 1] - base;
 
 		if (ngpio >= 0) {
-			sprintf(chip_name, "%s-%c", GPIO_NAME,
-				pins_name_start + i);
+			chip_name = devm_kasprintf(dev, GFP_KERNEL,
+						   "%s-%c", GPIO_NAME,
+						   pins_name_start + i);
+			if (!chip_name)
+				return -ENOMEM;
+
 			ret = mockup_gpio_add(dev, &cntr[i],
 					      chip_name, base, ngpio);
 		} else {
Signature · 1 line
2.9.3
#0 lore raw

cover gpio: mockup: bug fixes and tweaks

While trying to use the gpio-mockup driver for testing purposes I noticed there's an issue with the gpiochip's label. It turned out the label pointer points to invalid memory area. This series fixes it and adds some minor changes in other places too.

Bartosz Golaszewski (3):
gpio: mockup: make pins_name_start static
gpio: mockup: dynamically allocate memory for chip name
gpio: mockup: coding style fixes

 drivers/gpio/gpio-mockup.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
Signature · 1 line
2.9.3
#1 lore raw

PATCH 1/3 gpio: mockup: make pins_name_start static

This variable is not used outside this module. Make it static.

---
 drivers/gpio/gpio-mockup.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpio/gpio-mockup.c +1 -1
index 1ef85b0..af0c1e8 100644
@@ -43,7 +43,7 @@ static int gpio_mockup_ranges[MAX_GC << 1];
 static int gpio_mockup_params_nr;
 module_param_array(gpio_mockup_ranges, int, &gpio_mockup_params_nr, 0400);
 
-const char pins_name_start = 'A';
+static const char pins_name_start = 'A';
 
 static int mockup_gpio_get(struct gpio_chip *gc, unsigned int offset)
 {
Signature · 1 line
2.9.3
#5 lore raw

PATCH 3/3 gpio: mockup: coding style fixes

In reply to Bartosz Golaszewski

Fix whitespace errors and arrange local variables for better readability.

---
 drivers/gpio/gpio-mockup.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
drivers/gpio/gpio-mockup.c +4 -7
index 10f6bf6..82a9efd 100644
@@ -120,12 +120,9 @@ static int mockup_gpio_add(struct device *dev,
 
 static int mockup_gpio_probe(struct platform_device *pdev)
 {
-	struct device *dev = &pdev->dev;
 	struct mockup_gpio_controller *cntr;
-	int ret;
-	int i;
-	int base;
-	int ngpio;
+	struct device *dev = &pdev->dev;
+	int ret, i, base, ngpio;
 	char *chip_name;
 
 	if (gpio_mockup_params_nr < 2)
@@ -174,8 +171,8 @@ static int mockup_gpio_probe(struct platform_device *pdev)
 
 static struct platform_driver mockup_gpio_driver = {
 	.driver = {
-		   .name = GPIO_NAME,
-		   },
+		.name = GPIO_NAME,
+	},
 	.probe = mockup_gpio_probe,
 };
 
Signature · 1 line
2.9.3
#4 lore raw

Re: [PATCH 2/3] gpio: mockup: dynamically allocate memory for chip name

In reply to Bartosz Golaszewski
Quoted 7 lines · Bartosz Golaszewski

Currently the chip name buffer is allocated on the stack and the address of the buffer is passed to the gpio framework. It's invalid after probe() returns, so the sysfs label attribute displays garbage.

Use devm_kasprintf() for each string instead.

Patch applied.

Yours,
Linus Walleij