44 * A simple benchmark program for PostgreSQL
55 * Originally written by Tatsuo Ishii and enhanced by many contributors.
66 *
7- * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.94 2010/01/02 16:57:32 momjian Exp $
7+ * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.95 2010/01/06 01:12:14 itagaki Exp $
88 * Copyright (c) 2000-2010, PostgreSQL Global Development Group
99 * ALL RIGHTS RESERVED;
1010 *
@@ -431,8 +431,23 @@ getVariable(CState *st, char *name)
431431return NULL ;
432432}
433433
434+ /* check whether the name consists of alphabets, numerals and underscores. */
435+ static bool
436+ isLegalVariableName (const char * name )
437+ {
438+ int i ;
439+
440+ for (i = 0 ;name [i ]!= '\0' ;i ++ )
441+ {
442+ if (!isalnum ((unsignedchar )name [i ])&& name [i ]!= '_' )
443+ return false;
444+ }
445+
446+ return true;
447+ }
448+
434449static int
435- putVariable (CState * st ,char * name ,char * value )
450+ putVariable (CState * st ,const char * context , char * name ,char * value )
436451{
437452Variable key ,
438453* var ;
@@ -452,14 +467,24 @@ putVariable(CState *st, char *name, char *value)
452467{
453468Variable * newvars ;
454469
470+ /*
471+ * Check for the name only when declaring a new variable to avoid
472+ * overhead.
473+ */
474+ if (!isLegalVariableName (name ))
475+ {
476+ fprintf (stderr ,"%s: invalid variable name '%s'\n" ,context ,name );
477+ return false;
478+ }
479+
455480if (st -> variables )
456481newvars = (Variable * )realloc (st -> variables ,
457482(st -> nvariables + 1 )* sizeof (Variable ));
458483else
459484newvars = (Variable * )malloc (sizeof (Variable ));
460485
461486if (newvars == NULL )
462- return false ;
487+ goto out_of_memory ;
463488
464489st -> variables = newvars ;
465490
@@ -493,6 +518,10 @@ putVariable(CState *st, char *name, char *value)
493518}
494519
495520return true;
521+
522+ out_of_memory :
523+ fprintf (stderr ,"%s: out of memory for variable '%s'\n" ,context ,name );
524+ return false;
496525}
497526
498527static char *
@@ -687,11 +716,8 @@ runShellCommand(CState *st, char *variable, char **argv, int argc)
687716return false;
688717}
689718snprintf (res ,sizeof (res ),"%d" ,retval );
690- if (!putVariable (st ,variable ,res ))
691- {
692- fprintf (stderr ,"%s: out of memory\n" ,argv [0 ]);
719+ if (!putVariable (st ,"setshell" ,variable ,res ))
693720return false;
694- }
695721
696722#ifdef DEBUG
697723printf ("shell parameter name: %s, value: %s\n" ,argv [1 ],res );
@@ -987,9 +1013,8 @@ doCustom(CState *st, instr_time *conn_time)
9871013#endif
9881014snprintf (res ,sizeof (res ),"%d" ,getrand (min ,max ));
9891015
990- if (putVariable (st ,argv [1 ],res )== false )
1016+ if (! putVariable (st ,argv [0 ], argv [ 1 ],res ))
9911017{
992- fprintf (stderr ,"%s: out of memory\n" ,argv [0 ]);
9931018st -> ecnt ++ ;
9941019return true;
9951020}
@@ -1057,9 +1082,8 @@ doCustom(CState *st, instr_time *conn_time)
10571082}
10581083}
10591084
1060- if (putVariable (st ,argv [1 ],res )== false )
1085+ if (! putVariable (st ,argv [0 ], argv [ 1 ],res ))
10611086{
1062- fprintf (stderr ,"%s: out of memory\n" ,argv [0 ]);
10631087st -> ecnt ++ ;
10641088return true;
10651089}
@@ -1874,11 +1898,8 @@ main(int argc, char **argv)
18741898}
18751899
18761900* p ++ = '\0' ;
1877- if (putVariable (& state [0 ],optarg ,p )== false)
1878- {
1879- fprintf (stderr ,"Couldn't allocate memory for variable\n" );
1901+ if (!putVariable (& state [0 ],"option" ,optarg ,p ))
18801902exit (1 );
1881- }
18821903}
18831904break ;
18841905case 'F' :
@@ -1958,11 +1979,8 @@ main(int argc, char **argv)
19581979state [i ].id = i ;
19591980for (j = 0 ;j < state [0 ].nvariables ;j ++ )
19601981{
1961- if (putVariable (& state [i ],state [0 ].variables [j ].name ,state [0 ].variables [j ].value )== false)
1962- {
1963- fprintf (stderr ,"Couldn't allocate memory for variable\n" );
1982+ if (!putVariable (& state [i ],"startup" ,state [0 ].variables [j ].name ,state [0 ].variables [j ].value ))
19641983exit (1 );
1965- }
19661984}
19671985}
19681986}
@@ -2039,11 +2057,8 @@ main(int argc, char **argv)
20392057snprintf (val ,sizeof (val ),"%d" ,scale );
20402058for (i = 0 ;i < nclients ;i ++ )
20412059{
2042- if (putVariable (& state [i ],"scale" ,val )== false)
2043- {
2044- fprintf (stderr ,"Couldn't allocate memory for variable\n" );
2060+ if (!putVariable (& state [i ],"startup" ,"scale" ,val ))
20452061exit (1 );
2046- }
20472062}
20482063}
20492064