Commit 08a46ad4 authored by Tom Lane's avatar Tom Lane

Fix some more boundary-case errors in psql variable substitution:

wasn't really right for case where :var is at the end of the line,
was definitely not right if var expanded to empty in that case,
and failed to recalculate thislen before jumping back to rescan.
parent c3c54a21
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* *
* Copyright 2000 by PostgreSQL Global Development Group * Copyright 2000 by PostgreSQL Global Development Group
* *
* $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.48 2002/06/15 19:37:48 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/mainloop.c,v 1.49 2002/07/03 16:47:46 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "mainloop.h" #include "mainloop.h"
...@@ -366,41 +366,46 @@ MainLoop(FILE *source) ...@@ -366,41 +366,46 @@ MainLoop(FILE *source)
* temporarily overwritten */ * temporarily overwritten */
in_length = strspn(&line[i + thislen], VALID_VARIABLE_CHARS); in_length = strspn(&line[i + thislen], VALID_VARIABLE_CHARS);
/* mark off the possible variable name */
after = line[i + thislen + in_length]; after = line[i + thislen + in_length];
line[i + thislen + in_length] = '\0'; line[i + thislen + in_length] = '\0';
/*
* if the variable doesn't exist we'll leave the string as
* is
*/
value = GetVariable(pset.vars, &line[i + thislen]); value = GetVariable(pset.vars, &line[i + thislen]);
/* restore overwritten character */
line[i + thislen + in_length] = after;
if (value) if (value)
{ {
/* It is a variable, perform substitution */
out_length = strlen(value); out_length = strlen(value);
/* Allow for 'after' character also 2002-05-27 */ new = malloc(len + out_length - in_length + 1);
new = malloc(len + out_length - (1 + in_length) + 1 + 1);
if (!new) if (!new)
{ {
psql_error("out of memory\n"); psql_error("out of memory\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
sprintf(new, "%.*s%s%c", i, line, value, after); sprintf(new, "%.*s%s%s", i, line, value,
if (after) &line[i + thislen + in_length]);
strcat(new, line + i + 1 + in_length + 1);
free(line); free(line);
line = new; line = new;
len = strlen(new); len = strlen(new);
if (i < len)
{
thislen = PQmblen(line+i, pset.encoding);
goto rescan; /* reparse the just substituted */ goto rescan; /* reparse the just substituted */
} }
}
else else
{ {
/* restore overwritten character */ /*
line[i + thislen + in_length] = after; * if the variable doesn't exist we'll leave the string as
/* move on ... */ * is ... move on ...
*/
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment