mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* barebox crashes with CONFIG_CMD_MAGICVAR=y and CONFIG_CMD_MAGICVAR_HELP unset
@ 2016-09-08 19:27 Uwe Kleine-König
  2016-09-09  6:25 ` Sascha Hauer
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2016-09-08 19:27 UTC (permalink / raw)
  To: barebox

Hello,

with

	CONFIG_CMD_MAGICVAR=y
	# CONFIG_CMD_MAGICVAR_HELP is not set

calling magicvar results in

	md->description = xstrdup(description)

in line 64 of commands/magicvar.c. As CONFIG_CMD_MAGICVAR_HELP is not
set, description is NULL which makes xstrdup fail.

I fixed it here doing

-       md->description = xstrdup(description);
+       md->description = description ? xstrdup(description) : NULL;

but I can imagine other variants:

	if (IS_ENABLED(CONFIG_CMD_MAGICVAR_HELP))
		md->description = xstrdup(description);

or fixing xstrdup to not choke on NULL.

What do you prefer?

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: barebox crashes with CONFIG_CMD_MAGICVAR=y and CONFIG_CMD_MAGICVAR_HELP unset
  2016-09-08 19:27 barebox crashes with CONFIG_CMD_MAGICVAR=y and CONFIG_CMD_MAGICVAR_HELP unset Uwe Kleine-König
@ 2016-09-09  6:25 ` Sascha Hauer
  2016-09-09  6:37   ` [PATCH] xstrdup: don't panic on xstrdup(NULL) Uwe Kleine-König
  0 siblings, 1 reply; 4+ messages in thread
From: Sascha Hauer @ 2016-09-09  6:25 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Thu, Sep 08, 2016 at 09:27:52PM +0200, Uwe Kleine-König wrote:
> Hello,
> 
> with
> 
> 	CONFIG_CMD_MAGICVAR=y
> 	# CONFIG_CMD_MAGICVAR_HELP is not set
> 
> calling magicvar results in
> 
> 	md->description = xstrdup(description)
> 
> in line 64 of commands/magicvar.c. As CONFIG_CMD_MAGICVAR_HELP is not
> set, description is NULL which makes xstrdup fail.
> 
> I fixed it here doing
> 
> -       md->description = xstrdup(description);
> +       md->description = description ? xstrdup(description) : NULL;
> 
> but I can imagine other variants:
> 
> 	if (IS_ENABLED(CONFIG_CMD_MAGICVAR_HELP))
> 		md->description = xstrdup(description);
> 
> or fixing xstrdup to not choke on NULL.
> 
> What do you prefer?

kstrdup in the kernel tests for NULL and xstrdup in busybox does also.
Let's just add the test.

Sascha


-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] xstrdup: don't panic on xstrdup(NULL)
  2016-09-09  6:25 ` Sascha Hauer
@ 2016-09-09  6:37   ` Uwe Kleine-König
  2016-09-12  6:16     ` Sascha Hauer
  0 siblings, 1 reply; 4+ messages in thread
From: Uwe Kleine-König @ 2016-09-09  6:37 UTC (permalink / raw)
  To: barebox

Instead return just NULL. This matches the behaviour of kstrdup in the
kernel and xstrdup in busybox.

This fixes a panic with CONFIG_CMD_MAGICVAR=y and
CONFIG_CMD_MAGICVAR_HELP unset in magicvar_add() where description is
always NULL.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
 lib/xfuncs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/lib/xfuncs.c b/lib/xfuncs.c
index aaf078854429..1dc2ea92d84f 100644
--- a/lib/xfuncs.c
+++ b/lib/xfuncs.c
@@ -56,10 +56,15 @@ EXPORT_SYMBOL(xzalloc);
 
 char *xstrdup(const char *s)
 {
-	char *p = strdup(s);
+	char *p;
+
+	if (!s)
+		return NULL;
 
+	p = strdup(s);
 	if (!p)
 		panic("ERROR: out of memory\n");
+
 	return p;
 }
 EXPORT_SYMBOL(xstrdup);
-- 
2.8.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xstrdup: don't panic on xstrdup(NULL)
  2016-09-09  6:37   ` [PATCH] xstrdup: don't panic on xstrdup(NULL) Uwe Kleine-König
@ 2016-09-12  6:16     ` Sascha Hauer
  0 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2016-09-12  6:16 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: barebox

On Fri, Sep 09, 2016 at 08:37:22AM +0200, Uwe Kleine-König wrote:
> Instead return just NULL. This matches the behaviour of kstrdup in the
> kernel and xstrdup in busybox.
> 
> This fixes a panic with CONFIG_CMD_MAGICVAR=y and
> CONFIG_CMD_MAGICVAR_HELP unset in magicvar_add() where description is
> always NULL.
> 
> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
> ---

Applied, thanks

Sascha

>  lib/xfuncs.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/xfuncs.c b/lib/xfuncs.c
> index aaf078854429..1dc2ea92d84f 100644
> --- a/lib/xfuncs.c
> +++ b/lib/xfuncs.c
> @@ -56,10 +56,15 @@ EXPORT_SYMBOL(xzalloc);
>  
>  char *xstrdup(const char *s)
>  {
> -	char *p = strdup(s);
> +	char *p;
> +
> +	if (!s)
> +		return NULL;
>  
> +	p = strdup(s);
>  	if (!p)
>  		panic("ERROR: out of memory\n");
> +
>  	return p;
>  }
>  EXPORT_SYMBOL(xstrdup);
> -- 
> 2.8.1
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

-- 
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
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-09-12  6:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-08 19:27 barebox crashes with CONFIG_CMD_MAGICVAR=y and CONFIG_CMD_MAGICVAR_HELP unset Uwe Kleine-König
2016-09-09  6:25 ` Sascha Hauer
2016-09-09  6:37   ` [PATCH] xstrdup: don't panic on xstrdup(NULL) Uwe Kleine-König
2016-09-12  6:16     ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox