Re: [HACKERS] scripts/common.c minor memory leak - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [HACKERS] scripts/common.c minor memory leak
Date
Msg-id 200610032146.k93LkuA17441@momjian.us
Whole thread Raw
List pgsql-patches
Andrew Dunstan wrote:
> Martijn van Oosterhout wrote:
> > Just a minor thing. In yesno_prompt(), the value is resp is allocated
> > memory that is never freed.
> >
> > File: src/bin/scripts/common.c
> > Line: 218
> >
> > Not terribly important though, it's not used in critical utilities, but
> > it's used often.
> >
> > Found by coverity.
> >
>
> It is surely not the only memory leak.  We know there are some and in
> most cases (like this) they aren't worth the trouble to clean up. If it
> were used in psql or the backend I'd be worried, but it isn't, so I'm not.

I have applied the attached patch to fix this.  One reason I think it is
good to fix this is because it illustrates poor use of simple_prompt(),
that might be copied by others.

--
  Bruce Momjian   bruce@momjian.us
  EnterpriseDB    http://www.enterprisedb.com

  + If your life is a hard drive, Christ can be your backup. +
Index: src/bin/scripts/common.c
===================================================================
RCS file: /cvsroot/pgsql/src/bin/scripts/common.c,v
retrieving revision 1.22
diff -c -c -r1.22 common.c
*** src/bin/scripts/common.c    22 Sep 2006 19:51:14 -0000    1.22
--- src/bin/scripts/common.c    3 Oct 2006 21:40:52 -0000
***************
*** 208,227 ****
  {
      char prompt[256];

      for (;;)
      {
          char *resp;

-         /* translator: This is a question followed by the translated options for "yes" and "no". */
-         snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
-                  _(question), _(PG_YESLETTER), _(PG_NOLETTER));
          resp = simple_prompt(prompt, 1, true);

          if (strcmp(resp, _(PG_YESLETTER)) == 0)
              return true;
          else if (strcmp(resp, _(PG_NOLETTER)) == 0)
              return false;

          printf(_("Please answer \"%s\" or \"%s\".\n"),
                 _(PG_YESLETTER), _(PG_NOLETTER));
      }
--- 208,235 ----
  {
      char prompt[256];

+     /* translator: This is a question followed by the translated options for "yes" and "no". */
+     snprintf(prompt, sizeof(prompt), _("%s (%s/%s) "),
+              _(question), _(PG_YESLETTER), _(PG_NOLETTER));
+
      for (;;)
      {
          char *resp;

          resp = simple_prompt(prompt, 1, true);

          if (strcmp(resp, _(PG_YESLETTER)) == 0)
+         {
+             free(resp);
              return true;
+         }
          else if (strcmp(resp, _(PG_NOLETTER)) == 0)
+         {
+             free(resp);
              return false;
+         }

+         free(resp);
          printf(_("Please answer \"%s\" or \"%s\".\n"),
                 _(PG_YESLETTER), _(PG_NOLETTER));
      }

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] Numeric overflow problem + patch
Next
From: Bruce Momjian
Date:
Subject: Re: pgevent fixes