Commit 5635c7aa authored by Tom Lane's avatar Tom Lane

Fix portability and translatability issues in commit 64f85894.

Compilation failed for lack of an #ifdef on builds without
pg_strong_random().  Also fix relevant error messages to meet
project style guidelines.

Fabien Coelho, further adjusted by me

Discussion: https://postgr.es/m/32390.1522464534@sss.pgh.pa.us
parent b0c90c85
...@@ -4669,8 +4669,8 @@ printResults(TState *threads, StatsData *total, instr_time total_time, ...@@ -4669,8 +4669,8 @@ printResults(TState *threads, StatsData *total, instr_time total_time,
} }
/* call srandom based on some seed. NULL triggers the default behavior. */ /* call srandom based on some seed. NULL triggers the default behavior. */
static void static bool
set_random_seed(const char *seed, const char *origin) set_random_seed(const char *seed)
{ {
/* srandom expects an unsigned int */ /* srandom expects an unsigned int */
unsigned int iseed; unsigned int iseed;
...@@ -4685,10 +4685,14 @@ set_random_seed(const char *seed, const char *origin) ...@@ -4685,10 +4685,14 @@ set_random_seed(const char *seed, const char *origin)
else if (strcmp(seed, "rand") == 0) else if (strcmp(seed, "rand") == 0)
{ {
/* use some "strong" random source */ /* use some "strong" random source */
#ifdef HAVE_STRONG_RANDOM
if (!pg_strong_random(&iseed, sizeof(iseed))) if (!pg_strong_random(&iseed, sizeof(iseed)))
#endif
{ {
fprintf(stderr, "cannot seed random from a strong source\n"); fprintf(stderr,
exit(1); "cannot seed random from a strong source, none available: "
"use \"time\" or an unsigned integer value.\n");
return false;
} }
} }
else else
...@@ -4698,9 +4702,9 @@ set_random_seed(const char *seed, const char *origin) ...@@ -4698,9 +4702,9 @@ set_random_seed(const char *seed, const char *origin)
if (sscanf(seed, "%u%c", &iseed, &garbage) != 1) if (sscanf(seed, "%u%c", &iseed, &garbage) != 1)
{ {
fprintf(stderr, fprintf(stderr,
"error while scanning '%s' from %s, expecting an unsigned integer, 'time' or 'rand'\n", "unrecognized random seed option \"%s\": expecting an unsigned integer, \"time\" or \"rand\"\n",
seed, origin); seed);
exit(1); return false;
} }
} }
...@@ -4709,6 +4713,7 @@ set_random_seed(const char *seed, const char *origin) ...@@ -4709,6 +4713,7 @@ set_random_seed(const char *seed, const char *origin)
srandom(iseed); srandom(iseed);
/* no precision loss: 32 bit unsigned int cast to 64 bit int */ /* no precision loss: 32 bit unsigned int cast to 64 bit int */
random_seed = iseed; random_seed = iseed;
return true;
} }
...@@ -4823,7 +4828,11 @@ main(int argc, char **argv) ...@@ -4823,7 +4828,11 @@ main(int argc, char **argv)
memset(state, 0, sizeof(CState)); memset(state, 0, sizeof(CState));
/* set random seed early, because it may be used while parsing scripts. */ /* set random seed early, because it may be used while parsing scripts. */
set_random_seed(getenv("PGBENCH_RANDOM_SEED"), "PGBENCH_RANDOM_SEED environment variable"); if (!set_random_seed(getenv("PGBENCH_RANDOM_SEED")))
{
fprintf(stderr, "error while setting random seed from PGBENCH_RANDOM_SEED environment variable\n");
exit(1);
}
while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1) while ((c = getopt_long(argc, argv, "iI:h:nvp:dqb:SNc:j:Crs:t:T:U:lf:D:F:M:P:R:L:", long_options, &optindex)) != -1)
{ {
...@@ -5099,7 +5108,11 @@ main(int argc, char **argv) ...@@ -5099,7 +5108,11 @@ main(int argc, char **argv)
break; break;
case 9: /* random-seed */ case 9: /* random-seed */
benchmarking_option_set = true; benchmarking_option_set = true;
set_random_seed(optarg, "--random-seed option"); if (!set_random_seed(optarg))
{
fprintf(stderr, "error while setting random seed from --random-seed option\n");
exit(1);
}
break; break;
default: default:
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname); fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
......
...@@ -111,7 +111,8 @@ my @options = ( ...@@ -111,7 +111,8 @@ my @options = (
[qr{unrecognized initialization step}, [qr{unrecognized initialization step},
qr{allowed steps are} ] ], qr{allowed steps are} ] ],
[ 'bad random seed', '--random-seed=one', [ 'bad random seed', '--random-seed=one',
[qr{error while scanning 'one' from --random-seed option, expecting an unsigned integer} ] ], [qr{unrecognized random seed option "one": expecting an unsigned integer, "time" or "rand"},
qr{error while setting random seed from --random-seed option} ] ],
# loging sub-options # loging sub-options
[ 'sampling => log', '--sampling-rate=0.01', [ 'sampling => log', '--sampling-rate=0.01',
......
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