Skip to contentSkip to conversation

led: try to get LED's label from the 'label' property

PATCH Superseded Antony Pavlov 3 participants 7 messages Jul 27, 2014 – Jul 29, 2014

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 led: try to get LED's label from the 'label' property

---
 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +3 -1
index 7bb3b49..cddac08 100644
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));
+		if (!gled->led.name)
+			gled->led.name = xstrdup(child->name);
 		gled->gpio = gpio;
 		gled->active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
 
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#1 lore raw
Quoted 10 lines · Antony Pavlov
---
 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +0 -0
index 7bb3b49..cddac08 100644

drivers/led/led-gpio.c static int led_gpio_of_probe(struct device_d *dev) · line 215

Quoted 5 lines
drivers/led/led-gpio.c +1 -1
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));

When the property doesn't exist you pass NULL to xstrdup. This will crash. You'll have to add a check or make xstrdup tolerate NULL pointers.

Sascha

Quoted 14 lines
drivers/led/led-gpio.c +2 -0
+		if (!gled->led.name)
+			gled->led.name = xstrdup(child->name);
 		gled->gpio = gpio;
 		gled->active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
 
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#2 lore raw
Quoted 13 lines · On Mon, 28 Jul 2014 07:49:17 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 12 lines · Antony Pavlov
---
 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +0 -0
index 7bb3b49..cddac08 100644
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
Quoted 6 lines

drivers/led/led-gpio.c static int led_gpio_of_probe(struct device_d *dev) · line 215

Quoted 3 lines
drivers/led/led-gpio.c +1 -1
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));

When the property doesn't exist you pass NULL to xstrdup. This will crash.

My bad. I have seen barebox strdup() realization but missed xstrdup() realization.

Barebox' strdup() can handle NULL argument:

char * strdup(const char *s)

{
        char *new;

        if ((s == NULL) ||
            ((new = malloc (strlen(s) + 1)) == NULL) ) {                         
                return NULL;                                                     
        }

        strcpy (new, s);
        return new;
}

But xstrdup() can't do so:

char *xstrdup(const char *s)

{
        char *p = strdup(s);

        if (!p)
                panic("ERROR: out of memory\n");
        return p;
}               

Can I just change first xstrdup() to strdup() in my patch?

E.g.

- gled->led.name = xstrdup(of_get_property(child, "label", NULL));

+               gled->led.name = strdup(of_get_property(child, "label", NULL));
                if (!gled->led.name)
                        gled->led.name = xstrdup(child->name);
Quoted 25 lines

You'll have to add a check or make xstrdup tolerate NULL pointers.

Sascha

Quoted 14 lines
drivers/led/led-gpio.c +2 -0
+		if (!gled->led.name)
+			gled->led.name = xstrdup(child->name);
 		gled->gpio = gpio;
 		gled->active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
 
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 8 lines
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#3 lore raw
Quoted 51 lines · Antony Pavlov
Quoted 13 lines · On Mon, 28 Jul 2014 07:49:17 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 12 lines · Antony Pavlov
---
 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +0 -0
index 7bb3b49..cddac08 100644
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
Quoted 6 lines

drivers/led/led-gpio.c static int led_gpio_of_probe(struct device_d *dev) · line 215

Quoted 3 lines
drivers/led/led-gpio.c +1 -1
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));

When the property doesn't exist you pass NULL to xstrdup. This will crash.

My bad. I have seen barebox strdup() realization but missed xstrdup() realization.

Barebox' strdup() can handle NULL argument:

char * strdup(const char *s)

{
        char *new;

        if ((s == NULL) ||
            ((new = malloc (strlen(s) + 1)) == NULL) ) {                         
                return NULL;                                                     
        }

        strcpy (new, s);
        return new;
}

But xstrdup() can't do so:

char *xstrdup(const char *s)

{
        char *p = strdup(s);

        if (!p)
                panic("ERROR: out of memory\n");
        return p;
}               
Quoted 1 line

