public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gas: Fix memory leaks in ginsn.c
@ 2024-04-09 23:05 H.J. Lu
  2024-04-11  5:14 ` Indu Bhagat
  0 siblings, 1 reply; 2+ messages in thread
From: H.J. Lu @ 2024-04-09 23:05 UTC (permalink / raw)
  To: binutils; +Cc: indu.bhagat

Free buffer memory after use in ginsn.c.

	* ginsn.c (ginsn_dst_print): Free buffer after use.
	(ginsn_print): Likewise.
---
 gas/ginsn.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/gas/ginsn.c b/gas/ginsn.c
index 492e161876b..fe7a5253310 100644
--- a/gas/ginsn.c
+++ b/gas/ginsn.c
@@ -507,6 +507,7 @@ ginsn_dst_print (struct ginsn_dst *dst)
       char *buf = XNEWVEC (char, 32);
       sprintf (buf, "%%r%d", ginsn_get_dst_reg (dst));
       strcat (dst_str, buf);
+      free (buf);
     }
   else if (dst->type == GINSN_DST_INDIRECT)
     {
@@ -514,6 +515,7 @@ ginsn_dst_print (struct ginsn_dst *dst)
       sprintf (buf, "[%%r%d+%lld]", ginsn_get_dst_reg (dst),
 		 (long long int) ginsn_get_dst_disp (dst));
       strcat (dst_str, buf);
+      free (buf);
     }
 
   gas_assert (strlen (dst_str) < GINSN_LISTING_OPND_LEN);
@@ -570,20 +572,26 @@ ginsn_print (ginsnS *ginsn)
 
   /* src 1.  */
   src = ginsn_get_src1 (ginsn);
+  char *src_buf = ginsn_src_print (src);
   str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
-			" %s", ginsn_src_print (src));
+			" %s", src_buf);
+  free (src_buf);
   gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);
 
   /* src 2.  */
   src = ginsn_get_src2 (ginsn);
+  src_buf = ginsn_src_print (src);
   str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
-			"%s", ginsn_src_print (src));
+			"%s", src_buf);
+  free (src_buf);
   gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);
 
   /* dst.  */
   dst = ginsn_get_dst (ginsn);
+  char *dst_buf = ginsn_dst_print (dst);
   str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
-			"%s", ginsn_dst_print (dst));
+			"%s", dst_buf);
+  free (dst_buf);
 
 end:
   gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);
-- 
2.44.0


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

* Re: [PATCH] gas: Fix memory leaks in ginsn.c
  2024-04-09 23:05 [PATCH] gas: Fix memory leaks in ginsn.c H.J. Lu
@ 2024-04-11  5:14 ` Indu Bhagat
  0 siblings, 0 replies; 2+ messages in thread
From: Indu Bhagat @ 2024-04-11  5:14 UTC (permalink / raw)
  To: H.J. Lu, binutils

On 4/9/24 16:05, H.J. Lu wrote:
> Free buffer memory after use in ginsn.c.
> 
> 	* ginsn.c (ginsn_dst_print): Free buffer after use.
> 	(ginsn_print): Likewise.

Thanks for fixing these.

I saw that ginsn_dst_print has these weird way of creating a new buffer 
and strcat'ing it to the original buffer.  I can fix those patterns to 
use an snprintf like the rest of the file, after your patch goes in.

Indu

> ---
>   gas/ginsn.c | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/gas/ginsn.c b/gas/ginsn.c
> index 492e161876b..fe7a5253310 100644
> --- a/gas/ginsn.c
> +++ b/gas/ginsn.c
> @@ -507,6 +507,7 @@ ginsn_dst_print (struct ginsn_dst *dst)
>         char *buf = XNEWVEC (char, 32);
>         sprintf (buf, "%%r%d", ginsn_get_dst_reg (dst));
>         strcat (dst_str, buf);
> +      free (buf);
>       }
>     else if (dst->type == GINSN_DST_INDIRECT)
>       {
> @@ -514,6 +515,7 @@ ginsn_dst_print (struct ginsn_dst *dst)
>         sprintf (buf, "[%%r%d+%lld]", ginsn_get_dst_reg (dst),
>   		 (long long int) ginsn_get_dst_disp (dst));
>         strcat (dst_str, buf);
> +      free (buf);
>       }
>   
>     gas_assert (strlen (dst_str) < GINSN_LISTING_OPND_LEN);
> @@ -570,20 +572,26 @@ ginsn_print (ginsnS *ginsn)
>   
>     /* src 1.  */
>     src = ginsn_get_src1 (ginsn);
> +  char *src_buf = ginsn_src_print (src);
>     str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
> -			" %s", ginsn_src_print (src));
> +			" %s", src_buf);
> +  free (src_buf);
>     gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);
>   
>     /* src 2.  */
>     src = ginsn_get_src2 (ginsn);
> +  src_buf = ginsn_src_print (src);
>     str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
> -			"%s", ginsn_src_print (src));
> +			"%s", src_buf);
> +  free (src_buf);
>     gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);
>   
>     /* dst.  */
>     dst = ginsn_get_dst (ginsn);
> +  char *dst_buf = ginsn_dst_print (dst);
>     str_size += snprintf (ginsn_str + str_size, GINSN_LISTING_LEN - str_size,
> -			"%s", ginsn_dst_print (dst));
> +			"%s", dst_buf);
> +  free (dst_buf);
>   
>   end:
>     gas_assert (str_size >= 0 && str_size < GINSN_LISTING_LEN);


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

end of thread, other threads:[~2024-04-11  5:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-09 23:05 [PATCH] gas: Fix memory leaks in ginsn.c H.J. Lu
2024-04-11  5:14 ` Indu Bhagat

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).