On Fri, Feb 14, 2014 at 11:28:49PM -0500, Tom Lane wrote:
> Bruce Momjian <br...@momjian.us> writes:
> > The attached tiny patch fixes a small leak in psql's \gset command and
> > simplifies memory freeing in two places.
> 
> The first and third hunks look to me like they introduce memory
> leaks, not fix them.  The second hunk is probably safe enough,

The first and third just move the free into blocks where we have already
tested the value is not null.  It just more clearly matches the
surrounding code.  Do you see something different?

> although I'm not sure the case can actually occur --- gset should
> free the prefix before any new backslash command can be executed.

Oh, interesting idea.  Updated patch attached.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
new file mode 100644
index 764534a..8225159
*** a/src/bin/psql/command.c
--- b/src/bin/psql/command.c
*************** exec_command(const char *cmd,
*** 746,762 ****
  		{
  			expand_tilde(&fname);
  			pset.gfname = pg_strdup(fname);
  		}
- 		free(fname);
  		status = PSQL_CMD_SEND;
  	}
  
  	/* \gset [prefix] -- send query and store result into variables */
  	else if (strcmp(cmd, "gset") == 0)
  	{
! 		char	   *prefix = psql_scan_slash_option(scan_state,
! 													OT_NORMAL, NULL, false);
  
  		if (prefix)
  			pset.gset_prefix = prefix;
  		else
--- 746,766 ----
  		{
  			expand_tilde(&fname);
  			pset.gfname = pg_strdup(fname);
+ 			free(fname);
  		}
  		status = PSQL_CMD_SEND;
  	}
  
  	/* \gset [prefix] -- send query and store result into variables */
  	else if (strcmp(cmd, "gset") == 0)
  	{
! 		char	   *prefix;
! 
! 		if (pset.gset_prefix)
! 			free(pset.gset_prefix);
  
+ 		prefix = psql_scan_slash_option(scan_state,
+ 										OT_NORMAL, NULL, false);
  		if (prefix)
  			pset.gset_prefix = prefix;
  		else
*************** exec_command(const char *cmd,
*** 1152,1159 ****
  				success = false;
  			}
  			free(newval);
  		}
- 		free(opt0);
  	}
  
  
--- 1156,1163 ----
  				success = false;
  			}
  			free(newval);
+ 			free(opt0);
  		}
  	}
  
  
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to