Commit 4057b64f authored by Tom Lane's avatar Tom Lane

Modify readfuncs so that recursive use of stringToNode will not crash

and burn.  Just for added luck, change reading of CONST nodes so that
we do not need to consult pg_type rows while reading them; this means
that no database access occurs during stringToNode.  This requires
changing the order in which const-node fields are written, which means
an initdb is forced.
parent 14022014
/* /*
*
* outfuncs.c * outfuncs.c
* routines to convert a node to ascii representation * routines to convert a node to ascii representation
* *
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.135 2000/12/03 20:45:33 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.136 2001/01/07 01:08:47 tgl Exp $
* *
* NOTES * NOTES
* Every (plan) node in POSTGRES has an associated "out" routine which * Every (plan) node in POSTGRES has an associated "out" routine which
* knows how to create its ascii representation. These functions are * knows how to create its ascii representation. These functions are
* useful for debugging as well as for storing plans in the system * useful for debugging as well as for storing plans in the system
* catalogs (eg. indexes). This is also the plan string sent out in * catalogs (eg. views).
* Mariposa.
*
* These functions update the in/out argument of type StringInfo
* passed to them. This argument contains the string holding the ASCII
* representation plus some other information (string length, etc.)
*
*/ */
#include "postgres.h" #include "postgres.h"
#include <ctype.h> #include <ctype.h>
#include "access/heapam.h"
#include "access/htup.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "lib/stringinfo.h" #include "lib/stringinfo.h"
#include "nodes/execnodes.h"
#include "nodes/nodes.h" #include "nodes/nodes.h"
#include "nodes/parsenodes.h" #include "nodes/parsenodes.h"
#include "nodes/pg_list.h"
#include "nodes/plannodes.h" #include "nodes/plannodes.h"
#include "nodes/primnodes.h" #include "nodes/primnodes.h"
#include "nodes/relation.h" #include "nodes/relation.h"
#include "parser/parse.h" #include "parser/parse.h"
#include "utils/datum.h" #include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
static void _outDatum(StringInfo str, Datum value, Oid type); static void _outDatum(StringInfo str, Datum value, int typlen, bool typbyval);
static void _outNode(StringInfo str, void *obj); static void _outNode(StringInfo str, void *obj);
/* /*
...@@ -63,8 +48,8 @@ _outToken(StringInfo str, char *s) ...@@ -63,8 +48,8 @@ _outToken(StringInfo str, char *s)
/* /*
* Look for characters or patterns that are treated specially by * Look for characters or patterns that are treated specially by
* read.c (either in lsptok() or in nodeRead()), and therefore need a * read.c (either in pg_strtok() or in nodeRead()), and therefore need
* protective backslash. * a protective backslash.
*/ */
/* These characters only need to be quoted at the start of the string */ /* These characters only need to be quoted at the start of the string */
if (*s == '<' || if (*s == '<' ||
...@@ -762,18 +747,17 @@ static void ...@@ -762,18 +747,17 @@ static void
_outConst(StringInfo str, Const *node) _outConst(StringInfo str, Const *node)
{ {
appendStringInfo(str, appendStringInfo(str,
" CONST :consttype %u :constlen %d :constisnull %s :constvalue ", " CONST :consttype %u :constlen %d :constbyval %s"
" :constisnull %s :constvalue ",
node->consttype, node->consttype,
node->constlen, node->constlen,
node->constbyval ? "true" : "false",
node->constisnull ? "true" : "false"); node->constisnull ? "true" : "false");
if (node->constisnull) if (node->constisnull)
appendStringInfo(str, "<>"); appendStringInfo(str, "<>");
else else
_outDatum(str, node->constvalue, node->consttype); _outDatum(str, node->constvalue, node->constlen, node->constbyval);
appendStringInfo(str, " :constbyval %s ",
node->constbyval ? "true" : "false");
} }
/* /*
...@@ -1234,38 +1218,31 @@ _outJoinInfo(StringInfo str, JoinInfo *node) ...@@ -1234,38 +1218,31 @@ _outJoinInfo(StringInfo str, JoinInfo *node)
* Print the value of a Datum given its type. * Print the value of a Datum given its type.
*/ */
static void static void
_outDatum(StringInfo str, Datum value, Oid type) _outDatum(StringInfo str, Datum value, int typlen, bool typbyval)
{ {
int16 typeLength; Size length,
bool byValue; i;
Size length;
char *s; char *s;
int i;
/* length = datumGetSize(value, typbyval, typlen);
* find some information about the type and the "real" length of the
* datum.
*/
get_typlenbyval(type, &typeLength, &byValue);
length = datumGetSize(value, byValue, typeLength);
if (byValue) if (typbyval)
{ {
s = (char *) (&value); s = (char *) (&value);
appendStringInfo(str, " %u [ ", (unsigned int) length); appendStringInfo(str, " %u [ ", (unsigned int) length);
for (i = 0; i < (int) sizeof(Datum); i++) for (i = 0; i < (Size) sizeof(Datum); i++)
appendStringInfo(str, "%d ", (int) (s[i])); appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfo(str, "] "); appendStringInfo(str, "] ");
} }
else else
{ /* !byValue */ {
s = (char *) DatumGetPointer(value); s = (char *) DatumGetPointer(value);
if (!PointerIsValid(s)) if (!PointerIsValid(s))
appendStringInfo(str, " 0 [ ] "); appendStringInfo(str, " 0 [ ] ");
else else
{ {
appendStringInfo(str, " %u [ ", (unsigned int) length); appendStringInfo(str, " %u [ ", (unsigned int) length);
for (i = 0; i < (int) length; i++) for (i = 0; i < length; i++)
appendStringInfo(str, "%d ", (int) (s[i])); appendStringInfo(str, "%d ", (int) (s[i]));
appendStringInfo(str, "] "); appendStringInfo(str, "] ");
} }
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.26 2000/12/03 20:45:33 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/nodes/read.c,v 1.27 2001/01/07 01:08:47 tgl Exp $
* *
* HISTORY * HISTORY
* AUTHOR DATE MAJOR EVENT * AUTHOR DATE MAJOR EVENT
...@@ -25,6 +25,11 @@ ...@@ -25,6 +25,11 @@
#include "nodes/pg_list.h" #include "nodes/pg_list.h"
#include "nodes/readfuncs.h" #include "nodes/readfuncs.h"
/* Static state for pg_strtok */
static char *pg_strtok_ptr = NULL;
/* /*
* stringToNode - * stringToNode -
* returns a Node with a given legal ASCII representation * returns a Node with a given legal ASCII representation
...@@ -32,10 +37,22 @@ ...@@ -32,10 +37,22 @@
void * void *
stringToNode(char *str) stringToNode(char *str)
{ {
char *save_strtok;
void *retval; void *retval;
lsptok(str, NULL); /* set the string used in lsptok */ /*
retval = nodeRead(true); /* start reading */ * We save and restore the pre-existing state of pg_strtok.
* This makes the world safe for re-entrant invocation of stringToNode,
* without incurring a lot of notational overhead by having to pass the
* next-character pointer around through all the readfuncs.c code.
*/
save_strtok = pg_strtok_ptr;
pg_strtok_ptr = str; /* point pg_strtok at the string to read */
retval = nodeRead(true); /* do the reading */
pg_strtok_ptr = save_strtok;
return retval; return retval;
} }
...@@ -47,7 +64,7 @@ stringToNode(char *str) ...@@ -47,7 +64,7 @@ stringToNode(char *str)
*****************************************************************************/ *****************************************************************************/
/* /*
* lsptok --- retrieve next "token" from a string. * pg_strtok --- retrieve next "token" from a string.
* *
* Works kinda like strtok, except it never modifies the source string. * Works kinda like strtok, except it never modifies the source string.
* (Instead of storing nulls into the string, the length of the token * (Instead of storing nulls into the string, the length of the token
...@@ -55,9 +72,7 @@ stringToNode(char *str) ...@@ -55,9 +72,7 @@ stringToNode(char *str)
* Also, the rules about what is a token are hard-wired rather than being * Also, the rules about what is a token are hard-wired rather than being
* configured by passing a set of terminating characters. * configured by passing a set of terminating characters.
* *
* The string is initially set by passing a non-NULL "string" value, * The string is assumed to have been initialized already by stringToNode.
* and subsequent calls with string==NULL read the previously given value.
* (Pass length==NULL to set the string without reading its first token.)
* *
* The rules for tokens are: * The rules for tokens are:
* * Whitespace (space, tab, newline) always separates tokens. * * Whitespace (space, tab, newline) always separates tokens.
...@@ -89,20 +104,12 @@ stringToNode(char *str) ...@@ -89,20 +104,12 @@ stringToNode(char *str)
* as a single token. * as a single token.
*/ */
char * char *
lsptok(char *string, int *length) pg_strtok(int *length)
{ {
static char *saved_str = NULL;
char *local_str; /* working pointer to string */ char *local_str; /* working pointer to string */
char *ret_str; /* start of token to return */ char *ret_str; /* start of token to return */
if (string != NULL) local_str = pg_strtok_ptr;
{
saved_str = string;
if (length == NULL)
return NULL;
}
local_str = saved_str;
while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t') while (*local_str == ' ' || *local_str == '\n' || *local_str == '\t')
local_str++; local_str++;
...@@ -110,7 +117,7 @@ lsptok(char *string, int *length) ...@@ -110,7 +117,7 @@ lsptok(char *string, int *length)
if (*local_str == '\0') if (*local_str == '\0')
{ {
*length = 0; *length = 0;
saved_str = local_str; pg_strtok_ptr = local_str;
return NULL; /* no more tokens */ return NULL; /* no more tokens */
} }
...@@ -147,7 +154,7 @@ lsptok(char *string, int *length) ...@@ -147,7 +154,7 @@ lsptok(char *string, int *length)
if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>') if (*length == 2 && ret_str[0] == '<' && ret_str[1] == '>')
*length = 0; *length = 0;
saved_str = local_str; pg_strtok_ptr = local_str;
return ret_str; return ret_str;
} }
...@@ -223,7 +230,7 @@ nodeTokenType(char *token, int length) ...@@ -223,7 +230,7 @@ nodeTokenType(char *token, int length)
} }
/* /*
* these three cases do not need length checks, since lsptok() will * these three cases do not need length checks, since pg_strtok() will
* always treat them as single-byte tokens * always treat them as single-byte tokens
*/ */
else if (*token == '(') else if (*token == '(')
...@@ -248,12 +255,13 @@ nodeTokenType(char *token, int length) ...@@ -248,12 +255,13 @@ nodeTokenType(char *token, int length)
* Slightly higher-level reader. * Slightly higher-level reader.
* *
* This routine applies some semantic knowledge on top of the purely * This routine applies some semantic knowledge on top of the purely
* lexical tokenizer lsptok(). It can read * lexical tokenizer pg_strtok(). It can read
* * Value token nodes (integers, floats, or strings); * * Value token nodes (integers, floats, or strings);
* * Plan nodes (via parsePlanString() from readfuncs.c); * * Plan nodes (via parsePlanString() from readfuncs.c);
* * Lists of the above. * * Lists of the above.
* *
* Secrets: He assumes that lsptok already has the string (see above). * We assume pg_strtok is already initialized with a string to read (hence
* this should only be invoked from within a stringToNode operation).
* Any callers should set read_car_only to true. * Any callers should set read_car_only to true.
*/ */
void * void *
...@@ -266,7 +274,7 @@ nodeRead(bool read_car_only) ...@@ -266,7 +274,7 @@ nodeRead(bool read_car_only)
*return_value; *return_value;
bool make_dotted_pair_cell = false; bool make_dotted_pair_cell = false;
token = lsptok(NULL, &tok_len); token = pg_strtok(&tok_len);
if (token == NULL) if (token == NULL)
return NULL; return NULL;
...@@ -277,8 +285,8 @@ nodeRead(bool read_car_only) ...@@ -277,8 +285,8 @@ nodeRead(bool read_car_only)
{ {
case PLAN_SYM: case PLAN_SYM:
this_value = parsePlanString(); this_value = parsePlanString();
token = lsptok(NULL, &tok_len); token = pg_strtok(&tok_len);
if (token[0] != '}') if (token == NULL || token[0] != '}')
elog(ERROR, "nodeRead: did not find '}' at end of plan node"); elog(ERROR, "nodeRead: did not find '}' at end of plan node");
if (!read_car_only) if (!read_car_only)
make_dotted_pair_cell = true; make_dotted_pair_cell = true;
......
This diff is collapsed.
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: catversion.h,v 1.67 2000/12/28 13:00:27 vadim Exp $ * $Id: catversion.h,v 1.68 2001/01/07 01:08:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -53,6 +53,6 @@ ...@@ -53,6 +53,6 @@
*/ */
/* yyyymmddN */ /* yyyymmddN */
#define CATALOG_VERSION_NO 200012280 #define CATALOG_VERSION_NO 200101061
#endif #endif
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: readfuncs.h,v 1.9 2000/01/26 05:58:17 momjian Exp $ * $Id: readfuncs.h,v 1.10 2001/01/07 01:08:48 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
/* /*
* prototypes for functions in read.c (the lisp token parser) * prototypes for functions in read.c (the lisp token parser)
*/ */
extern char *lsptok(char *string, int *length); extern char *pg_strtok(int *length);
extern char *debackslash(char *token, int length); extern char *debackslash(char *token, int length);
extern void *nodeRead(bool read_car_only); extern void *nodeRead(bool read_car_only);
......
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