public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix resource leak found by Coverity
@ 2018-10-10 13:53 Gary Benson
  2018-10-10 14:11 ` Pedro Alves
  0 siblings, 1 reply; 3+ messages in thread
From: Gary Benson @ 2018-10-10 13:53 UTC (permalink / raw)
  To: gdb-patches

Hi all,

This commit fixes a resource leak found by Coverity where
find_oload_champ leaked every badness vector it calculated
bar the one it returned.

Built and regtested on RHEL 7.5 x86_64.

Ok to commit?

Thanks,
Gary

--
gdb/ChangeLog:

	* valops.c (find_oload_champ): Fix resource leak found by
	Coverity.
---
 gdb/ChangeLog | 5 +++++
 gdb/valops.c  | 9 ++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/gdb/valops.c b/gdb/valops.c
index c45caef..db2d3ed 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -3024,8 +3024,6 @@ find_oload_champ (struct value **args, int nargs,
 		  struct badness_vector **oload_champ_bv)
 {
   int ix;
-  /* A measure of how good an overloaded instance is.  */
-  struct badness_vector *bv;
   /* Index of best overloaded function.  */
   int oload_champ = -1;
   /* Current ambiguity state for overload resolution.  */
@@ -3075,13 +3073,15 @@ find_oload_champ (struct value **args, int nargs,
 
       /* Compare parameter types to supplied argument types.  Skip
          THIS for static methods.  */
-      bv = rank_function (parm_types, nparms, 
+      struct badness_vector *bv
+	= rank_function (parm_types, nparms,
 			  args + static_offset,
 			  nargs - static_offset);
 
       if (!*oload_champ_bv)
 	{
 	  *oload_champ_bv = bv;
+	  bv = NULL;
 	  oload_champ = 0;
 	}
       else /* See whether current candidate is better or worse than
@@ -3095,7 +3095,9 @@ find_oload_champ (struct value **args, int nargs,
 	    oload_ambiguous = 2;
 	    break;
 	  case 2:		/* New champion, record details.  */
+	    xfree (*oload_champ_bv);
 	    *oload_champ_bv = bv;
+	    bv = NULL;
 	    oload_ambiguous = 0;
 	    oload_champ = ix;
 	    break;
@@ -3103,6 +3105,7 @@ find_oload_champ (struct value **args, int nargs,
 	  default:
 	    break;
 	  }
+      xfree (bv);
       xfree (parm_types);
       if (overload_debug)
 	{
-- 
1.8.3.1

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

* Re: [PATCH] Fix resource leak found by Coverity
  2018-10-10 13:53 [PATCH] Fix resource leak found by Coverity Gary Benson
@ 2018-10-10 14:11 ` Pedro Alves
  2018-10-10 14:20   ` [PATCH] Fix badness vector resource leaks " Gary Benson
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2018-10-10 14:11 UTC (permalink / raw)
  To: Gary Benson, gdb-patches

Please use some more descriptive subject in these emails/commits,
otherwise we're going to end up with dozens of indistinct 
"Fix resource leak found by Coverity" patches/emails.  :-)

E.g., "Fix leak in valops.c:find_oload_champ" or some such.

As for the patch itself, I think that we'd still be leaking bv->rank,
right?  Not just here, but in the callers of rank_function
as well?

I have a patch from last year here that converts the badness
vector to a C++ std::vector:

 https://github.com/palves/gdb/commits/palves/badness_vector

See top two commits.

I guess a better approach would be to get that in instead.

Simon had a badness_vector C++ification patch too,
though his was different.  ISTR that I prefer my approach,
but I won't be surprised if Simon preferred his.  :-)

Thanks,
Pedro Alves

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

* Re: [PATCH] Fix badness vector resource leaks found by Coverity
  2018-10-10 14:11 ` Pedro Alves
@ 2018-10-10 14:20   ` Gary Benson
  0 siblings, 0 replies; 3+ messages in thread
From: Gary Benson @ 2018-10-10 14:20 UTC (permalink / raw)
  To: Pedro Alves; +Cc: gdb-patches

Pedro Alves wrote:
> Please use some more descriptive subject in these emails/commits,
> otherwise we're going to end up with dozens of indistinct 
> "Fix resource leak found by Coverity" patches/emails.  :-)

Yeah, it just occurred to me to do that, after I looked in my
inbox :D

> As for the patch itself, I think that we'd still be leaking
> bv->rank, right?  Not just here, but in the callers of rank_function
> as well?

Oh, I just looked in valops.c to see what other things did to
free the vectors.  So there's other leaks in there as well :/

> I have a patch from last year here that converts the badness
> vector to a C++ std::vector:
> 
>  https://github.com/palves/gdb/commits/palves/badness_vector
> 
> See top two commits.
> 
> I guess a better approach would be to get that in instead.
> 
> Simon had a badness_vector C++ification patch too,
> though his was different.  ISTR that I prefer my approach,
> but I won't be surprised if Simon preferred his.  :-)

I'll look at them.  Were they ever submitted, or unfinished, or... ?

Cheers,
Gary

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

end of thread, other threads:[~2018-10-10 14:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-10 13:53 [PATCH] Fix resource leak found by Coverity Gary Benson
2018-10-10 14:11 ` Pedro Alves
2018-10-10 14:20   ` [PATCH] Fix badness vector resource leaks " Gary Benson

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