From 1b49f60ded6b11b51667c48dd714fdf4264ccff0 Mon Sep 17 00:00:00 2001 From: PatR Date: Tue, 29 Jan 2019 15:00:07 -0800 Subject: [PATCH 1/3] warning fix The new code provoked several warnings; this fixes one of them. Moving the declaration of 'rolecount' would have been sufficient, but I've gone another way. --- include/extern.h | 2 +- src/pray.c | 14 ++++++-------- src/role.c | 15 +++++++++++---- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/include/extern.h b/include/extern.h index f67d09c85..6b5da1758 100644 --- a/include/extern.h +++ b/include/extern.h @@ -2122,7 +2122,7 @@ E boolean FDECL(validrole, (int)); E boolean FDECL(validrace, (int, int)); E boolean FDECL(validgend, (int, int, int)); E boolean FDECL(validalign, (int, int, int)); -E int NDECL(randrole); +E int FDECL(randrole, (BOOLEAN_P)); E int FDECL(randrace, (int)); E int FDECL(randgend, (int, int)); E int FDECL(randalign, (int, int)); diff --git a/src/pray.c b/src/pray.c index ff9532fbd..29cf878a4 100644 --- a/src/pray.c +++ b/src/pray.c @@ -2095,15 +2095,13 @@ aligntyp alignment; if (!Hallucination) return align_gname(alignment); - /* Count the roles, so that we can pick one at random. */ - int rolecount = 0; - while (roles[rolecount].filecode) - rolecount++; - - /* The priest may not have initialized god names. If this is the - case, and we roll priest, we need to try again. */ + /* Some roles (Priest) don't have a pantheon unless we're playing as + that role, so keep trying until we get a role which does have one. + [If playing a Priest, the current pantheon will be twice as likely + to get picked as any of the others. That's not significant enough + to bother dealing with.] */ do - which = rn2_on_display_rng(rolecount); + which = randrole(TRUE); while (!roles[which].lgod); switch (rn2_on_display_rng(9)) { diff --git a/src/role.c b/src/role.c index 2a2acce53..f263609a1 100644 --- a/src/role.c +++ b/src/role.c @@ -787,9 +787,16 @@ int rolenum; } int -randrole() +randrole(for_display) +boolean for_display; { - return rn2(SIZE(roles) - 1); + int res = SIZE(roles) - 1; + + if (for_display) + res = rn2_on_display_rng(res); + else + res = rn2(res); + return res; } STATIC_OVL int @@ -805,7 +812,7 @@ randrole_filtered() && ok_gend(i, ROLE_NONE, ROLE_RANDOM, ROLE_NONE) && ok_align(i, ROLE_NONE, ROLE_NONE, ROLE_RANDOM)) set[n++] = i; - return n ? set[rn2(n)] : randrole(); + return n ? set[rn2(n)] : randrole(FALSE); } int @@ -2088,7 +2095,7 @@ role_init() if (flags.pantheon == -1) { /* new game */ flags.pantheon = flags.initrole; /* use own gods */ while (!roles[flags.pantheon].lgod) /* unless they're missing */ - flags.pantheon = randrole(); + flags.pantheon = randrole(FALSE); } if (!urole.lgod) { urole.lgod = roles[flags.pantheon].lgod; From 39b6a7e5154081b13a24707f8a768bfb6b5fef9a Mon Sep 17 00:00:00 2001 From: PatR Date: Tue, 29 Jan 2019 15:18:17 -0800 Subject: [PATCH 2/3] another warning fix Comparison between signed and unsigned. Compiler stupidity since the range of possible values that signed 'i' can take is limited and never negative. --- src/rnd.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/rnd.c b/src/rnd.c index fa8ce0e20..8f92e42ab 100644 --- a/src/rnd.c +++ b/src/rnd.c @@ -17,16 +17,16 @@ struct rnglist_t { isaac64_ctx rng_state; }; -enum {CORE = 0, DISP}; +enum { CORE = 0, DISP = 1 }; static struct rnglist_t rnglist[] = { - {rn2, FALSE, {0}}, /* CORE */ - {rn2_on_display_rng, FALSE, {0}}, /* DISP */ + { rn2, FALSE, { 0 } }, /* CORE */ + { rn2_on_display_rng, FALSE, { 0 } }, /* DISP */ }; int whichrng(fn) -int (*fn)(int); +int FDECL((*fn), (int)); { int i; @@ -39,19 +39,21 @@ int (*fn)(int); void init_isaac64(seed, fn) unsigned long seed; -int FDECL((*fn),(int)); +int FDECL((*fn), (int)); { - unsigned char new_rng_state[sizeof(seed)]; - int i, rngindx = whichrng(fn); + unsigned char new_rng_state[sizeof seed]; + unsigned i; + int rngindx = whichrng(fn); if (rngindx < 0) panic("Bad rng function passed to init_isaac64()."); - for (i=0; i>= 8; } - isaac64_init(&rnglist[rngindx].rng_state, new_rng_state, sizeof(seed)); + isaac64_init(&rnglist[rngindx].rng_state, new_rng_state, + (int) sizeof seed); } static int From cf49b6fe8714000f94f486c032e6b2f560ccace1 Mon Sep 17 00:00:00 2001 From: PatR Date: Tue, 29 Jan 2019 15:27:22 -0800 Subject: [PATCH 3/3] last warning fix Compiler gave four diagnostics about 'seed' being used uninitialized if 'no_seed' were false, but two of those were alternate suggestions for how to suppress them. --- sys/unix/unixmain.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sys/unix/unixmain.c b/sys/unix/unixmain.c index 1d6500b68..c800c4919 100644 --- a/sys/unix/unixmain.c +++ b/sys/unix/unixmain.c @@ -768,18 +768,18 @@ error: unsigned long sys_random_seed() { - unsigned long seed; + unsigned long seed = 0L; unsigned long pid = (unsigned long) getpid(); boolean no_seed = TRUE; #ifdef DEV_RANDOM - FILE *fptr = NULL; + FILE *fptr; fptr = fopen(DEV_RANDOM, "r"); if (fptr) { - fread(&seed, sizeof(long), 1, fptr); + fread(&seed, sizeof long, 1, fptr); has_strong_rngseed = TRUE; /* decl.c */ no_seed = FALSE; - fclose(fptr); + (void) fclose(fptr); } else { /* leaves clue, doesn't exit */ paniclog("sys_random_seed", "falling back to weak seed");