Can I just change first xstrdup() to strdup() in my patch?

Why not add a NULL pointer check to xstrdup?

Review

Quoted 7 lines

E.g.

(quoted diff) +1 -1
-               gled->led.name = xstrdup(of_get_property(child, "label", NULL));
+               gled->led.name = strdup(of_get_property(child, "label", NULL));
                if (!gled->led.name)
                        gled->led.name = xstrdup(child->name);

I think this code will raise questions. When reading this code one could easily ask why stdrup is used in the first place and xstrdup in the second. A year ahead even we may not know the answer without thinking about it ;)

Sascha

Signature · 9 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#4 lore raw
Quoted 47 lines · On Mon, 28 Jul 2014 08:29:13 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 46 lines · Antony Pavlov
Quoted 13 lines · On Mon, 28 Jul 2014 07:49:17 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 12 lines · Antony Pavlov
---
 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +0 -0
index 7bb3b49..cddac08 100644
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
Quoted 6 lines

drivers/led/led-gpio.c static int led_gpio_of_probe(struct device_d *dev) · line 215

Quoted 3 lines
drivers/led/led-gpio.c +1 -1
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));

When the property doesn't exist you pass NULL to xstrdup. This will crash.

My bad. I have seen barebox strdup() realization but missed xstrdup() realization.

Barebox' strdup() can handle NULL argument:

char * strdup(const char *s)

{
        char *new;

        if ((s == NULL) ||
            ((new = malloc (strlen(s) + 1)) == NULL) ) {                         
                return NULL;                                                     
        }

        strcpy (new, s);
        return new;
}

But xstrdup() can't do so:

char *xstrdup(const char *s)

{
        char *p = strdup(s);
Quoted 8 lines
Quoted 6 lines
        if (!p)
                panic("ERROR: out of memory\n");
        return p;
}               

Can I just change first xstrdup() to strdup() in my patch?

Why not add a NULL pointer check to xstrdup?

This will be duplication of code. strdup() inside xstrdup() check it too.

Quoted 21 lines

Review

Quoted 7 lines

E.g.

(quoted diff) +1 -1
-               gled->led.name = xstrdup(of_get_property(child, "label", NULL));
+               gled->led.name = strdup(of_get_property(child, "label", NULL));
                if (!gled->led.name)
                        gled->led.name = xstrdup(child->name);

I think this code will raise questions. When reading this code one could easily ask why stdrup is used in the first place and xstrdup in the second. A year ahead even we may not know the answer without thinking about it ;)

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 8 lines
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
#5 lore raw
In reply to Antony Pavlov
Quoted 60 lines · Antony Pavlov
Quoted 13 lines · On Mon, 28 Jul 2014 07:49:17 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 12 lines · Antony Pavlov
---
 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +0 -0
index 7bb3b49..cddac08 100644
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
Quoted 6 lines

drivers/led/led-gpio.c static int led_gpio_of_probe(struct device_d *dev) · line 215

Quoted 3 lines
drivers/led/led-gpio.c +1 -1
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));

When the property doesn't exist you pass NULL to xstrdup. This will crash.

My bad. I have seen barebox strdup() realization but missed xstrdup() realization.

Barebox' strdup() can handle NULL argument:

char * strdup(const char *s)

{
        char *new;

        if ((s == NULL) ||
            ((new = malloc (strlen(s) + 1)) == NULL) ) {                         
                return NULL;                                                     
        }

        strcpy (new, s);
        return new;
}

But xstrdup() can't do so:

char *xstrdup(const char *s)

{
        char *p = strdup(s);

        if (!p)
                panic("ERROR: out of memory\n");
        return p;
}               

Can I just change first xstrdup() to strdup() in my patch?

E.g.

(quoted diff) +1 -1
-               gled->led.name = xstrdup(of_get_property(child, "label", NULL));
+               gled->led.name = strdup(of_get_property(child, "label", NULL));
                if (!gled->led.name)
                        gled->led.name = xstrdup(child->name);
