* A simple benchmark program for PostgreSQL
  * Originally written by Tatsuo Ishii and enhanced by many contributors.
  *
- * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.94 2010/01/02 16:57:32 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.95 2010/01/06 01:12:14 itagaki Exp $
  * Copyright (c) 2000-2010, PostgreSQL Global Development Group
  * ALL RIGHTS RESERVED;
  *
                return NULL;
 }
 
+/* check whether the name consists of alphabets, numerals and underscores. */
+static bool
+isLegalVariableName(const char *name)
+{
+       int             i;
+
+       for (i = 0; name[i] != '\0'; i++)
+       {
+               if (!isalnum((unsigned char) name[i]) && name[i] != '_')
+                       return false;
+       }
+
+       return true;
+}
+
 static int
-putVariable(CState *st, char *name, char *value)
+putVariable(CState *st, const char *context, char *name, char *value)
 {
        Variable        key,
                           *var;
        {
                Variable   *newvars;
 
+               /*
+                * Check for the name only when declaring a new variable to avoid
+                * overhead.
+                */
+               if (!isLegalVariableName(name))
+               {
+                       fprintf(stderr, "%s: invalid variable name '%s'\n", context, name);
+                       return false;
+               }
+
                if (st->variables)
                        newvars = (Variable *) realloc(st->variables,
                                                                        (st->nvariables + 1) * sizeof(Variable));
                        newvars = (Variable *) malloc(sizeof(Variable));
 
                if (newvars == NULL)
-                       return false;
+                       goto out_of_memory;
 
                st->variables = newvars;
 
        }
 
        return true;
+
+out_of_memory:
+       fprintf(stderr, "%s: out of memory for variable '%s'\n", context, name);
+       return false;
 }
 
 static char *
                return false;
        }
        snprintf(res, sizeof(res), "%d", retval);
-       if (!putVariable(st, variable, res))
-       {
-               fprintf(stderr, "%s: out of memory\n", argv[0]);
+       if (!putVariable(st, "setshell", variable, res))
                return false;
-       }
 
 #ifdef DEBUG
        printf("shell parameter name: %s, value: %s\n", argv[1], res);
 #endif
                        snprintf(res, sizeof(res), "%d", getrand(min, max));
 
-                       if (putVariable(st, argv[1], res) == false)
+                       if (!putVariable(st, argv[0], argv[1], res))
                        {
-                               fprintf(stderr, "%s: out of memory\n", argv[0]);
                                st->ecnt++;
                                return true;
                        }
                                }
                        }
 
-                       if (putVariable(st, argv[1], res) == false)
+                       if (!putVariable(st, argv[0], argv[1], res))
                        {
-                               fprintf(stderr, "%s: out of memory\n", argv[0]);
                                st->ecnt++;
                                return true;
                        }
                                        }
 
                                        *p++ = '\0';
-                                       if (putVariable(&state[0], optarg, p) == false)
-                                       {
-                                               fprintf(stderr, "Couldn't allocate memory for variable\n");
+                                       if (!putVariable(&state[0], "option", optarg, p))
                                                exit(1);
-                                       }
                                }
                                break;
                        case 'F':
                        state[i].id = i;
                        for (j = 0; j < state[0].nvariables; j++)
                        {
-                               if (putVariable(&state[i], state[0].variables[j].name, state[0].variables[j].value) == false)
-                               {
-                                       fprintf(stderr, "Couldn't allocate memory for variable\n");
+                               if (!putVariable(&state[i], "startup", state[0].variables[j].name, state[0].variables[j].value))
                                        exit(1);
-                               }
                        }
                }
        }
                snprintf(val, sizeof(val), "%d", scale);
                for (i = 0; i < nclients; i++)
                {
-                       if (putVariable(&state[i], "scale", val) == false)
-                       {
-                               fprintf(stderr, "Couldn't allocate memory for variable\n");
+                       if (!putVariable(&state[i], "startup", "scale", val))
                                exit(1);
-                       }
                }
        }