4
4
* A simple benchmark program for PostgreSQL
5
5
* Originally written by Tatsuo Ishii and enhanced by many contributors.
6
6
*
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 $
8
8
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
9
9
* ALL RIGHTS RESERVED;
10
10
*
@@ -431,8 +431,23 @@ getVariable(CState *st, char *name)
431
431
return NULL ;
432
432
}
433
433
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
+
434
449
static int
435
- putVariable (CState * st ,char * name ,char * value )
450
+ putVariable (CState * st ,const char * context , char * name ,char * value )
436
451
{
437
452
Variable key ,
438
453
* var ;
@@ -452,14 +467,24 @@ putVariable(CState *st, char *name, char *value)
452
467
{
453
468
Variable * newvars ;
454
469
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
+
455
480
if (st -> variables )
456
481
newvars = (Variable * )realloc (st -> variables ,
457
482
(st -> nvariables + 1 )* sizeof (Variable ));
458
483
else
459
484
newvars = (Variable * )malloc (sizeof (Variable ));
460
485
461
486
if (newvars == NULL )
462
- return false ;
487
+ goto out_of_memory ;
463
488
464
489
st -> variables = newvars ;
465
490
@@ -493,6 +518,10 @@ putVariable(CState *st, char *name, char *value)
493
518
}
494
519
495
520
return true;
521
+
522
+ out_of_memory :
523
+ fprintf (stderr ,"%s: out of memory for variable '%s'\n" ,context ,name );
524
+ return false;
496
525
}
497
526
498
527
static char *
@@ -687,11 +716,8 @@ runShellCommand(CState *st, char *variable, char **argv, int argc)
687
716
return false;
688
717
}
689
718
snprintf (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 ))
693
720
return false;
694
- }
695
721
696
722
#ifdef DEBUG
697
723
printf ("shell parameter name: %s, value: %s\n" ,argv [1 ],res );
@@ -987,9 +1013,8 @@ doCustom(CState *st, instr_time *conn_time)
987
1013
#endif
988
1014
snprintf (res ,sizeof (res ),"%d" ,getrand (min ,max ));
989
1015
990
- if (putVariable (st ,argv [1 ],res )== false )
1016
+ if (! putVariable (st ,argv [0 ], argv [ 1 ],res ))
991
1017
{
992
- fprintf (stderr ,"%s: out of memory\n" ,argv [0 ]);
993
1018
st -> ecnt ++ ;
994
1019
return true;
995
1020
}
@@ -1057,9 +1082,8 @@ doCustom(CState *st, instr_time *conn_time)
1057
1082
}
1058
1083
}
1059
1084
1060
- if (putVariable (st ,argv [1 ],res )== false )
1085
+ if (! putVariable (st ,argv [0 ], argv [ 1 ],res ))
1061
1086
{
1062
- fprintf (stderr ,"%s: out of memory\n" ,argv [0 ]);
1063
1087
st -> ecnt ++ ;
1064
1088
return true;
1065
1089
}
@@ -1874,11 +1898,8 @@ main(int argc, char **argv)
1874
1898
}
1875
1899
1876
1900
* 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 ))
1880
1902
exit (1 );
1881
- }
1882
1903
}
1883
1904
break ;
1884
1905
case 'F' :
@@ -1958,11 +1979,8 @@ main(int argc, char **argv)
1958
1979
state [i ].id = i ;
1959
1980
for (j = 0 ;j < state [0 ].nvariables ;j ++ )
1960
1981
{
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 ))
1964
1983
exit (1 );
1965
- }
1966
1984
}
1967
1985
}
1968
1986
}
@@ -2039,11 +2057,8 @@ main(int argc, char **argv)
2039
2057
snprintf (val ,sizeof (val ),"%d" ,scale );
2040
2058
for (i = 0 ;i < nclients ;i ++ )
2041
2059
{
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 ))
2045
2061
exit (1 );
2046
- }
2047
2062
}
2048
2063
}
2049
2064