From 089be4bca8b1c3cfa4cbd755ea9a998f2f75f932 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 9 Dec 2018 03:42:08 +0100 Subject: [PATCH] cppcheck style warnings: reduce variable scope --- src/creport.c | 2 +- src/economy.c | 3 +-- src/exparse.c | 24 ++++++++++++--------- src/give.c | 1 - src/jsonconf.c | 4 +++- src/kernel/attrib.c | 10 ++++----- src/kernel/order.c | 3 +-- src/kernel/resources.c | 4 +--- src/kernel/save.c | 45 ++++++++++++++++++++------------------- src/kernel/unit.c | 6 ++---- src/laws.c | 2 +- src/magic.c | 8 +++---- src/main.c | 2 +- src/skill.c | 2 +- src/spells/combatspells.c | 5 ++--- src/spy.c | 3 +-- src/summary.c | 3 ++- src/tools/gethash.c | 36 ++++++++++++++++--------------- src/util/keyword.c | 2 +- src/util/mt19937ar.c | 4 ++-- src/util/rng.test.c | 18 ++++++---------- 21 files changed, 91 insertions(+), 96 deletions(-) diff --git a/src/creport.c b/src/creport.c index 9ec5530db..8f30fa50e 100644 --- a/src/creport.c +++ b/src/creport.c @@ -1157,7 +1157,7 @@ static char *cr_output_resource(char *buf, const resource_type *rtype, assert(rtype); name = resourcename(rtype, NMF_PLURAL); assert(name); - buf += sprintf(buf, "RESOURCE %u\n", str_hash(rtype->_name)); + buf += sprintf(buf, "RESOURCE %d\n", str_hash(rtype->_name)); tname = LOC(loc, name); assert(tname); tname = translate(name, tname); diff --git a/src/economy.c b/src/economy.c index 1d3f6685f..067c3e513 100644 --- a/src/economy.c +++ b/src/economy.c @@ -2239,7 +2239,6 @@ static void breed_cmd(unit * u, struct order *ord) const char *s; param_t p; region *r = u->region; - const resource_type *rtype = NULL; if (r->land == NULL) { ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_onlandonly", "")); @@ -2275,7 +2274,7 @@ static void breed_cmd(unit * u, struct order *ord) break; default: if (p != P_ANY) { - rtype = findresourcetype(s, u->faction->locale); + const resource_type *rtype = findresourcetype(s, u->faction->locale); if (rtype == get_resourcetype(R_SEED) || rtype == get_resourcetype(R_MALLORN_SEED)) { breedtrees(u, m); break; diff --git a/src/exparse.c b/src/exparse.c index 71efe0667..602bdf4e9 100644 --- a/src/exparse.c +++ b/src/exparse.c @@ -874,7 +874,6 @@ static void start_resources(parseinfo *pi, const XML_Char *el, const XML_Char ** } static void start_ships(parseinfo *pi, const XML_Char *el, const XML_Char **attr) { - const char *flag_names[] = { "opensea", "fly", "nocoast", "speedy", NULL }; if (xml_strequal(el, "ship")) { const XML_Char *name; @@ -917,10 +916,13 @@ static void start_ships(parseinfo *pi, const XML_Char *el, const XML_Char **attr else if (xml_strequal(attr[i], "storm")) { stype->storm = xml_float(attr[i + 1]); } - else if (!handle_flag(&flags, attr + i, flag_names)) { - /* we already handled the name earlier */ - if (!xml_strequal(attr[i], "name")) { - handle_bad_input(pi, el, attr[i]); + else { + const char *flag_names[] = { "opensea", "fly", "nocoast", "speedy", NULL }; + if (!handle_flag(&flags, attr + i, flag_names)) { + /* we already handled the name earlier */ + if (!xml_strequal(attr[i], "name")) { + handle_bad_input(pi, el, attr[i]); + } } } } @@ -1209,7 +1211,6 @@ static void start_races(parseinfo *pi, const XML_Char *el, const XML_Char **attr } static void start_buildings(parseinfo *pi, const XML_Char *el, const XML_Char **attr) { - const char *flag_names[] = { "nodestroy", "nobuild", "unique", "decay", "magic", "namechange", "fort", "oneperturn", NULL }; if (xml_strequal(el, "building")) { const XML_Char *name; @@ -1245,10 +1246,13 @@ static void start_buildings(parseinfo *pi, const XML_Char *el, const XML_Char ** /* magres is specified in percent! */ btype->magres = frac_make(xml_int(attr[i + 1]), 100); } - else if (!handle_flag(&flags, attr + i, flag_names)) { - /* we already handled the name earlier */ - if (!xml_strequal(attr[i], "name")) { - handle_bad_input(pi, el, attr[i]); + else { + const char *flag_names[] = { "nodestroy", "nobuild", "unique", "decay", "magic", "namechange", "fort", "oneperturn", NULL }; + if (!handle_flag(&flags, attr + i, flag_names)) { + /* we already handled the name earlier */ + if (!xml_strequal(attr[i], "name")) { + handle_bad_input(pi, el, attr[i]); + } } } } diff --git a/src/give.c b/src/give.c index 60b728479..90ed64f50 100644 --- a/src/give.c +++ b/src/give.c @@ -169,7 +169,6 @@ int give_quota(const unit * src, const unit * dst, const item_type * type, if (config_changed(&config)) { divisor = config_get_int("rules.items.give_divisor", divisor); } - assert(divisor <= 0 || divisor >= 1); if (divisor >= 1) { /* predictable > correct: */ return n / divisor; diff --git a/src/jsonconf.c b/src/jsonconf.c index ff442f990..48be28b16 100644 --- a/src/jsonconf.c +++ b/src/jsonconf.c @@ -1032,12 +1032,14 @@ static int add_po_string(const char *msgid, const char *msgstr, const char *msgc } static int include_po(const char *uri) { - char name[PATH_MAX], lname[8]; + char name[PATH_MAX]; const char *filename = uri_to_file(uri, name, sizeof(name)); const char *pos = strstr(filename, ".po"); if (pos) { size_t len; const char *str = --pos; + char lname[8]; + while (str > filename && *str != '.') --str; len = (size_t)(pos - str); if (len < sizeof(lname)) { diff --git a/src/kernel/attrib.c b/src/kernel/attrib.c index d84a8c8e9..997a04151 100644 --- a/src/kernel/attrib.c +++ b/src/kernel/attrib.c @@ -185,15 +185,15 @@ void at_register(attrib_type * at) static attrib_type *at_find_key(unsigned int hk) { - const char *translate[3][2] = { - { "zielregion", "targetregion" }, /* remapping: from 'zielregion, heute targetregion */ - { "verzaubert", "curse" }, /* remapping: früher verzaubert, jetzt curse */ - { NULL, NULL } - }; attrib_type *find = at_hash[hk % MAXATHASH]; while (find && hk != find->hashkey) find = find->nexthash; if (!find) { + const char *translate[3][2] = { + { "zielregion", "targetregion" }, /* remapping: from 'zielregion, heute targetregion */ + { "verzaubert", "curse" }, /* remapping: früher verzaubert, jetzt curse */ + { NULL, NULL } + }; int i = 0; while (translate[i][0]) { if (__at_hashkey(translate[i][0]) == hk) diff --git a/src/kernel/order.c b/src/kernel/order.c index 6b81f7dcf..b4467fa5b 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -170,7 +170,6 @@ const char *crescape(const char *str, char *buffer, size_t size) { int stream_order(struct stream *out, const struct order *ord, const struct locale *lang, bool escape) { const char *text; - order_data *od = NULL; keyword_t kwd = ORD_KEYWORD(ord); if (ord->command & CMD_QUIET) { @@ -203,7 +202,7 @@ int stream_order(struct stream *out, const struct order *ord, const struct local } } else { - od = odata_load(ord->id); + order_data *od = odata_load(ord->id); text = OD_STRING(od); if (text) { char obuf[1024]; diff --git a/src/kernel/resources.c b/src/kernel/resources.c index 4bb27616d..b5741236b 100644 --- a/src/kernel/resources.c +++ b/src/kernel/resources.c @@ -181,10 +181,8 @@ struct rawmaterial_type *rmt_get(const struct resource_type *rtype) struct rawmaterial_type *rmt_create(struct resource_type *rtype) { - rawmaterial_type *rmtype; - if (!rtype->raw) { - rmtype = rtype->raw = malloc(sizeof(rawmaterial_type)); + rawmaterial_type *rmtype = rtype->raw = malloc(sizeof(rawmaterial_type)); rmtype->rtype = rtype; rmtype->terraform = terraform_default; rmtype->update = NULL; diff --git a/src/kernel/save.c b/src/kernel/save.c index 70f40e0cf..1393d8da7 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -272,12 +272,11 @@ static void read_owner(gamedata *data, region_owner ** powner) static void write_owner(gamedata *data, region_owner * owner) { if (owner) { - faction *f; WRITE_INT(data->store, owner->since_turn); if (owner->since_turn >= 0) { + faction *f = owner->last_owner; WRITE_INT(data->store, owner->morale_turn); WRITE_INT(data->store, owner->flags); - f = owner->last_owner; write_faction_reference((f && f->_alive) ? f : NULL, data->store); f = owner->owner; write_faction_reference((f && f->_alive) ? f : NULL, data->store); @@ -887,12 +886,12 @@ int get_spell_level_faction(const spell * sp, void * cbdata) } static char * getpasswd(int fno) { - const char *prefix = itoa36(fno); - size_t len = strlen(prefix); FILE * F = fopen("passwords.txt", "r"); - char line[80]; if (F) { + const char *prefix = itoa36(fno); + size_t len = strlen(prefix); while (!feof(F)) { + char line[80]; fgets(line, sizeof(line), F); if (line[len] == ':' && strncmp(prefix, line, len) == 0) { size_t slen = strlen(line) - 1; @@ -1147,7 +1146,7 @@ static int cb_sb_maxlevel(spellbook_entry *sbe, void *cbdata) { int readgame(const char *filename) { - int n, stream_version; + int n = -2, stream_version; char path[PATH_MAX]; gamedata gdata = { 0 }; storage store; @@ -1164,23 +1163,25 @@ int readgame(const char *filename) return -1; } sz = fread(&gdata.version, sizeof(int), 1, F); - sz = fread(&stream_version, sizeof(int), 1, F); - assert((sz == 1 && stream_version == STREAM_VERSION) || !"unsupported data format"); - assert(gdata.version >= MIN_VERSION || !"unsupported data format"); - assert(gdata.version <= MAX_VERSION || !"unsupported data format"); + if (sz == 1) { + sz = fread(&stream_version, sizeof(int), 1, F); + assert((sz == 1 && stream_version == STREAM_VERSION) || !"unsupported data format"); + assert(gdata.version >= MIN_VERSION || !"unsupported data format"); + assert(gdata.version <= MAX_VERSION || !"unsupported data format"); - fstream_init(&strm, F); - binstore_init(&store, &strm); - gdata.store = &store; + fstream_init(&strm, F); + binstore_init(&store, &strm); + gdata.store = &store; - if (gdata.version >= BUILDNO_VERSION) { - int build; - READ_INT(&store, &build); - log_debug("data in %s created with build %d.", filename, build); + if (gdata.version >= BUILDNO_VERSION) { + int build; + READ_INT(&store, &build); + log_debug("data in %s created with build %d.", filename, build); + } + n = read_game(&gdata); + binstore_done(&store); + fstream_done(&strm); } - n = read_game(&gdata); - binstore_done(&store); - fstream_done(&strm); return n; } @@ -1350,8 +1351,6 @@ int read_game(gamedata *data) int p, nread; faction *f, **fp; region *r; - building **bp; - ship **shp; unit *u; storage * store = data->store; const struct building_type *bt_lighthouse = bt_find("lighthouse"); @@ -1407,6 +1406,8 @@ int read_game(gamedata *data) while (--nread >= 0) { unit **up; + building **bp; + ship **shp; r = read_region(data); diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 748c76003..bf2ad7a37 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -776,7 +776,6 @@ void clone_men(const unit * u, unit * dst, int n) /* "hat attackiert"-status wird uebergeben */ if (dst) { - skill *sv, *sn; skill_t sk; ship *sh; @@ -784,9 +783,8 @@ void clone_men(const unit * u, unit * dst, int n) for (sk = 0; sk != MAXSKILLS; ++sk) { int weeks, level = 0; - - sv = unit_skill(u, sk); - sn = unit_skill(dst, sk); + skill *sv = unit_skill(u, sk); + skill *sn = unit_skill(dst, sk); if (sv == NULL && sn == NULL) continue; diff --git a/src/laws.c b/src/laws.c index 4a153fc16..5620606e4 100644 --- a/src/laws.c +++ b/src/laws.c @@ -3388,8 +3388,8 @@ int pay_cmd(unit * u, struct order *ord) static int reserve_i(unit * u, struct order *ord, int flags) { - char token[128]; if (u->number > 0) { + char token[128]; int use, count, para; const item_type *itype; const char *s; diff --git a/src/magic.c b/src/magic.c index ebaac3583..98d5dc05d 100644 --- a/src/magic.c +++ b/src/magic.c @@ -1334,7 +1334,6 @@ static void do_fumble(castorder * co) int level = co->level; int duration; double effect; - static const race *rc_toad; static int rc_cache; fumble_f fun; @@ -1359,6 +1358,7 @@ static void do_fumble(castorder * co) * The list of things to happen are attached to a timeout * trigger and that's added to the triggerlit of the mage gone toad. */ + static const race *rc_toad; trigger *trestore = trigger_changerace(mage, u_race(mage), mage->irace); if (chance(0.7)) { const resource_type *rtype = rt_find("toadslime"); @@ -2119,10 +2119,9 @@ void add_castorder(spellrank * cll, castorder * co) void free_castorders(castorder * co) { - castorder *co2; while (co) { - co2 = co; + castorder *co2 = co; co = co->next; free_castorder(co2); free(co2); @@ -2202,15 +2201,14 @@ void remove_familiar(unit * mage) { attrib *a = a_find(mage->attribs, &at_familiar); attrib *an; - skillmod_data *smd; if (a != NULL) { a_remove(&mage->attribs, a); } a = a_find(mage->attribs, &at_skillmod); while (a && a->type == &at_skillmod) { + skillmod_data *smd = (skillmod_data *)a->data.v; an = a->next; - smd = (skillmod_data *)a->data.v; if (smd->special == sm_familiar) { a_remove(&mage->attribs, a); } diff --git a/src/main.c b/src/main.c index 0dec070ce..bd5a92b46 100644 --- a/src/main.c +++ b/src/main.c @@ -108,11 +108,11 @@ static const char * valid_keys[] = { static dictionary *parse_config(const char *filename) { - char path[PATH_MAX]; dictionary *d; const char *str, *cfgpath = config_get("config.path"); if (cfgpath) { + char path[PATH_MAX]; path_join(cfgpath, filename, path, sizeof(path)); log_debug("reading from configuration file %s\n", path); d = iniparser_load(path); diff --git a/src/skill.c b/src/skill.c index abeecca04..41656f0ff 100644 --- a/src/skill.c +++ b/src/skill.c @@ -94,9 +94,9 @@ skill_t findskill(const char *name) skill_t get_skill(const char *s, const struct locale * lang) { skill_t result = NOSKILL; - char buffer[64]; if (s) { + char buffer[64]; char * str = transliterate(buffer, sizeof(buffer) - sizeof(int), s); if (str) { int i; diff --git a/src/spells/combatspells.c b/src/spells/combatspells.c index 21b33e7bf..296d88563 100644 --- a/src/spells/combatspells.c +++ b/src/spells/combatspells.c @@ -357,8 +357,6 @@ int sp_sleep(struct castorder * co) const spell * sp = co->sp; battle *b = fi->side->battle; unit *mage = fi->unit; - unit *du; - troop dt; int force, enemies; int k = 0; message *m; @@ -374,7 +372,8 @@ int sp_sleep(struct castorder * co) return 0; } while (force && enemies) { - dt = select_enemy(fi, FIGHT_ROW, BEHIND_ROW, SELECT_ADVANCE); + unit *du; + troop dt = select_enemy(fi, FIGHT_ROW, BEHIND_ROW, SELECT_ADVANCE); assert(dt.fighter); du = dt.fighter->unit; if (!is_magic_resistant(mage, du, 0)) { diff --git a/src/spy.c b/src/spy.c index c0d4d614c..b03896094 100644 --- a/src/spy.c +++ b/src/spy.c @@ -217,7 +217,6 @@ int setstealth_cmd(unit * u, struct order *ord) { char token[64]; const char *s; - int level; init_order_depr(ord); s = gettoken(token, sizeof(token)); @@ -231,7 +230,7 @@ int setstealth_cmd(unit * u, struct order *ord) if (isdigit(*(const unsigned char *)s)) { /* Tarnungslevel setzen */ - level = atoi((const char *)s); + int level = atoi((const char *)s); if (level > effskill(u, SK_STEALTH, 0)) { ADDMSG(&u->faction->msgs, msg_feedback(u, ord, "error_lowstealth", "")); return 0; diff --git a/src/summary.c b/src/summary.c index 64bb055b5..e01679d56 100644 --- a/src/summary.c +++ b/src/summary.c @@ -77,11 +77,12 @@ int *nmrs = NULL; int update_nmrs(void) { - int i, newplayers = 0; + int newplayers = 0; faction *f; int timeout = NMRTimeout(); if (timeout>0) { + int i; if (nmrs == NULL) { nmrs = malloc(sizeof(int) * (timeout + 1)); } diff --git a/src/tools/gethash.c b/src/tools/gethash.c index 75054b11e..f4385a495 100644 --- a/src/tools/gethash.c +++ b/src/tools/gethash.c @@ -4,21 +4,23 @@ int main(int argc, char **argv) { - char key[4]; - char code[4]; - char result[4]; - int a, i, rot; - for (a = 1; a < argc; ++a) { - const char *str = argv[a]; - size_t len = strlen(str); - str = str + len - 6; - memcpy(key, str, 3); - memcpy(code, str + 3, 3); - result[3] = key[3] = code[3] = 0; - rot = atoi(key); - for (i = 0; i != 3; ++i) - result[(i + rot) % 3] = ((code[i] + 10 - key[i]) % 10) + '0'; - printf("%s %s\n", argv[a], result); - } - return 0; + char key[4]; + char code[4]; + char result[4]; + int a; + for (a = 1; a < argc; ++a) { + int rot, i; + const char *str = argv[a]; + size_t len = strlen(str); + str = str + len - 6; + memcpy(key, str, 3); + memcpy(code, str + 3, 3); + result[3] = key[3] = code[3] = 0; + rot = atoi(key); + for (i = 0; i != 3; ++i) { + result[(i + rot) % 3] = ((code[i] + 10 - key[i]) % 10) + '0'; + } + printf("%s %s\n", argv[a], result); + } + return 0; } diff --git a/src/util/keyword.c b/src/util/keyword.c index 0d9a11dc4..9d5ba1e6a 100644 --- a/src/util/keyword.c +++ b/src/util/keyword.c @@ -52,13 +52,13 @@ keyword_t findkeyword(const char *s) { keyword_t get_keyword(const char *s, const struct locale *lang) { keyword_t result = NOKEYWORD; - char buffer[64]; assert(lang); assert(s); while (*s == '@') ++s; if (*s) { + char buffer[64]; char *str = transliterate(buffer, sizeof(buffer) - sizeof(int), s); if (str) { diff --git a/src/util/mt19937ar.c b/src/util/mt19937ar.c index 0dda925ca..ad58d2ca8 100644 --- a/src/util/mt19937ar.c +++ b/src/util/mt19937ar.c @@ -100,10 +100,10 @@ void init_by_array(unsigned long init_key[], int key_length) unsigned long genrand_int32(void) { unsigned long y; - static unsigned long mag01[2]={0x0UL, MATRIX_A}; - /* mag01[x] = x * MATRIX_A for x=0,1 */ if (mti >= N) { /* generate N words at one time */ + static unsigned long mag01[2] = { 0x0UL, MATRIX_A }; + /* mag01[x] = x * MATRIX_A for x=0,1 */ int kk; if (mti == N+1) /* if init_genrand() has not been called, */ diff --git a/src/util/rng.test.c b/src/util/rng.test.c index 34e530c5e..6ab8719a1 100644 --- a/src/util/rng.test.c +++ b/src/util/rng.test.c @@ -8,17 +8,13 @@ static void test_rng_round(CuTest * tc) { - double f; - int i, r; - for (i = 0; i < 1000; ++i) { - f = rng_double(); - r = RAND_ROUND(f); - CuAssertTrue(tc, f >= 0); - CuAssertTrue(tc, r <= (int)f + 1); - CuAssertTrue(tc, r >= (int)f); - CuAssertTrue(tc, r == (int)r); - CuAssertTrue(tc, r == RAND_ROUND(r)); - } + double f = rng_double(); + int r = RAND_ROUND(f); + CuAssertTrue(tc, f >= 0); + CuAssertTrue(tc, r <= (int)f + 1); + CuAssertTrue(tc, r >= (int)f); + CuAssertTrue(tc, r == (int)r); + CuAssertTrue(tc, r == RAND_ROUND(r)); } CuSuite *get_rng_suite(void)