From 17ee0e50b6375f4b9e92605810d10fba3612059e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 8 Dec 2017 21:08:11 +0100 Subject: [PATCH] cppcheck warnings and scope reductions. --- src/bindings.c | 12 +++--------- src/economy.c | 6 ++---- src/json.test.c | 15 +++++++++------ src/kernel/alliance.c | 2 +- src/kernel/building.c | 2 +- src/kernel/item.c | 2 +- src/kernel/save.c | 2 +- src/kernel/ship.c | 2 +- src/kernel/unit.c | 5 ++--- src/kernel/version.c | 2 +- src/kernel/xmlreader.c | 9 +++++---- src/laws.c | 21 +++++++++++---------- src/modules/autoseed.c | 2 +- src/move.c | 3 ++- src/util/attrib.c | 2 +- src/util/lists.c | 10 ---------- src/util/lists.h | 6 ------ 17 files changed, 42 insertions(+), 61 deletions(-) diff --git a/src/bindings.c b/src/bindings.c index 7a24a3d07..14f0c96f3 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -710,7 +710,6 @@ static int config_get_resource(lua_State * L) lua_settable(L, -3); } if (itype->construction) { - int i; lua_pushstring(L, "build_skill_min"); lua_pushinteger(L, itype->construction->minskill); lua_settable(L, -3); @@ -718,6 +717,7 @@ static int config_get_resource(lua_State * L) lua_pushstring(L, skillnames[itype->construction->skill]); lua_settable(L, -3); if (itype->construction->materials) { + int i; lua_pushstring(L, "materials"); lua_newtable(L); for (i = 0; itype->construction->materials[i].number; ++i) { @@ -774,7 +774,6 @@ static int config_get_btype(lua_State * L) lua_settable(L, -3); } if (btype->construction) { - int i; lua_pushstring(L, "build_skill_min"); lua_pushinteger(L, btype->construction->minskill); lua_settable(L, -3); @@ -782,6 +781,7 @@ static int config_get_btype(lua_State * L) lua_pushstring(L, skillnames[btype->construction->skill]); lua_settable(L, -3); if (btype->construction->materials) { + int i; lua_pushstring(L, "materials"); lua_newtable(L); for (i = 0; btype->construction->materials[i].number; ++i) { @@ -840,7 +840,6 @@ static int config_get_stype(lua_State * L) } } if (stype->construction) { - int i; lua_pushstring(L, "build_skill_min"); lua_pushinteger(L, stype->construction->minskill); lua_settable(L, -3); @@ -848,6 +847,7 @@ static int config_get_stype(lua_State * L) lua_pushstring(L, skillnames[stype->construction->skill]); lua_settable(L, -3); if (stype->construction->materials) { + int i; lua_pushstring(L, "materials"); lua_newtable(L); for (i = 0; stype->construction->materials[i].number; ++i) { @@ -922,12 +922,6 @@ int tolua_read_xml(lua_State * L) return 1; } -typedef struct event_args { - int hfunction; - int hargs; - const char *sendertype; -} event_args; - static int tolua_report_unit(lua_State * L) { char buffer[512]; diff --git a/src/economy.c b/src/economy.c index 55e0fafb1..759d60cb8 100644 --- a/src/economy.c +++ b/src/economy.c @@ -1763,8 +1763,6 @@ static void expandselling(region * r, econ_request * sellorders, int limit) price = ltype->price * multi; if (money >= price) { - int abgezogenhafen = 0; - int abgezogensteuer = 0; unit *u = g_requests[j].unit; item *itm; attrib *a = a_find(u->attribs, &at_luxuries); @@ -1779,7 +1777,7 @@ static void expandselling(region * r, econ_request * sellorders, int limit) if (hafenowner != NULL) { if (hafenowner->faction != u->faction) { - abgezogenhafen = price / 10; + int abgezogenhafen = price / 10; hafencollected += abgezogenhafen; price -= abgezogenhafen; money -= abgezogenhafen; @@ -1787,7 +1785,7 @@ static void expandselling(region * r, econ_request * sellorders, int limit) } if (maxb != NULL) { if (maxowner->faction != u->faction) { - abgezogensteuer = price * tax_per_size[maxeffsize] / 100; + int abgezogensteuer = price * tax_per_size[maxeffsize] / 100; taxcollected += abgezogensteuer; price -= abgezogensteuer; money -= abgezogensteuer; diff --git a/src/json.test.c b/src/json.test.c index 57cb6e136..74b104329 100644 --- a/src/json.test.c +++ b/src/json.test.c @@ -1,19 +1,22 @@ #include -#include -#include -#include - -#include -#include #include "json.h" #include "tests.h" +#include +#include + +#include +#include + #include +#include + #include #include #include + static char *strip(char *str) { char *s = str, *b = str, *e = str; /* b is where text begins, e where it ends, s where we insert it. */ diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index ac981479d..f0cdf3bbd 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -422,7 +422,7 @@ const char *alliancename(const alliance * al) char *ibuf = idbuf[(++nextbuf) % 8]; if (al && al->name) { - slprintf(ibuf, sizeof(name), "%s (%s)", al->name, itoa36(al->id)); + slprintf(ibuf, sizeof(idbuf[0]), "%s (%s)", al->name, itoa36(al->id)); } else { return NULL; diff --git a/src/kernel/building.c b/src/kernel/building.c index 7eee3b9e2..ea01fe665 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -499,7 +499,7 @@ const char *buildingname(const building * b) static name idbuf[8]; static int nextbuf = 0; char *ibuf = idbuf[(++nextbuf) % 8]; - return write_buildingname(b, ibuf, sizeof(name)); + return write_buildingname(b, ibuf, sizeof(idbuf[0])); } void building_set_owner(struct unit * owner) diff --git a/src/kernel/item.c b/src/kernel/item.c index ac6875330..a633d5463 100644 --- a/src/kernel/item.c +++ b/src/kernel/item.c @@ -596,7 +596,7 @@ const resource_type *get_resourcetype(resource_t type) { static struct resource_type * rtypes[MAX_RESOURCES]; const resource_type *rtype = NULL; if (update != num_resources) { - memset(rtypes, 0, sizeof(rtypes)); + memset(rtypes, 0, sizeof(resource_type *) * MAX_RESOURCES); update = num_resources; } else { diff --git a/src/kernel/save.c b/src/kernel/save.c index ba7692806..a044601aa 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -301,7 +301,7 @@ int current_turn(void) perror(zText); } else { - int c = fscanf(F, "%d\n", &cturn); + int c = fscanf(F, "%4d\n", &cturn); fclose(F); if (c != 1) { return -1; diff --git a/src/kernel/ship.c b/src/kernel/ship.c index 764500b50..a55eada2e 100644 --- a/src/kernel/ship.c +++ b/src/kernel/ship.c @@ -386,7 +386,7 @@ const char *shipname(const ship * sh) static name idbuf[8]; static int nextbuf = 0; char *ibuf = idbuf[(++nextbuf) % 8]; - return write_shipname(sh, ibuf, sizeof(name)); + return write_shipname(sh, ibuf, sizeof(idbuf[0])); } int shipcapacity(const ship * sh) diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 07d1f5236..bc776befa 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1711,7 +1711,6 @@ void unit_addorder(unit * u, order * ord) int unit_max_hp(const unit * u) { int h; - double p; static int config; static bool rule_stamina; h = u_race(u)->hitpoints; @@ -1720,7 +1719,7 @@ int unit_max_hp(const unit * u) rule_stamina = config_get_int("rules.stamina", 1)!=0; } if (rule_stamina) { - p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2; + double p = pow(effskill(u, SK_STAMINA, u->region) / 2.0, 1.5) * 0.2; h += (int)(h * p + 0.5); } @@ -1890,7 +1889,7 @@ char *write_unitname(const unit * u, char *buffer, size_t size) const char *unitname(const unit * u) { char *ubuf = idbuf[(++nextbuf) % 8]; - return write_unitname(u, ubuf, sizeof(name)); + return write_unitname(u, ubuf, sizeof(idbuf[0])); } bool unit_name_equals_race(const unit *u) { diff --git a/src/kernel/version.c b/src/kernel/version.c index c7d757773..c0dc8d2bc 100644 --- a/src/kernel/version.c +++ b/src/kernel/version.c @@ -18,6 +18,6 @@ const char *eressea_version(void) { int version_no(const char *str) { int maj = 0, min = 0, pat = 0; - sscanf(str, "%d.%d.%d", &maj, &min, &pat); + sscanf(str, "%4d.%4d.%4d", &maj, &min, &pat); return (maj << 16) | (min << 8) | pat; } diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 92112ddb6..051bc0999 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -284,13 +284,13 @@ static int parse_buildings(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc); xmlXPathObjectPtr buildings; - int i; /* reading eressea/buildings/building */ buildings = xmlXPathEvalExpression(BAD_CAST "/eressea/buildings/building", xpath); if (buildings->nodesetval != NULL) { xmlNodeSetPtr nodes = buildings->nodesetval; + int i; for (i = 0; i != nodes->nodeNr; ++i) { xmlNodePtr node = nodes->nodeTab[i]; xmlChar *propValue; @@ -396,12 +396,12 @@ static int parse_ships(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc); xmlXPathObjectPtr ships; - int i; /* reading eressea/ships/ship */ ships = xmlXPathEvalExpression(BAD_CAST "/eressea/ships/ship", xpath); if (ships->nodesetval != NULL) { xmlNodeSetPtr nodes = ships->nodesetval; + int i; for (i = 0; i != nodes->nodeNr; ++i) { xmlNodePtr child, node = nodes->nodeTab[i]; xmlChar *propValue; @@ -810,7 +810,6 @@ static int parse_resources(xmlDocPtr doc) resource_type *rtype; unsigned int flags = RTF_NONE; xmlXPathObjectPtr result; - int k; if (xml_bvalue(node, "pooled", true)) flags |= RTF_POOLED; @@ -828,7 +827,8 @@ static int parse_resources(xmlDocPtr doc) /* reading eressea/resources/resource/function */ xpath->node = node; result = xmlXPathEvalExpression(BAD_CAST "function", xpath); - if (result->nodesetval != NULL) + if (result->nodesetval != NULL) { + int k; for (k = 0; k != result->nodesetval->nodeNr; ++k) { xmlNodePtr node = result->nodesetval->nodeTab[k]; pf_generic fun; @@ -852,6 +852,7 @@ static int parse_resources(xmlDocPtr doc) } xmlFree(propValue); } + } xmlXPathFreeObject(result); if (xml_bvalue(node, "material", false)) { diff --git a/src/laws.c b/src/laws.c index 72b7c4f38..f03f76730 100644 --- a/src/laws.c +++ b/src/laws.c @@ -433,14 +433,15 @@ static void horses(region * r) rsethorses(r, (int)(horses * 0.9)); } else if (maxhorses) { - int i; double growth = (RESOURCE_QUANTITY * HORSEGROWTH * 200 * (maxhorses - horses)) / maxhorses; if (growth > 0) { - if (a_find(r->attribs, &at_horseluck)) + int i; + if (a_find(r->attribs, &at_horseluck)) { growth *= 2; + } /* printf("Horses: <%d> %d -> ", growth, horses); */ i = (int)(0.5 + (horses * 0.0001) * growth); /* printf("%d\n", horses); */ @@ -562,13 +563,13 @@ growing_trees_e3(region * r, const int current_season, static void growing_trees(region * r, const int current_season, const int last_weeks_season) { - int growth, grownup_trees, i, seeds, sprout; - direction_t d; + int grownup_trees, i, seeds, sprout; attrib *a; if (current_season == SEASON_SUMMER || current_season == SEASON_AUTUMN) { double seedchance = 0.01F * RESOURCE_QUANTITY; int elves = count_race(r, get_race(RC_ELF)); + direction_t d; a = a_find(r->attribs, &at_germs); if (a && last_weeks_season == SEASON_SPRING) { @@ -638,6 +639,7 @@ growing_trees(region * r, const int current_season, const int last_weeks_season) } else if (current_season == SEASON_SPRING) { + int growth; if (is_cursed(r->attribs, &ct_godcursezone)) return; @@ -2153,7 +2155,6 @@ int email_cmd(unit * u, struct order *ord) int password_cmd(unit * u, struct order *ord) { char pwbuf[32]; - int i; const char *s; bool pwok = true; @@ -2161,6 +2162,7 @@ int password_cmd(unit * u, struct order *ord) s = gettoken(pwbuf, sizeof(pwbuf)); if (!s || !*s) { + int i; for (i = 0; i < 6; i++) pwbuf[i] = (char)(97 + rng_int() % 26); pwbuf[6] = 0; @@ -4272,7 +4274,7 @@ void update_subscriptions(void) int subscription, fno; faction *f; - if (fscanf(F, "%d %4s", &subscription, zFaction) <= 0) + if (fscanf(F, "%4d %4s", &subscription, zFaction) <= 0) break; fno = atoi36(zFaction); f = findfaction(fno); @@ -4358,7 +4360,7 @@ bool cansee_unit(const unit * u, const unit * target, int modifier) else if (target->faction == u->faction) return true; else { - int n, rings, o; + int n, rings; if (is_guard(target) || usiege(target) || target->building || target->ship) { @@ -4375,7 +4377,7 @@ bool cansee_unit(const unit * u, const unit * target, int modifier) return false; } if (skill_enabled(SK_PERCEPTION)) { - o = effskill(u, SK_PERCEPTION, target->region); + int o = effskill(u, SK_PERCEPTION, target->region); if (o >= n) { return true; } @@ -4394,7 +4396,6 @@ cansee_durchgezogen(const faction * f, const region * r, const unit * u, /* und es muss niemand aus f in der region sein, wenn sie vom Turm * erblickt wird */ { - int n; unit *u2; if (fval(u_race(u), RCF_INVISIBLE) || u->number == 0) @@ -4402,7 +4403,7 @@ cansee_durchgezogen(const faction * f, const region * r, const unit * u, else if (u->faction == f) return true; else { - int rings; + int rings, n; if (is_guard(u) || usiege(u) || u->building || u->ship) { return true; diff --git a/src/modules/autoseed.c b/src/modules/autoseed.c index 24153870a..1e877459e 100644 --- a/src/modules/autoseed.c +++ b/src/modules/autoseed.c @@ -155,7 +155,7 @@ newfaction *read_newfactions(const char *filename) email[0] = '\0'; password[0] = '\0'; - if (sscanf(buf, "%54s %19s %7s %15s %d %d", email, race, lang, + if (sscanf(buf, "%54s %19s %7s %15s %4d %4d", email, race, lang, password, &subscription, &alliance) < 3) { break; } diff --git a/src/move.c b/src/move.c index 289d7a421..798c3da27 100644 --- a/src/move.c +++ b/src/move.c @@ -1764,9 +1764,10 @@ static void sail(unit * u, order * ord, region_list ** routep, bool drifting) } if (!flying_ship(sh)) { + int reason; if (storms_enabled) { int stormchance = 0; - int stormyness, reason; + int stormyness; gamedate date; get_gamedate(turn, &date); stormyness = storms ? storms[date.month] * 5 : 0; diff --git a/src/util/attrib.c b/src/util/attrib.c index 10ec660f8..9ef03ca55 100644 --- a/src/util/attrib.c +++ b/src/util/attrib.c @@ -537,5 +537,5 @@ void a_write_orig(struct storage *store, const attrib * attribs, const void *own void attrib_done(void) { cb_clear(&cb_deprecated); - memset(at_hash, 0, sizeof at_hash); + memset(at_hash, 0, sizeof(at_hash[0]) * MAXATHASH); } diff --git a/src/util/lists.c b/src/util/lists.c index be3deeffd..98dac3711 100644 --- a/src/util/lists.c +++ b/src/util/lists.c @@ -76,16 +76,6 @@ void translist(void *l1, void *l2, void *p) addlist(l2, p); } -void insertlist(void_list ** l, void_list * p) -{ - - /* insert entry p at the beginning of list l */ - - p->next = *l; - *l = p; - -} - void removelist(void *l, void *p) { diff --git a/src/util/lists.h b/src/util/lists.h index 8b9b093df..75920c5d4 100644 --- a/src/util/lists.h +++ b/src/util/lists.h @@ -33,14 +33,8 @@ extern "C" { void freestrlist(strlist * s); void addlist(void *l1, void *p1); void translist(void *l1, void *l2, void *p); -#ifndef MALLOCDBG void freelist(void *p1); void removelist(void *l, void *p); -#else -#define freelist(p) { while (p) { void * p2 = p->next; free(p); p = p2; } } -#define removelist(l,p) { choplist(l, p); free(p); } -#endif - unsigned int listlen(void *l); #ifdef __cplusplus