pgbench: Make set_random_seed() 64-bit everywhere.
authorPeter Eisentraut <peter@eisentraut.org>
Sat, 29 Mar 2025 14:24:42 +0000 (15:24 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Sat, 29 Mar 2025 14:24:42 +0000 (15:24 +0100)
Delete an intermediate variable, a redundant cast, a use of long and a
use of long long.  scanf() the seed directly into a uint64, now that we
can do that with SCNu64 from <inttypes.h>.

The previous coding was from pre-C99 times when %lld might not have been
there, so it read into an unsigned long.  Therefore behavior varied
by OS, and --random-seed would accept either 32 or 64 bit seeds.  Now
it's the same everywhere.

Author: Thomas Munro <thomas.munro@gmail.com>
Discussion: https://postgr.es/m/b936d2fb-590d-49c3-a615-92c3a88c6c19%40eisentraut.org

src/bin/pgbench/pgbench.c

index c17c728ebb4f13caea4713713c3986fa7aff60f4..497a936c141f3025dbc115e9ea949110dfc140ee 100644 (file)
@@ -6634,27 +6634,23 @@ set_random_seed(const char *seed)
    }
    else
    {
-       /* parse unsigned-int seed value */
-       unsigned long ulseed;
        char        garbage;
 
-       /* Don't try to use UINT64_FORMAT here; it might not work for sscanf */
-       if (sscanf(seed, "%lu%c", &ulseed, &garbage) != 1)
+       if (sscanf(seed, "%" SCNu64 "%c", &iseed, &garbage) != 1)
        {
            pg_log_error("unrecognized random seed option \"%s\"", seed);
            pg_log_error_detail("Expecting an unsigned integer, \"time\" or \"rand\".");
            return false;
        }
-       iseed = (uint64) ulseed;
    }
 
    if (seed != NULL)
-       pg_log_info("setting random seed to %llu", (unsigned long long) iseed);
+       pg_log_info("setting random seed to %" PRIu64, iseed);
 
    random_seed = iseed;
 
    /* Initialize base_random_sequence using seed */
-   pg_prng_seed(&base_random_sequence, (uint64) iseed);
+   pg_prng_seed(&base_random_sequence, iseed);
 
    return true;
 }