public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sim: hw: rework code to avoid gcc warnings
@ 2021-01-09  5:57 Mike Frysinger
  2021-01-09 11:29 ` Andrew Burgess
  0 siblings, 1 reply; 2+ messages in thread
From: Mike Frysinger @ 2021-01-09  5:57 UTC (permalink / raw)
  To: gdb-patches

Newer gcc thinks we might return a pointer to a stack buffer, but
we don't -- we strdup it before returning.  Rework the code to just
malloc the buffer from the start and avoid the stack+strdup.

sim/common/:

	* hw-base.c (full_name_of_hw): Delete full_name.  Replace
	hw_strdup call with hw_malloc.
---
 sim/common/hw-base.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

diff --git a/sim/common/hw-base.c b/sim/common/hw-base.c
index d4b519845d1e..87ca7292e558 100644
--- a/sim/common/hw-base.c
+++ b/sim/common/hw-base.c
@@ -281,11 +281,10 @@ full_name_of_hw (struct hw *leaf,
 		 unsigned sizeof_buf)
 {
   /* get a buffer */
-  char full_name[1024];
-  if (buf == (char*)0)
+  if (buf == NULL)
     {
-      buf = full_name;
-      sizeof_buf = sizeof (full_name);
+      sizeof_buf = 1024;
+      buf = hw_malloc (leaf, sizeof_buf);
     }
 
   /* use head recursion to construct the path */
@@ -318,9 +317,6 @@ full_name_of_hw (struct hw *leaf,
       strcat (buf, unit);
     }
 
-  /* return it usefully */
-  if (buf == full_name)
-    buf = hw_strdup (leaf, full_name);
   return buf;
 }
 
-- 
2.28.0


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

* Re: [PATCH] sim: hw: rework code to avoid gcc warnings
  2021-01-09  5:57 [PATCH] sim: hw: rework code to avoid gcc warnings Mike Frysinger
@ 2021-01-09 11:29 ` Andrew Burgess
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Burgess @ 2021-01-09 11:29 UTC (permalink / raw)
  To: Mike Frysinger; +Cc: gdb-patches

* Mike Frysinger via Gdb-patches <gdb-patches@sourceware.org> [2021-01-09 00:57:31 -0500]:

> Newer gcc thinks we might return a pointer to a stack buffer, but
> we don't -- we strdup it before returning.  Rework the code to just
> malloc the buffer from the start and avoid the stack+strdup.
> 
> sim/common/:
> 
> 	* hw-base.c (full_name_of_hw): Delete full_name.  Replace
> 	hw_strdup call with hw_malloc.

LGTM.

Thanks,
Andrew

> ---
>  sim/common/hw-base.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/sim/common/hw-base.c b/sim/common/hw-base.c
> index d4b519845d1e..87ca7292e558 100644
> --- a/sim/common/hw-base.c
> +++ b/sim/common/hw-base.c
> @@ -281,11 +281,10 @@ full_name_of_hw (struct hw *leaf,
>  		 unsigned sizeof_buf)
>  {
>    /* get a buffer */
> -  char full_name[1024];
> -  if (buf == (char*)0)
> +  if (buf == NULL)
>      {
> -      buf = full_name;
> -      sizeof_buf = sizeof (full_name);
> +      sizeof_buf = 1024;
> +      buf = hw_malloc (leaf, sizeof_buf);
>      }
>  
>    /* use head recursion to construct the path */
> @@ -318,9 +317,6 @@ full_name_of_hw (struct hw *leaf,
>        strcat (buf, unit);
>      }
>  
> -  /* return it usefully */
> -  if (buf == full_name)
> -    buf = hw_strdup (leaf, full_name);
>    return buf;
>  }
>  
> -- 
> 2.28.0
> 

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

end of thread, other threads:[~2021-01-09 11:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-09  5:57 [PATCH] sim: hw: rework code to avoid gcc warnings Mike Frysinger
2021-01-09 11:29 ` Andrew Burgess

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 read-only IMAP folder(s) and NNTP newsgroup(s).