Quoted 3 lines
Quoted 3 lines

You'll have to add a check or make xstrdup tolerate NULL pointers.

Correct code sequence would probably be:

const char *label;

if (of_property_read_string(child, "label", &label))

	gled->led.name = xstrdup(label);
else
	gled->led.name = xstrdup(child->name);

or

const char *label = child->name;

of_property_read_string(child, "label", &label);
gled->led.name = xstrdup(label);

Sebastian

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
drivers/led/led-gpio.c +2 -0
+		if (!gled->led.name)
+			gled->led.name = xstrdup(child->name);
 		gled->gpio = gpio;
 		gled->active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
 
Signature · 1 line
2.0.1
Quoted 13 lines
Quoted 11 lines
Quoted 5 lines

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox

#6 lore raw
Quoted 66 lines · On Mon, 28 Jul 2014 08:34:43 +0200 Sebastian Hesselbarth…
Quoted 61 lines · Antony Pavlov
Quoted 14 lines · On Mon, 28 Jul 2014 07:49:17 +0200 Sascha Hauer <[email protected]> wrote:
Quoted 13 lines · Antony Pavlov

---

 drivers/led/led-gpio.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/led/led-gpio.c +0 -0
index 7bb3b49..cddac08 100644
@@ -212,7 +212,9 @@ static int led_gpio_of_probe(struct device_d *dev)
 			continue;
 
Quoted 6 lines

drivers/led/led-gpio.c static int led_gpio_of_probe(struct device_d *dev) · line 215

Quoted 3 lines
drivers/led/led-gpio.c +1 -1
 		gled = xzalloc(sizeof(*gled));
-		gled->led.name = xstrdup(child->name);
+		gled->led.name = xstrdup(of_get_property(child, "label", NULL));

When the property doesn't exist you pass NULL to xstrdup. This will crash.

My bad. I have seen barebox strdup() realization but missed xstrdup() realization.

Barebox' strdup() can handle NULL argument:

char * strdup(const char *s)

{
        char *new;

        if ((s == NULL) ||
            ((new = malloc (strlen(s) + 1)) == NULL) ) {                         
                return NULL;                                                     
        }

        strcpy (new, s);
        return new;
}

But xstrdup() can't do so:

char *xstrdup(const char *s)

{
        char *p = strdup(s);

        if (!p)
                panic("ERROR: out of memory\n");
        return p;
}               

Can I just change first xstrdup() to strdup() in my patch?

E.g.

(quoted diff) +1 -1
-               gled->led.name = xstrdup(of_get_property(child, "label", NULL));
+               gled->led.name = strdup(of_get_property(child, "label", NULL));
                if (!gled->led.name)
                        gled->led.name = xstrdup(child->name);
Quoted 3 lines
Quoted 3 lines

You'll have to add a check or make xstrdup tolerate NULL pointers.

Quoted 8 lines

Correct code sequence would probably be:

const char *label;

if (of_property_read_string(child, "label", &label))

	gled->led.name = xstrdup(label);
else
	gled->led.name = xstrdup(child->name);

Hmm. of_property_read_string() returns 0 on success...

Quoted 33 lines

or

const char *label = child->name;

of_property_read_string(child, "label", &label);
gled->led.name = xstrdup(label);

Sebastian

Quoted 22 lines
Quoted 20 lines
Quoted 14 lines
drivers/led/led-gpio.c +2 -0
+		if (!gled->led.name)
+			gled->led.name = xstrdup(child->name);
 		gled->gpio = gpio;
 		gled->active_low = (flags & OF_GPIO_ACTIVE_LOW) ? 1 : 0;
 
Signature · 7 lines
2.0.1


_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Signature · 4 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
Signature · 8 lines
-- 
Best regards,
  Antony Pavlov

_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox