seedrng: remove unnecessary zero-filling of local variables

function                                             old     new   delta
seedrng_main                                        1292    1273     -19

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2022-04-27 17:20:43 +02:00
parent 6da9947358
commit 401356511c

View File

@ -54,20 +54,23 @@
#define CREDITABLE_SEED_NAME "seed.credit" #define CREDITABLE_SEED_NAME "seed.credit"
#define NON_CREDITABLE_SEED_NAME "seed.no-credit" #define NON_CREDITABLE_SEED_NAME "seed.no-credit"
enum seedrng_lengths { enum {
MIN_SEED_LEN = SHA256_OUTSIZE, MIN_SEED_LEN = SHA256_OUTSIZE,
MAX_SEED_LEN = 512 MAX_SEED_LEN = 512
}; };
static size_t determine_optimal_seed_len(void) static size_t determine_optimal_seed_len(void)
{ {
char poolsize_str[11] = { 0 }; char poolsize_str[12];
unsigned long poolsize; unsigned poolsize;
int n;
if (open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1) < 0) { n = open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1);
if (n < 0) {
bb_perror_msg("unable to determine pool size, assuming %u bits", MIN_SEED_LEN * 8); bb_perror_msg("unable to determine pool size, assuming %u bits", MIN_SEED_LEN * 8);
return MIN_SEED_LEN; return MIN_SEED_LEN;
} }
poolsize_str[n] = '\0';
poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8; poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8;
return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN); return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN);
} }
@ -159,7 +162,7 @@ static int seed_from_file_if_exists(const char *filename, int dfd, bool credit,
int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
int seedrng_main(int argc UNUSED_PARAM, char *argv[]) int seedrng_main(int argc UNUSED_PARAM, char *argv[])
{ {
const char *seed_dir = DEFAULT_SEED_DIR, *creditable_seed, *non_creditable_seed; const char *seed_dir, *creditable_seed, *non_creditable_seed;
int fd, dfd, program_ret = 0; int fd, dfd, program_ret = 0;
uint8_t new_seed[MAX_SEED_LEN]; uint8_t new_seed[MAX_SEED_LEN];
size_t new_seed_len; size_t new_seed_len;
@ -178,10 +181,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
; ;
#endif #endif
seed_dir = DEFAULT_SEED_DIR;
skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n; skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n;
creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME);
non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME);
umask(0077); umask(0077);
if (getuid()) if (getuid())
bb_simple_error_msg_and_die(bb_msg_you_must_be_root); bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
@ -200,6 +201,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
clock_gettime(CLOCK_BOOTTIME, &timestamp); clock_gettime(CLOCK_BOOTTIME, &timestamp);
sha256_hash(&hash, &timestamp, sizeof(timestamp)); sha256_hash(&hash, &timestamp, sizeof(timestamp));
creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME);
non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME);
for (int i = 1; i < 3; ++i) { for (int i = 1; i < 3; ++i) {
if (seed_from_file_if_exists(i == 1 ? non_creditable_seed : creditable_seed, if (seed_from_file_if_exists(i == 1 ? non_creditable_seed : creditable_seed,
dfd, i == 1 ? false : !skip_credit, &hash) < 0) dfd, i == 1 ? false : !skip_credit, &hash) < 0)