static analysis warnings.

PVS-Studio warnings reduced or suppressed.
This commit is contained in:
Enno Rehling 2016-11-23 18:56:40 +01:00
parent ea6cd2ca32
commit 7f9313f1a7
12 changed files with 56 additions and 54 deletions

View file

@ -295,7 +295,7 @@ static void a_initeffect(attrib * a)
a->data.v = calloc(sizeof(effect_data), 1); a->data.v = calloc(sizeof(effect_data), 1);
} }
static void a_finalizeeffect(attrib * a) static void a_finalizeeffect(attrib * a) //-V524
{ {
free(a->data.v); free(a->data.v);
} }

View file

@ -3984,7 +3984,7 @@ static bool start_battle(region * r, battle ** bp)
while (a && a->type == &at_curse) { while (a && a->type == &at_curse) {
curse *c = (curse *)a->data.v; curse *c = (curse *)a->data.v;
if (c->type == calm_ct if (c->type == calm_ct
&& curse_geteffect(c) == u2->faction->subscription) { && curse_geteffect_int(c) == u2->faction->subscription) {
if (curse_active(c)) { if (curse_active(c)) {
calm = true; calm = true;
break; break;
@ -4275,9 +4275,7 @@ void do_battle(region * r)
/* Hier ist das Gefecht beendet, und wir k<>nnen die /* Hier ist das Gefecht beendet, und wir k<>nnen die
* Hilfsstrukturen * wieder l<EFBFBD>schen: */ * Hilfsstrukturen * wieder l<EFBFBD>schen: */
if (b) { free_battle(b);
free_battle(b);
}
} }
void do_battles(void) { void do_battles(void) {

View file

@ -72,7 +72,7 @@ extern "C" {
unsigned char relations[MAXSIDES]; unsigned char relations[MAXSIDES];
struct side *enemies[MAXSIDES]; struct side *enemies[MAXSIDES];
struct fighter *fighters; struct fighter *fighters;
int index; /* Eintrag der Fraktion in b->matrix/b->enemies */ unsigned int index; /* Eintrag der Fraktion in b->matrix/b->enemies */
int size[NUMROWS]; /* Anzahl Personen in Reihe X. 0 = Summe */ int size[NUMROWS]; /* Anzahl Personen in Reihe X. 0 = Summe */
int nonblockers[NUMROWS]; /* Anzahl nichtblockierender Kaempfer, z.B. Schattenritter. */ int nonblockers[NUMROWS]; /* Anzahl nichtblockierender Kaempfer, z.B. Schattenritter. */
int alive; /* Die Partei hat den Kampf verlassen */ int alive; /* Die Partei hat den Kampf verlassen */

View file

@ -1,8 +1,9 @@
#include "platform.h" #include "platform.h"
#include <util/base36.h>
#include "json.h" #include "json.h"
#include <util/base36.h>
#include <util/log.h>
#include <kernel/plane.h> #include <kernel/plane.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/faction.h> #include <kernel/faction.h>
@ -22,12 +23,20 @@ int json_import(struct stream * out) {
assert(out && out->api); assert(out && out->api);
while (!out->api->readln(out->handle, buffer, sizeof(buffer))) { while (!out->api->readln(out->handle, buffer, sizeof(buffer))) {
size_t len = strlen(buffer); size_t len = strlen(buffer);
data = (char *)realloc(data, sz + len + 1); char *tmp;
tmp = (char *)realloc(data, sz + len + 1);
if (!tmp) {
log_fatal("allocation failure in json_import");
free(data);
return 1;
}
data = tmp;
memcpy(data + sz, buffer, len); memcpy(data + sz, buffer, len);
sz += len; sz += len;
data[sz] = 0; data[sz] = 0;
} }
json = cJSON_Parse(data); json = cJSON_Parse(data);
free(data);
child = cJSON_GetObjectItem(json, "regions"); child = cJSON_GetObjectItem(json, "regions");
if (child && child->type == cJSON_Object) { if (child && child->type == cJSON_Object) {
cJSON *j; cJSON *j;
@ -64,7 +73,7 @@ int json_export(stream * out, int flags) {
cJSON_AddItemToObject(root, "planes", json = cJSON_CreateObject()); cJSON_AddItemToObject(root, "planes", json = cJSON_CreateObject());
for (p = planes; p; p = p->next) { for (p = planes; p; p = p->next) {
cJSON *data; cJSON *data;
_snprintf(id, sizeof(id), "%u", p->id); _snprintf(id, sizeof(id), "%d", p->id);
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject()); cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
cJSON_AddNumberToObject(data, "x", p->minx); cJSON_AddNumberToObject(data, "x", p->minx);
cJSON_AddNumberToObject(data, "y", p->miny); cJSON_AddNumberToObject(data, "y", p->miny);
@ -76,7 +85,7 @@ int json_export(stream * out, int flags) {
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject()); cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
for (r = regions; r; r = r->next) { for (r = regions; r; r = r->next) {
cJSON *data; cJSON *data;
_snprintf(id, sizeof(id), "%u", r->uid); _snprintf(id, sizeof(id), "%d", r->uid);
cJSON_AddItemToObject(json, id, data = cJSON_CreateObject()); cJSON_AddItemToObject(json, id, data = cJSON_CreateObject());
cJSON_AddNumberToObject(data, "x", r->x); cJSON_AddNumberToObject(data, "x", r->x);
cJSON_AddNumberToObject(data, "y", r->y); cJSON_AddNumberToObject(data, "y", r->y);

View file

@ -648,7 +648,7 @@ growing_trees(region * r, const int current_season, const int last_weeks_season)
* verfügbaren Fläche ab. In Gletschern gibt es weniger * verfügbaren Fläche ab. In Gletschern gibt es weniger
* Möglichkeiten als in Ebenen. */ * Möglichkeiten als in Ebenen. */
sprout = 0; sprout = 0;
seedchance = (1000 * region_maxworkers(r2)) / (double)r2->terrain->size; seedchance = (1000.0 * region_maxworkers(r2)) / r2->terrain->size;
for (i = 0; i < seeds / MAXDIRECTIONS; i++) { for (i = 0; i < seeds / MAXDIRECTIONS; i++) {
if (rng_int() % 10000 < seedchance) if (rng_int() % 10000 < seedchance)
sprout++; sprout++;
@ -1269,7 +1269,6 @@ static void remove_idle_players(void)
} else { } else {
if (fval(f, FFL_NOIDLEOUT)) { if (fval(f, FFL_NOIDLEOUT)) {
f->lastorders = turn; f->lastorders = turn;
fp = &f->next;
} }
else if (turn != f->lastorders) { else if (turn != f->lastorders) {
char info[256]; char info[256];
@ -2995,9 +2994,9 @@ static void ageing(void)
for (up = &r->units; *up;) { for (up = &r->units; *up;) {
unit *u = *up; unit *u = *up;
a_age(&u->attribs, u); a_age(&u->attribs, u);
if (u == *up) if (u == *up)
handle_event(u->attribs, "timer", u); handle_event(u->attribs, "timer", u);
if (u == *up) if (u == *up) //-V581
up = &(*up)->next; up = &(*up)->next;
} }
@ -3007,7 +3006,7 @@ static void ageing(void)
a_age(&s->attribs, s); a_age(&s->attribs, s);
if (s == *sp) if (s == *sp)
handle_event(s->attribs, "timer", s); handle_event(s->attribs, "timer", s);
if (s == *sp) if (s == *sp) //-V581
sp = &(*sp)->next; sp = &(*sp)->next;
} }

View file

@ -173,7 +173,7 @@ static void a_initicastle(struct attrib *a)
a->data.v = calloc(sizeof(icastle_data), 1); a->data.v = calloc(sizeof(icastle_data), 1);
} }
static void a_finalizeicastle(struct attrib *a) static void a_finalizeicastle(struct attrib *a) //-V524
{ {
free(a->data.v); free(a->data.v);
} }

View file

@ -1,6 +1,8 @@
#include <platform.h> #include <platform.h>
#include "prefix.h" #include "prefix.h"
#include <util/log.h>
#include <assert.h> #include <assert.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
@ -10,7 +12,7 @@ char **race_prefixes = NULL;
static size_t size = 4; static size_t size = 4;
static unsigned int next = 0; static unsigned int next = 0;
void add_raceprefix(const char *prefix) int add_raceprefix(const char *prefix)
{ {
assert(prefix); assert(prefix);
if (race_prefixes == NULL) { if (race_prefixes == NULL) {
@ -19,11 +21,18 @@ void add_raceprefix(const char *prefix)
race_prefixes = malloc(size * sizeof(char *)); race_prefixes = malloc(size * sizeof(char *));
} }
if (next + 1 == size) { if (next + 1 == size) {
char **tmp;
tmp = realloc(race_prefixes, 2 * size * sizeof(char *));
if (!tmp) {
log_fatal("allocation failure");
return 1;
}
race_prefixes = tmp;
size *= 2; size *= 2;
race_prefixes = realloc(race_prefixes, size * sizeof(char *));
} }
race_prefixes[next++] = _strdup(prefix); race_prefixes[next++] = _strdup(prefix);
race_prefixes[next] = NULL; race_prefixes[next] = NULL;
return 0;
} }
void free_prefixes(void) { void free_prefixes(void) {

View file

@ -7,7 +7,7 @@
extern "C" { extern "C" {
#endif #endif
void add_raceprefix(const char *); int add_raceprefix(const char *);
char **race_prefixes; // zero-terminated array of valid prefixes char **race_prefixes; // zero-terminated array of valid prefixes
void free_prefixes(void); void free_prefixes(void);

View file

@ -8,11 +8,11 @@
static void test_add_prefix(CuTest *tc) { static void test_add_prefix(CuTest *tc) {
test_setup(); test_setup();
CuAssertPtrEquals(tc, 0, race_prefixes); CuAssertPtrEquals(tc, 0, race_prefixes);
add_raceprefix("sea"); CuAssertIntEquals(tc, 0, add_raceprefix("sea"));
CuAssertPtrNotNull(tc, race_prefixes); CuAssertPtrNotNull(tc, race_prefixes);
CuAssertStrEquals(tc, "sea", race_prefixes[0]); CuAssertStrEquals(tc, "sea", race_prefixes[0]);
CuAssertPtrEquals(tc, 0, race_prefixes[1]); CuAssertPtrEquals(tc, 0, race_prefixes[1]);
add_raceprefix("moon"); CuAssertIntEquals(tc, 0, add_raceprefix("moon"));
CuAssertStrEquals(tc, "sea", race_prefixes[0]); CuAssertStrEquals(tc, "sea", race_prefixes[0]);
CuAssertStrEquals(tc, "moon", race_prefixes[1]); CuAssertStrEquals(tc, "moon", race_prefixes[1]);
CuAssertPtrEquals(tc, 0, race_prefixes[2]); CuAssertPtrEquals(tc, 0, race_prefixes[2]);

View file

@ -69,7 +69,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
extern struct attrib_type at_unitdissolve; extern struct attrib_type at_unitdissolve;
/* In a->data.ca[1] steht der Prozentsatz mit dem sich die Einheit /* In a->data.ca[1] steht der Prozentsatz mit dem sich die Einheit
* auflöst, in a->data.ca[0] kann angegeben werden, wohin die Personen * aufl<EFBFBD>st, in a->data.ca[0] kann angegeben werden, wohin die Personen
* verschwinden. Passiert bereits in der ersten Runde! */ * verschwinden. Passiert bereits in der ersten Runde! */
static void dissolve_units(void) static void dissolve_units(void)
{ {
@ -442,12 +442,12 @@ static void melt_iceberg(region * r)
ADDMSG(&u->faction->msgs, msg_message("iceberg_melt", "region", r)); ADDMSG(&u->faction->msgs, msg_message("iceberg_melt", "region", r));
} }
/* driftrichtung löschen */ /* driftrichtung l<EFBFBD>schen */
a = a_find(r->attribs, &at_iceberg); a = a_find(r->attribs, &at_iceberg);
if (a) if (a)
a_remove(&r->attribs, a); a_remove(&r->attribs, a);
/* Gebäude löschen */ /* Geb<EFBFBD>ude l<>schen */
while (r->buildings) { while (r->buildings) {
remove_building(&r->buildings, r->buildings); remove_building(&r->buildings, r->buildings);
} }
@ -455,7 +455,7 @@ static void melt_iceberg(region * r)
/* in Ozean wandeln */ /* in Ozean wandeln */
terraform_region(r, newterrain(T_OCEAN)); terraform_region(r, newterrain(T_OCEAN));
/* Einheiten, die nicht schwimmen können oder in Schiffen sind, /* Einheiten, die nicht schwimmen k<EFBFBD>nnen oder in Schiffen sind,
* ertrinken */ * ertrinken */
drown(r); drown(r);
} }
@ -519,13 +519,13 @@ static void move_iceberg(region * r)
freset(sh, SF_SELECT); freset(sh, SF_SELECT);
for (sh = r->ships; sh; sh = sh->next) { for (sh = r->ships; sh; sh = sh->next) {
/* Meldung an Kapitän */ /* Meldung an Kapit<EFBFBD>n */
double dmg = config_get_flt("rules.ship.damage.intoiceberg", 0.1); double dmg = config_get_flt("rules.ship.damage.intoiceberg", 0.1);
damage_ship(sh, dmg); damage_ship(sh, dmg);
fset(sh, SF_SELECT); fset(sh, SF_SELECT);
} }
/* Personen, Schiffe und Gebäude verschieben */ /* Personen, Schiffe und Geb<EFBFBD>ude verschieben */
while (rc->buildings) { while (rc->buildings) {
rc->buildings->region = r; rc->buildings->region = r;
translist(&rc->buildings, &r->buildings, rc->buildings); translist(&rc->buildings, &r->buildings, rc->buildings);
@ -544,7 +544,7 @@ static void move_iceberg(region * r)
u_set_building(u, b); /* undo leave-prevention */ u_set_building(u, b); /* undo leave-prevention */
} }
/* Beschädigte Schiffe können sinken */ /* Besch<EFBFBD>digte Schiffe k<>nnen sinken */
for (sh = r->ships; sh;) { for (sh = r->ships; sh;) {
shn = sh->next; shn = sh->next;
@ -725,7 +725,7 @@ static void orc_growth(void)
} }
} }
/** Talente von Dämonen verschieben sich. /** Talente von D<EFBFBD>monen verschieben sich.
*/ */
static void demon_skillchanges(void) static void demon_skillchanges(void)
{ {
@ -760,7 +760,7 @@ static void icebergs(void)
} }
#define HERBS_ROT /* herbs owned by units have a chance to rot. */ #define HERBS_ROT /* herbs owned by units have a chance to rot. */
#define HERBROTCHANCE 5 /* Verrottchance für Kräuter (ifdef HERBS_ROT) */ #define HERBROTCHANCE 5 /* Verrottchance f<EFBFBD>r Kr<4B>uter (ifdef HERBS_ROT) */
#ifdef HERBS_ROT #ifdef HERBS_ROT
static void rotting_herbs(void) static void rotting_herbs(void)
{ {

View file

@ -915,14 +915,16 @@ static void describe(struct stream *out, const region * r, faction * f)
bool transparent = b->type->transparent(b, f); bool transparent = b->type->transparent(b, f);
const char *name = border_name(b, r, f, GF_DETAILED | GF_ARTICLE); const char *name = border_name(b, r, f, GF_DETAILED | GF_ARTICLE);
if (!transparent) if (!transparent) {
see[d] = false; see[d] = false;
}
if (!see_border(b, f, r)) { if (!see_border(b, f, r)) {
b = b->next; b = b->next;
continue; continue;
} }
while (e && (e->transparent != transparent || strcmp(name, e->name))) while (e && (e->transparent != transparent || strcmp(name, e->name)!=0)) {
e = e->next; e = e->next;
}
if (!e) { if (!e) {
e = calloc(sizeof(struct edge), 1); e = calloc(sizeof(struct edge), 1);
e->name = _strdup(name); e->name = _strdup(name);
@ -2248,10 +2250,8 @@ report_plaintext(const char *filename, report_context * ctx,
*bufp = 0; *bufp = 0;
centre(out, buf, true); centre(out, buf, true);
newline(out); newline(out);
if (description == NULL) { description = mkname("potion", pname);
const char *potiontext = mkname("potion", pname); description = LOC(f->locale, description);
description = LOC(f->locale, potiontext);
}
centre(out, description, true); centre(out, description, true);
} }
} }

View file

@ -720,7 +720,7 @@ size_t size)
} }
} }
if (!isbattle) { if (!isbattle) {
bool printed = 0; int printed = 0;
order *ord;; order *ord;;
for (ord = u->old_orders; ord; ord = ord->next) { for (ord = u->old_orders; ord; ord = ord->next) {
keyword_t kwd = getkeyword(ord); keyword_t kwd = getkeyword(ord);
@ -1587,7 +1587,7 @@ static void var_free_resources(variant x)
x.v = 0; x.v = 0;
} }
static void var_free_regions(variant x) static void var_free_regions(variant x) //-V524
{ {
free(x.v); free(x.v);
} }
@ -1716,18 +1716,6 @@ static void eval_curse(struct opstack **stack, const void *userdata)
opush(stack, var); opush(stack, var);
} }
static void eval_unitname(struct opstack **stack, const void *userdata)
{ /* unit -> string */
const struct faction *f = (const struct faction *)userdata;
const struct unit *u = (const struct unit *)opop(stack).v;
const char *c = u ? unit_getname(u) : LOC(f->locale, "an_unknown_unit");
size_t len = strlen(c);
variant var;
var.v = strcpy(balloc(len + 1), c);
opush(stack, var);
}
static void eval_unitid(struct opstack **stack, const void *userdata) static void eval_unitid(struct opstack **stack, const void *userdata)
{ /* unit -> int */ { /* unit -> int */
const struct faction *f = (const struct faction *)userdata; const struct faction *f = (const struct faction *)userdata;
@ -1836,16 +1824,16 @@ static void eval_weight(struct opstack **stack, const void *userdata)
sprintf(buffer, "1 %s", LOC(lang, "weight_unit")); sprintf(buffer, "1 %s", LOC(lang, "weight_unit"));
} }
else { else {
sprintf(buffer, "%u %s", weight / SCALEWEIGHT, LOC(lang, sprintf(buffer, "%d %s", weight / SCALEWEIGHT, LOC(lang,
"weight_unit_p")); "weight_unit_p"));
} }
} }
else { else {
if (weight == 1) { if (weight == 1) {
sprintf(buffer, "1 %s %u", LOC(lang, "weight_per"), SCALEWEIGHT); sprintf(buffer, "1 %s %d", LOC(lang, "weight_per"), SCALEWEIGHT);
} }
else { else {
sprintf(buffer, "%u %s %u", weight, LOC(lang, "weight_per_p"), sprintf(buffer, "%d %s %d", weight, LOC(lang, "weight_per_p"),
SCALEWEIGHT); SCALEWEIGHT);
} }
} }
@ -2136,7 +2124,6 @@ void register_reports(void)
add_function("ship", &eval_ship); add_function("ship", &eval_ship);
add_function("unit", &eval_unit); add_function("unit", &eval_unit);
add_function("unit.dative", &eval_unit_dative); add_function("unit.dative", &eval_unit_dative);
add_function("unit.name", &eval_unitname);
add_function("unit.id", &eval_unitid); add_function("unit.id", &eval_unitid);
add_function("unit.size", &eval_unitsize); add_function("unit.size", &eval_unitsize);
add_function("building", &eval_building); add_function("building", &eval_building);