From c9f886078b665dbcd05965adadc3383f6e6d2a01 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 4 Nov 2015 19:08:45 +0100 Subject: [PATCH 01/14] CID 22452 'Constant' variable guards dead code github issue #353 there is no a_fshidden attribute. --- scripts/tests/e2/e2features.lua | 11 +++++++++++ src/creport.c | 12 ++---------- src/reports.c | 14 +++----------- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/scripts/tests/e2/e2features.lua b/scripts/tests/e2/e2features.lua index f5e40d42a..8505bfd39 100644 --- a/scripts/tests/e2/e2features.lua +++ b/scripts/tests/e2/e2features.lua @@ -335,3 +335,14 @@ function test_stonegolems() assert_equal(1 ,u2.number, "There shoud be one Stone Golems") -- end test Stone Golems four stones end + + +function test_birthdaycake() + r = region.create(0,0, "plain") + f = faction.create("cake@eressea.de", "human", "de") + u = unit.create(f, r, 1) + u:add_item("birthdaycake", 1) + u:clear_orders() + u:add_order("ZEIGE Geburtstagstorte") + process_orders() +end diff --git a/src/creport.c b/src/creport.c index c077db5f0..8078c545c 100644 --- a/src/creport.c +++ b/src/creport.c @@ -746,7 +746,6 @@ void cr_output_unit(stream *out, const region * r, const faction * f, building *b; const char *pzTmp; skill *sv; - const attrib *a_fshidden = NULL; bool itemcloak = false; static const curse_type *itemcloak_ct = 0; static bool init = false; @@ -834,13 +833,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, stream_printf(out, "\"%s\";typprefix\n", translate(prefix, LOC(f->locale, prefix))); } - if (u->faction != f && a_fshidden - && a_fshidden->data.ca[0] == 1 && effskill(u, SK_STEALTH, 0) >= 6) { - stream_printf(out, "-1;Anzahl\n"); - } - else { - stream_printf(out, "%d;Anzahl\n", u->number); - } + stream_printf(out, "%d;Anzahl\n", u->number); pzTmp = get_racename(u->attribs); if (pzTmp) { @@ -981,8 +974,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (f == u->faction || omniscient(f)) { show = u->items; } - else if (!itemcloak && mode >= see_unit && !(a_fshidden - && a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH, 0) >= 3)) { + else if (!itemcloak && mode >= see_unit) { int n = report_items(u->items, result, MAX_INVENTORY, u, f); assert(n >= 0); if (n > 0) diff --git a/src/reports.c b/src/reports.c index 792f8c7fb..78518a9ce 100644 --- a/src/reports.c +++ b/src/reports.c @@ -456,7 +456,6 @@ size_t size) building *b; bool isbattle = (bool)(mode == see_battle); int telepath_see = 0; - attrib *a_fshidden = NULL; item *itm; item *show; faction *fv = visible_faction(f, u); @@ -520,14 +519,8 @@ size_t size) bufp = STRLCPY(bufp, ", ", size); - if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1 - && effskill(u, SK_STEALTH, 0) >= 6) { - bufp = STRLCPY(bufp, "? ", size); - } - else { - if (wrptr(&bufp, &size, _snprintf(bufp, size, "%d ", u->number))) - WARN_STATIC_BUFFER(); - } + if (wrptr(&bufp, &size, _snprintf(bufp, size, "%d ", u->number))) + WARN_STATIC_BUFFER(); pzTmp = get_racename(u->attribs); if (pzTmp) { @@ -607,8 +600,7 @@ size_t size) if (f == u->faction || telepath_see || omniscient(f)) { show = u->items; } - else if (!itemcloak && mode >= see_unit && !(a_fshidden - && a_fshidden->data.ca[1] == 1 && effskill(u, SK_STEALTH, 0) >= 3)) { + else if (!itemcloak && mode >= see_unit) { int n = report_items(u->items, results, MAX_INVENTORY, u, f); assert(n >= 0); if (n > 0) From 4d1f2327fe283a9ba69a44cf43fa0ecd97022a7a Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 4 Nov 2015 19:13:15 +0100 Subject: [PATCH 02/14] CID 22545: Leak of memory or pointers to system resources github issue #335 volcano destruction was not causing a resuction in wages etc., because we forgot to add the attribute to the region. --- src/randenc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/randenc.c b/src/randenc.c index fa6e3e951..1654f27ee 100644 --- a/src/randenc.c +++ b/src/randenc.c @@ -580,8 +580,8 @@ volcano_destruction(region * volcano, region * r, const char *damage) rsettrees(r, 0, 0); a = a_find(r->attribs, &at_reduceproduction); - if (!a) { - a = make_reduceproduction(percent, time); + if (a) { + a = a_add(&r->attribs, make_reduceproduction(percent, time)); } else { /* Produktion vierteln ... */ From b6190b5193b1eedce03013f998ca5ec5395de26f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 4 Nov 2015 19:20:34 +0100 Subject: [PATCH 03/14] CID 22527 Resource leak --- src/kernel/connection.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/kernel/connection.c b/src/kernel/connection.c index eecbd6c59..e08cbe806 100644 --- a/src/kernel/connection.c +++ b/src/kernel/connection.c @@ -151,14 +151,13 @@ connection *get_borders(const region * r1, const region * r2) connection *new_border(border_type * type, region * from, region * to) { - connection *b = calloc(1, sizeof(struct connection)); + connection *b, **bp = get_borders_i(from, to); - if (from && to) { - connection **bp = get_borders_i(from, to); - while (*bp) - bp = &(*bp)->next; - *bp = b; + assert(from && to); + while (*bp) { + bp = &(*bp)->next; } + *bp = b = calloc(1, sizeof(connection)); b->type = type; b->from = from; b->to = to; From b81e56d306da234bc5fcd8abed665ce07dd1069f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:32:21 +0100 Subject: [PATCH 04/14] CID 22486 Dereference after null check --- src/kernel/unit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/unit.c b/src/kernel/unit.c index 336231d7d..493a3a66f 100644 --- a/src/kernel/unit.c +++ b/src/kernel/unit.c @@ -1013,7 +1013,7 @@ void transfermen(unit * u, unit * dst, int n) sn->level = (unsigned char)level; sn->weeks = (unsigned char)weeks; assert(sn->weeks > 0 && sn->weeks <= sn->level * 2 + 1); - assert(dst->number != 0 || (sn->level == sv->level + assert(dst->number != 0 || (sv && sn->level == sv->level && sn->weeks == sv->weeks)); } else if (sn) { From c68829029b9e4cdbf9c4508b5086c05833585713 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:35:03 +0100 Subject: [PATCH 05/14] 22485 Dereference after null check --- src/move.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/move.c b/src/move.c index 13f0e3f0e..5b68c3d07 100644 --- a/src/move.c +++ b/src/move.c @@ -2192,8 +2192,8 @@ static void travel(unit * u, region_list ** routep) region_list *route_begin = NULL; follower *followers = NULL; - if (routep) - *routep = NULL; + assert(routep); + *routep = NULL; /* a few pre-checks that need not be done for each step: */ if (!fval(r->terrain, SEA_REGION)) { From f83e71e99a3c51d390c7034d6998c30afba3d801 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:36:32 +0100 Subject: [PATCH 06/14] 22483 Dereference after null check --- src/bind_building.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/bind_building.c b/src/bind_building.c index 714b915dd..b8fb07611 100644 --- a/src/bind_building.c +++ b/src/bind_building.c @@ -193,11 +193,9 @@ static int tolua_building_create(lua_State * L) const char *bname = tolua_tostring(L, 2, 0); if (!r) { log_error("building.create expects a region as argument 1"); - } - if (!bname) { + } else if (!bname) { log_error("building.create expects a name as argument 2"); - } - if (bname) { + } else { const building_type *btype = bt_find(bname); if (btype) { building *b = new_building(btype, r, default_locale); From ba69f721160fe4f0d3abb0aaa48e26f6ceeaf2ff Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:40:09 +0100 Subject: [PATCH 07/14] CID 22502 Argument cannot be negative these curses macros are hard to decipher, I think this may be a kind of false positive because stdscr is always not-null? --- src/gmtool.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gmtool.c b/src/gmtool.c index 5929eea0b..fb55eae9a 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -1245,7 +1245,7 @@ void run_mapper(void) curs_set(1); set_readline(curses_readline); - + assert(stdscr); getbegyx(stdscr, x, y); width = getmaxx(stdscr); height = getmaxy(stdscr); From 06513d0db425321b7143cb117b3e107bc2e301b7 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:46:19 +0100 Subject: [PATCH 08/14] coverity CID 22578 --- storage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage b/storage index 1d92cb36d..4c83fbcbd 160000 --- a/storage +++ b/storage @@ -1 +1 @@ -Subproject commit 1d92cb36df41c183c378aad17cbbfc0eddbb5c84 +Subproject commit 4c83fbcbd0f38f5c69d4894c825d7b0cf67e0531 From 744246ab343b0227c0fc7fa8fcdace24ae27fc7f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:48:03 +0100 Subject: [PATCH 09/14] CID 22498 Missing break in switch --- src/gmtool.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gmtool.c b/src/gmtool.c index fb55eae9a..2c15f6b29 100644 --- a/src/gmtool.c +++ b/src/gmtool.c @@ -983,6 +983,7 @@ static void handlekey(state * st, int c) statusline(st->wnd_status->handle, "info-buildings true"); else statusline(st->wnd_status->handle, "info-buildings false"); + break; case 'f': st->info_flags ^= IFL_FACTIONS; if (st->info_flags & IFL_FACTIONS) From 64e8d4b094bb85b494db366db6d30454987e7cd4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:50:01 +0100 Subject: [PATCH 10/14] CID 22484 Explicit null dereferenced --- src/move.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/move.c b/src/move.c index 5b68c3d07..3124ea54c 100644 --- a/src/move.c +++ b/src/move.c @@ -1321,7 +1321,7 @@ static const region_list *cap_route(region * r, const region_list * route, region *current = r; int moves = speed; const region_list *iroute = route; - while (iroute != route_end) { + while (iroute && iroute != route_end) { region *next = iroute->data; direction_t reldir = reldirection(current, next); From 52edf15cece7d4c297d75235d521f8fb883a96cc Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 08:57:56 +0100 Subject: [PATCH 11/14] CID 22438 Unchecked return value from library --- src/reports.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/reports.c b/src/reports.c index 78518a9ce..45ed7e75f 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1547,14 +1547,18 @@ int write_reports(faction * f, time_t ltime) struct report_context ctx; const char *encoding = "UTF-8"; report_type *rtype; + const char *path = reportpath();; if (noreports) { return false; } prepare_report(&ctx, f); get_addresses(&ctx); - if (_access(reportpath(), 0) < 0) { - _mkdir(reportpath()); + if (_access(path, 0) < 0) { + if (_mkdir(path) != 0) { + log_error("could not create reports directory %s: %s", path, strerror(errno)); + abort(); + } } if (errno) { log_warning("errno was %d before writing reports", errno); From 91758d2e510ee051eb4ebc2f0245aecc7bed6504 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 09:00:52 +0100 Subject: [PATCH 12/14] CID 22439 Unchecked return value from library --- src/reports.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/reports.c b/src/reports.c index 45ed7e75f..f30175bab 100644 --- a/src/reports.c +++ b/src/reports.c @@ -1657,12 +1657,18 @@ int reports(void) time_t ltime = time(NULL); int retval = 0; char path[MAX_PATH]; + const char * rpath = reportpath(); log_info("Writing reports for turn %d:", turn); report_donations(); remove_empty_units(); - _mkdir(reportpath()); + if (_access(rpath, 0) < 0) { + if (_mkdir(rpath) != 0) { + log_error("could not create reports directory %s: %s", rpath, strerror(errno)); + abort(); + } + } sprintf(path, "%s/reports.txt", reportpath()); mailit = fopen(path, "w"); if (mailit == NULL) { From a8daf061ded895a96804d8acf8f9d54d499be1f9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 09:14:02 +0100 Subject: [PATCH 13/14] missing include --- storage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage b/storage index 4c83fbcbd..effebc945 160000 --- a/storage +++ b/storage @@ -1 +1 @@ -Subproject commit 4c83fbcbd0f38f5c69d4894c825d7b0cf67e0531 +Subproject commit effebc94586cd4d18a0e878c7b845c5fab9e722f From 92d159610968377e73ec5c7c37d5ec6d8a574dc4 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 5 Nov 2015 09:18:57 +0100 Subject: [PATCH 14/14] CID 22444: unchecked return value from fseek --- storage | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage b/storage index effebc945..89f3c1b01 160000 --- a/storage +++ b/storage @@ -1 +1 @@ -Subproject commit effebc94586cd4d18a0e878c7b845c5fab9e722f +Subproject commit 89f3c1b01e41f2675fcbfd51fd8494894dc22d44