Merge pull request #277 from ennorehling/feature/bug-2128-travelthru-length

refactoring: wrptr takes an int, not a size_t
This commit is contained in:
Enno Rehling 2015-08-18 10:26:56 +02:00
commit a91e4c6e71
19 changed files with 293 additions and 469 deletions

View File

@ -10,6 +10,7 @@ $BUILD/iniparser/inifile eressea.ini add lua:paths lunit:scripts
fi fi
} }
set -e
[ -z $BUILD ] && BUILD=Debug ; export BUILD [ -z $BUILD ] && BUILD=Debug ; export BUILD
s/cmake-init s/cmake-init
s/build s/build

View File

@ -2966,21 +2966,16 @@ static void print_header(battle * b)
side *s; side *s;
char *bufp = zText; char *bufp = zText;
size_t size = sizeof(zText) - 1; size_t size = sizeof(zText) - 1;
size_t bytes;
for (s = b->sides; s != b->sides + b->nsides; ++s) { for (s = b->sides; s != b->sides + b->nsides; ++s) {
fighter *df; fighter *df;
for (df = s->fighters; df; df = df->next) { for (df = s->fighters; df; df = df->next) {
if (is_attacker(df)) { if (is_attacker(df)) {
if (first) { if (first) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (lastf) { if (lastf) {
bytes = strlcpy(bufp, (const char *)lastf, size); bufp = STRLCPY(bufp, lastf, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
first = true; first = true;
} }
if (seematrix(f, s)) if (seematrix(f, s))
@ -2992,20 +2987,12 @@ static void print_header(battle * b)
} }
} }
if (first) { if (first) {
bytes = strlcpy(bufp, " ", size); bufp = STRLCPY(bufp, " ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, LOC(f->locale, "and"), size);
WARN_STATIC_BUFFER(); bufp = STRLCPY(bufp, " ", size);
bytes = strlcpy(bufp, (const char *)LOC(f->locale, "and"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, " ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (lastf) { if (lastf) {
bytes = strlcpy(bufp, (const char *)lastf, size); bufp = STRLCPY(bufp, lastf, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
m = msg_message("battle::starters", "factions", zText); m = msg_message("battle::starters", "factions", zText);
@ -3736,7 +3723,6 @@ static int battle_report(battle * b)
faction *fac = bf->faction; faction *fac = bf->faction;
char buf[32 * MAXSIDES]; char buf[32 * MAXSIDES];
char *bufp = buf; char *bufp = buf;
size_t bytes;
size_t size = sizeof(buf) - 1; size_t size = sizeof(buf) - 1;
message *m; message *m;
@ -3759,34 +3745,24 @@ static int battle_report(battle * b)
char buffer[32]; char buffer[32];
if (komma) { if (komma) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
slprintf(buffer, sizeof(buffer), "%s %2d(%s): ", slprintf(buffer, sizeof(buffer), "%s %2d(%s): ",
loc_army, army_index(s), abbrev); loc_army, army_index(s), abbrev);
bytes = strlcpy(bufp, buffer, size); bufp = STRLCPY(bufp, buffer, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
for (r = FIGHT_ROW; r != NUMROWS; ++r) { for (r = FIGHT_ROW; r != NUMROWS; ++r) {
if (alive[r]) { if (alive[r]) {
if (l != FIGHT_ROW) { if (l != FIGHT_ROW) {
bytes = strlcpy(bufp, "+", size); bufp = STRLCPY(bufp, "+", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
while (k--) { while (k--) {
bytes = strlcpy(bufp, "0+", size); bufp = STRLCPY(bufp, "0+", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
sprintf(buffer, "%d", alive[r]); sprintf(buffer, "%d", alive[r]);
bytes = strlcpy(bufp, buffer, size); bufp = STRLCPY(bufp, buffer, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
k = 0; k = 0;
l = r + 1; l = r + 1;

View File

@ -142,10 +142,8 @@ static struct unit *create_recruiter(void) {
static void test_heroes_dont_recruit(CuTest * tc) { static void test_heroes_dont_recruit(CuTest * tc) {
unit *u; unit *u;
order *ord; order *ord;
const message_type *msg_types[1];
test_cleanup(); test_cleanup();
msg_types[0] = register_msg("error_herorecruit", 3, "unit:unit", "region:region", "command:order");
u = create_recruiter(); u = create_recruiter();
fset(u, UFL_HERO); fset(u, UFL_HERO);
@ -155,7 +153,7 @@ static void test_heroes_dont_recruit(CuTest * tc) {
economics(u->region); economics(u->region);
CuAssertIntEquals(tc, 1, u->number); CuAssertIntEquals(tc, 1, u->number);
assert_messages(tc, u->faction->msgs->begin, msg_types, 1, true, 0); CuAssertPtrNotNull(tc, test_find_messagetype(u->faction->msgs, "error_herorecruit"));
test_cleanup(); test_cleanup();
} }

View File

@ -1676,6 +1676,7 @@ void attrib_init(void)
void kernel_init(void) void kernel_init(void)
{ {
register_reports(); register_reports();
mt_clear();
if (!mt_find("missing_message")) { if (!mt_find("missing_message")) {
mt_register(mt_new_va("missing_message", "name:string", 0)); mt_register(mt_new_va("missing_message", "name:string", 0));
mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0)); mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0));

View File

@ -17,6 +17,7 @@ void test_message(CuTest *tc) {
message *msg; message *msg;
message_type *mtype = mt_new("custom", NULL); message_type *mtype = mt_new("custom", NULL);
test_cleanup();
mt_register(mtype); mt_register(mtype);
CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom")); CuAssertPtrEquals(tc, mtype, (void *)mt_find("custom"));
CuAssertIntEquals(tc, 0, mtype->nparameters); CuAssertIntEquals(tc, 0, mtype->nparameters);
@ -41,6 +42,7 @@ static void test_merge_split(CuTest *tc) {
struct mlist **split; struct mlist **split;
message_type *mtype = mt_new("custom", NULL); message_type *mtype = mt_new("custom", NULL);
test_cleanup();
mt_register(mtype); mt_register(mtype);
add_message(&mlist, msg_message(mtype->name, "")); add_message(&mlist, msg_message(mtype->name, ""));
add_message(&append, msg_message(mtype->name, "")); add_message(&append, msg_message(mtype->name, ""));
@ -55,6 +57,7 @@ static void test_merge_split(CuTest *tc) {
CuAssertPtrEquals(tc, append->begin, mlist->begin->next); CuAssertPtrEquals(tc, append->begin, mlist->begin->next);
split_messages(mlist, split); split_messages(mlist, split);
CuAssertPtrEquals(tc, 0, mlist->begin->next); CuAssertPtrEquals(tc, 0, mlist->begin->next);
test_cleanup();
} }
CuSuite *get_messages_suite(void) { CuSuite *get_messages_suite(void) {

View File

@ -521,12 +521,10 @@ static void test_shipspeed_damage(CuTest *tc) {
static void test_shipspeed(CuTest *tc) { static void test_shipspeed(CuTest *tc) {
ship *sh; ship *sh;
const ship_type *stype; const ship_type *stype;
region *r;
unit *cap, *crew; unit *cap, *crew;
test_cleanup(); test_cleanup();
sh = setup_ship(); sh = setup_ship();
r = sh->region;
stype = sh->type; stype = sh->type;
CuAssertIntEquals_Msg(tc, "ship without a captain cannot move", 0, shipspeed(sh, NULL)); CuAssertIntEquals_Msg(tc, "ship without a captain cannot move", 0, shipspeed(sh, NULL));

View File

@ -2320,7 +2320,8 @@ static bool display_race(faction * f, unit * u, const race * rc)
name = rc_name_s(rc, NAME_SINGULAR); name = rc_name_s(rc, NAME_SINGULAR);
bytes = slprintf(bufp, size, "%s: ", LOC(f->locale, name)); bytes = slprintf(bufp, size, "%s: ", LOC(f->locale, name));
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
key = mkname("raceinfo", rc->_name); key = mkname("raceinfo", rc->_name);
@ -2329,36 +2330,38 @@ static bool display_race(faction * f, unit * u, const race * rc)
info = LOC(f->locale, mkname("raceinfo", "no_info")); info = LOC(f->locale, mkname("raceinfo", "no_info"));
} }
bytes = strlcpy(bufp, info, size); bufp = STRLCPY(bufp, info, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
/* hp_p : Trefferpunkte */ /* hp_p : Trefferpunkte */
bytes = bytes =
slprintf(bufp, size, " %d %s", rc->hitpoints, LOC(f->locale, slprintf(bufp, size, " %d %s", rc->hitpoints, LOC(f->locale,
"stat_hitpoints")); "stat_hitpoints"));
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
/* b_attacke : Angriff */ /* b_attacke : Angriff */
bytes = bytes =
slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_attack"), slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_attack"),
(rc->at_default + rc->at_bonus)); (rc->at_default + rc->at_bonus));
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
/* b_defense : Verteidigung */ /* b_defense : Verteidigung */
bytes = bytes =
slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_defense"), slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_defense"),
(rc->df_default + rc->df_bonus)); (rc->df_default + rc->df_bonus));
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
/* b_armor : Rüstung */ /* b_armor : Rüstung */
if (rc->armor > 0) { if (rc->armor > 0) {
bytes = bytes =
slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_armor"), rc->armor); slprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_armor"), rc->armor);
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
@ -2377,40 +2380,31 @@ static bool display_race(faction * f, unit * u, const race * rc)
} }
} }
if (rc->battle_flags & BF_EQUIPMENT) { if (rc->battle_flags & BF_EQUIPMENT) {
bytes = (size_t)_snprintf(bufp, size, " %s", LOC(f->locale, "stat_equipment")); if (wrptr(&bufp, &size, _snprintf(bufp, size, " %s", LOC(f->locale, "stat_equipment"))) != 0)
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
if (rc->battle_flags & BF_RES_PIERCE) { if (rc->battle_flags & BF_RES_PIERCE) {
bytes = (size_t)_snprintf(bufp, size, " %s", LOC(f->locale, "stat_pierce")); if (wrptr(&bufp, &size, _snprintf(bufp, size, " %s", LOC(f->locale, "stat_pierce"))) != 0)
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
if (rc->battle_flags & BF_RES_CUT) { if (rc->battle_flags & BF_RES_CUT) {
bytes = (size_t)_snprintf(bufp, size, " %s", LOC(f->locale, "stat_cut")); if (wrptr(&bufp, &size, _snprintf(bufp, size, " %s", LOC(f->locale, "stat_cut"))) != 0)
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
if (rc->battle_flags & BF_RES_BASH) { if (rc->battle_flags & BF_RES_BASH) {
bytes = (size_t)_snprintf(bufp, size, " %s", LOC(f->locale, "stat_bash")); if (wrptr(&bufp, &size, _snprintf(bufp, size, " %s", LOC(f->locale, "stat_bash"))) != 0)
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
bytes = if (wrptr(&bufp, &size, _snprintf(bufp, size, " %d %s", at_count, LOC(f->locale, (at_count == 1) ? "stat_attack" : "stat_attacks"))) != 0)
(size_t)_snprintf(bufp, size, " %d %s", at_count, LOC(f->locale,
(at_count == 1) ? "stat_attack" : "stat_attacks"));
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
for (a = 0; a < RACE_ATTACKS; a++) { for (a = 0; a < RACE_ATTACKS; a++) {
if (rc->attack[a].type != AT_NONE) { if (rc->attack[a].type != AT_NONE) {
if (a != 0) if (a != 0)
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
else else
bytes = strlcpy(bufp, ": ", size); bufp = STRLCPY(bufp, ": ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
switch (rc->attack[a].type) { switch (rc->attack[a].type) {
case AT_STANDARD: case AT_STANDARD:
@ -2439,7 +2433,8 @@ static bool display_race(faction * f, unit * u, const race * rc)
bytes = 0; bytes = 0;
} }
if (bytes && wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (bytes && wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }

View File

@ -745,14 +745,10 @@ static void test_peasant_luck_effect(CuTest *tc) {
static void test_luck_message(CuTest *tc) { static void test_luck_message(CuTest *tc) {
region* r; region* r;
const message_type *msg_types[1];
test_cleanup(); test_cleanup();
r = test_create_region(0, 0, NULL); r = test_create_region(0, 0, NULL);
rsetpeasants(r, 1); rsetpeasants(r, 1);
msg_types[0] = register_msg("peasantluck_success", 1, "births:int");
demographics(); demographics();
CuAssertPtrEquals_Msg(tc, "unexpected message", (void *)NULL, r->msgs); CuAssertPtrEquals_Msg(tc, "unexpected message", (void *)NULL, r->msgs);
@ -764,7 +760,7 @@ static void test_luck_message(CuTest *tc) {
demographics(); demographics();
assert_messages(tc, r->msgs->begin, msg_types, 1, true, 0); CuAssertPtrNotNull(tc, test_find_messagetype(r->msgs, "peasantluck_success"));
test_cleanup(); test_cleanup();
} }

View File

@ -77,6 +77,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h>
int *storms; int *storms;
@ -1131,7 +1132,6 @@ static const char *shortdirections[MAXDIRECTIONS] = {
static void cycle_route(order * ord, unit * u, int gereist) static void cycle_route(order * ord, unit * u, int gereist)
{ {
size_t bytes;
int cm = 0; int cm = 0;
char tail[1024], *bufp = tail; char tail[1024], *bufp = tail;
char neworder[2048]; char neworder[2048];
@ -1172,25 +1172,17 @@ static void cycle_route(order * ord, unit * u, int gereist)
if (!pause) { if (!pause) {
const char *loc = LOC(lang, shortdirections[d]); const char *loc = LOC(lang, shortdirections[d]);
if (bufp != tail) { if (bufp != tail) {
bytes = strlcpy(bufp, " ", size); bufp = STRLCPY_EX(bufp, " ", &size, "cycle_route");
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
bytes = strlcpy(bufp, loc, size); bufp = STRLCPY_EX(bufp, loc, &size, "cycle_route");
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
else if (strlen(neworder) > sizeof(neworder) / 2) else if (strlen(neworder) > sizeof(neworder) / 2)
break; break;
else if (cm == gereist && !paused && pause) { else if (cm == gereist && !paused && pause) {
const char *loc = LOC(lang, parameters[P_PAUSE]); const char *loc = LOC(lang, parameters[P_PAUSE]);
bytes = strlcpy(bufp, " ", size); bufp = STRLCPY_EX(bufp, " ", &size, "cycle_route");
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY_EX(bufp, loc, &size, "cycle_route");
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, loc, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
paused = true; paused = true;
} }
else if (pause) { else if (pause) {
@ -2552,7 +2544,8 @@ static int hunt(unit * u, order * ord)
bufp = command; bufp = command;
bytes = slprintf(bufp, size, "%s %s", LOC(u->faction->locale, keyword(K_MOVE)), LOC(u->faction->locale, directions[dir])); bytes = slprintf(bufp, size, "%s %s", LOC(u->faction->locale, keyword(K_MOVE)), LOC(u->faction->locale, directions[dir]));
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
moves = 1; moves = 1;
@ -2568,12 +2561,9 @@ static int hunt(unit * u, order * ord)
} }
rc = rconnect(rc, dir); rc = rconnect(rc, dir);
while (moves < speed && (dir = hunted_dir(rc->attribs, id)) != NODIRECTION) { while (moves < speed && (dir = hunted_dir(rc->attribs, id)) != NODIRECTION) {
bytes = strlcpy(bufp, " ", size); const char *loc = LOC(u->faction->locale, directions[dir]);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY_EX(bufp, " ", &size, "hunt");
WARN_STATIC_BUFFER(); bufp = STRLCPY_EX(bufp, loc, &size, "hunt");
bytes = strlcpy(bufp, LOC(u->faction->locale, directions[dir]), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
moves++; moves++;
rc = rconnect(rc, dir); rc = rconnect(rc, dir);
} }

View File

@ -100,6 +100,17 @@ extern int *storms;
extern int weeks_per_month; extern int weeks_per_month;
extern int months_per_year; extern int months_per_year;
static void check_errno(const char * file, int line) {
if (errno) {
char zText[64];
sprintf(zText, "error %d during report at %s:%d", errno, file, line);
perror(zText);
errno = 0;
}
}
#define CHECK_ERRNO() check_errno(__FILE__, __LINE__)
static char *gamedate_season(const struct locale *lang) static char *gamedate_season(const struct locale *lang)
{ {
static char buf[256]; // FIXME: static return value static char buf[256]; // FIXME: static return value
@ -1396,14 +1407,17 @@ static void durchreisende(stream *out, const region * r, const faction * f)
} }
} }
if (size > 0) { if (size > 0) {
CHECK_ERRNO();
if (maxtravel == 1) { if (maxtravel == 1) {
bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_one")); bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_one"));
} }
else { else {
bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_many")); bytes = _snprintf(bufp, size, " %s", LOC(f->locale, "has_moved_many"));
} }
CHECK_ERRNO();
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER_EX("durchreisende");
CHECK_ERRNO();
} }
*bufp = 0; *bufp = 0;
paragraph(out, buf, 0, 0, 0); paragraph(out, buf, 0, 0, 0);
@ -2206,6 +2220,7 @@ const char *charset)
} }
ch = 0; ch = 0;
CHECK_ERRNO();
for (a = a_find(f->attribs, &at_showitem); a && a->type == &at_showitem; for (a = a_find(f->attribs, &at_showitem); a && a->type == &at_showitem;
a = a->next) { a = a->next) {
const potion_type *ptype = const potion_type *ptype =
@ -2258,6 +2273,7 @@ const char *charset)
} }
} }
newline(out); newline(out);
CHECK_ERRNO();
centre(out, LOC(f->locale, "nr_alliances"), false); centre(out, LOC(f->locale, "nr_alliances"), false);
newline(out); newline(out);
@ -2265,6 +2281,7 @@ const char *charset)
rpline(out); rpline(out);
CHECK_ERRNO();
anyunits = 0; anyunits = 0;
for (r = ctx->first; sr == NULL && r != ctx->last; r = r->next) { for (r = ctx->first; sr == NULL && r != ctx->last; r = r->next) {
@ -2389,6 +2406,7 @@ const char *charset)
newline(out); newline(out);
rpline(out); rpline(out);
CHECK_ERRNO();
} }
if (!is_monsters(f)) { if (!is_monsters(f)) {
if (!anyunits) { if (!anyunits) {
@ -2400,6 +2418,7 @@ const char *charset)
} }
} }
fstream_done(&strm); fstream_done(&strm);
CHECK_ERRNO();
return 0; return 0;
} }

View File

@ -225,26 +225,19 @@ const char **name, const char **basename, int *number, bool singular)
} }
#define ORDERS_IN_NR 1 #define ORDERS_IN_NR 1
static size_t buforder(char *bufp, size_t size, const order * ord, int mode) static size_t buforder(char *buffer, size_t size, const order * ord, int mode)
{ {
size_t tsize = 0; char *bufp = buffer;
size_t bytes;
bytes = strlcpy(bufp, ", \"", size); bufp = STRLCPY(bufp, ", \"", size);
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (mode < ORDERS_IN_NR) { if (mode < ORDERS_IN_NR) {
char cmd[ORDERSIZE]; char cmd[ORDERSIZE];
get_command(ord, cmd, sizeof(cmd)); get_command(ord, cmd, sizeof(cmd));
bytes = strlcpy(bufp, cmd, size); bufp = STRLCPY(bufp, cmd, size);
} }
else { else {
bytes = strlcpy(bufp, "...", size); bufp = STRLCPY(bufp, "...", size);
} }
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (size > 1) { if (size > 1) {
*bufp++ = '\"'; *bufp++ = '\"';
@ -253,9 +246,8 @@ static size_t buforder(char *bufp, size_t size, const order * ord, int mode)
else { else {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
++tsize;
return tsize; return bufp-buffer;
} }
/** create a report of a list of items to a non-owner. /** create a report of a list of items to a non-owner.
@ -465,7 +457,6 @@ size_t size)
bool itemcloak = false; bool itemcloak = false;
const curse_type *itemcloak_ct = 0; const curse_type *itemcloak_ct = 0;
int result = 0; int result = 0;
size_t bytes;
item results[MAX_INVENTORY]; item results[MAX_INVENTORY];
itemcloak_ct = ct_find("itemcloak"); itemcloak_ct = ct_find("itemcloak");
@ -473,9 +464,7 @@ size_t size)
itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct)); itemcloak = curse_active(get_curse(u->attribs, itemcloak_ct));
} }
bytes = strlcpy(bufp, unitname(u), size); bufp = STRLCPY(bufp, unitname(u), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (!isbattle) { if (!isbattle) {
attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction); attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction);
@ -484,92 +473,61 @@ size_t size)
attrib *a = a_find(u->attribs, &at_group); attrib *a = a_find(u->attribs, &at_group);
if (a) { if (a) {
group *g = (group *)a->data.v; group *g = (group *)a->data.v;
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, groupid(g, f), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, groupid(g, f), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
if (getarnt) { if (getarnt) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, LOC(f->locale, "anonymous"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
else if (a_otherfaction) { else if (a_otherfaction) {
faction *otherfaction = get_otherfaction(a_otherfaction); faction *otherfaction = get_otherfaction(a_otherfaction);
if (otherfaction) { if (otherfaction) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, factionname(otherfaction), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, factionname(otherfaction), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
} }
else { else {
if (getarnt) { if (getarnt) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, LOC(f->locale, "anonymous"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
else { else {
if (a_otherfaction && alliedunit(u, f, HELP_FSTEALTH)) { if (a_otherfaction && alliedunit(u, f, HELP_FSTEALTH)) {
faction *f = get_otherfaction(a_otherfaction); faction *f = get_otherfaction(a_otherfaction);
int result = int result =
(size_t)_snprintf(bufp, size, ", %s (%s)", factionname(f), _snprintf(bufp, size, ", %s (%s)", factionname(f),
factionname(u->faction)); factionname(u->faction));
bytes = (size_t)result; if (wrptr(&bufp, &size, result) != 0)
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
else { else {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, factionname(fv), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, factionname(fv), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
} }
} }
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1 if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1
&& effskill(u, SK_STEALTH) >= 6) { && effskill(u, SK_STEALTH) >= 6) {
bytes = strlcpy(bufp, "? ", size); bufp = STRLCPY(bufp, "? ", size);
} }
else { else {
result = _snprintf(bufp, size, "%d ", u->number); if (wrptr(&bufp, &size, _snprintf(bufp, size, "%d ", u->number)))
bytes = (size_t)result; WARN_STATIC_BUFFER();
} }
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
pzTmp = get_racename(u->attribs); pzTmp = get_racename(u->attribs);
if (pzTmp) { if (pzTmp) {
bytes = strlcpy(bufp, pzTmp, size); bufp = STRLCPY(bufp, pzTmp, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) {
bytes = strlcpy(bufp, " (", size); bufp = STRLCPY(bufp, " (", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, racename(f->locale, u, u_race(u)), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, racename(f->locale, u, u_race(u)), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (size > 1) { if (size > 1) {
strcpy(bufp++, ")"); strcpy(bufp++, ")");
--size; --size;
@ -578,16 +536,10 @@ size_t size)
} }
else { else {
const race *irace = u_irace(u); const race *irace = u_irace(u);
bytes = strlcpy(bufp, racename(f->locale, u, irace), size); bufp = STRLCPY(bufp, racename(f->locale, u, irace), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (u->faction == f && irace != u_race(u)) { if (u->faction == f && irace != u_race(u)) {
bytes = strlcpy(bufp, " (", size); bufp = STRLCPY(bufp, " (", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, racename(f->locale, u, u_race(u)), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, racename(f->locale, u, u_race(u)), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (size > 1) { if (size > 1) {
strcpy(bufp++, ")"); strcpy(bufp++, ")");
--size; --size;
@ -596,42 +548,26 @@ size_t size)
} }
if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) { if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, LOC(f->locale, "hero"), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, LOC(f->locale, "hero"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
/* status */ /* status */
if (u->number && (u->faction == f || telepath_see || isbattle)) { if (u->number && (u->faction == f || telepath_see || isbattle)) {
const char *c = hp_status(u); const char *c = hp_status(u);
c = c ? LOC(f->locale, c) : 0; c = c ? LOC(f->locale, c) : 0;
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, report_kampfstatus(u, f->locale), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, report_kampfstatus(u, f->locale), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (c || fval(u, UFL_HUNGER)) { if (c || fval(u, UFL_HUNGER)) {
bytes = strlcpy(bufp, " (", size); bufp = STRLCPY(bufp, " (", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (c) { if (c) {
bytes = strlcpy(bufp, c, size); bufp = STRLCPY(bufp, c, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (fval(u, UFL_HUNGER)) { if (fval(u, UFL_HUNGER)) {
if (c) { if (c) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
bytes = strlcpy(bufp, LOC(f->locale, "unit_hungers"), size); bufp = STRLCPY(bufp, LOC(f->locale, "unit_hungers"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (size > 1) { if (size > 1) {
strcpy(bufp++, ")"); strcpy(bufp++, ")");
@ -640,29 +576,22 @@ size_t size)
} }
} }
if (is_guard(u, GUARD_ALL) != 0) { if (is_guard(u, GUARD_ALL) != 0) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, LOC(f->locale, "unit_guards"), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, LOC(f->locale, "unit_guards"), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if ((b = usiege(u)) != NULL) { if ((b = usiege(u)) != NULL) {
bytes = strlcpy(bufp, ", belagert ", size); bufp = STRLCPY(bufp, ", belagert ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, buildingname(b), size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, buildingname(b), size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
dh = 0; dh = 0;
if (u->faction == f || telepath_see) { if (u->faction == f || telepath_see) {
skill *sv; skill *sv;
for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) { for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) {
bytes = spskill(bufp, size, f->locale, u, sv, &dh, 1); size_t bytes = spskill(bufp, size, f->locale, u, sv, &dh, 1);
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <=INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }
@ -689,26 +618,21 @@ size_t size)
report_item(u, itm, f, &ic, NULL, &in, false); report_item(u, itm, f, &ic, NULL, &in, false);
if (in == 0 || ic == NULL) if (in == 0 || ic == NULL)
continue; continue;
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (!dh) { if (!dh) {
result = _snprintf(bufp, size, "%s: ", LOC(f->locale, "nr_inventory")); result = _snprintf(bufp, size, "%s: ", LOC(f->locale, "nr_inventory"));
bytes = (size_t)result; if (wrptr(&bufp, &size, result) != 0)
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
dh = 1; dh = 1;
} }
if (in == 1) { if (in == 1) {
bytes = strlcpy(bufp, ic, size); bufp = STRLCPY(bufp, ic, size);
} }
else { else {
result = _snprintf(bufp, size, "%d %s", in, ic); if (wrptr(&bufp, &size, _snprintf(bufp, size, "%d %s", in, ic)))
bytes = (size_t)result; WARN_STATIC_BUFFER();
} }
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
if (u->faction == f || telepath_see) { if (u->faction == f || telepath_see) {
@ -718,8 +642,7 @@ size_t size)
quicklist *ql = book->spells; quicklist *ql = book->spells;
int qi, header, maxlevel = effskill(u, SK_MAGIC); int qi, header, maxlevel = effskill(u, SK_MAGIC);
int result = _snprintf(bufp, size, ". Aura %d/%d", get_spellpoints(u), max_spellpoints(u->region, u)); int result = _snprintf(bufp, size, ". Aura %d/%d", get_spellpoints(u), max_spellpoints(u->region, u));
size_t bytes = (size_t)result; if (wrptr(&bufp, &size, result) != 0) {
if (result < 0 || wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
@ -729,19 +652,15 @@ size_t size)
int result = 0; int result = 0;
if (!header) { if (!header) {
result = _snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells")); result = _snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells"));
bytes = (size_t)result;
header = 1; header = 1;
} }
else { else {
bytes = strlcpy(bufp, ", ", size); result = (int)strlcpy(bufp, ", ", size);
} }
if (result < 0 || wrptr(&bufp, &size, bytes) != 0) { if (wrptr(&bufp, &size, result) != 0) {
WARN_STATIC_BUFFER();
}
bytes = strlcpy(bufp, spell_name(sbe->sp, f->locale), size);
if (wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
bufp = STRLCPY(bufp, spell_name(sbe->sp, f->locale), size);
} }
} }
@ -752,8 +671,7 @@ size_t size)
if (i != MAXCOMBATSPELLS) { if (i != MAXCOMBATSPELLS) {
int result = int result =
_snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_combatspells")); _snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_combatspells"));
size_t bytes = (size_t)result; if (wrptr(&bufp, &size, result) != 0)
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
dh = 0; dh = 0;
@ -763,30 +681,20 @@ size_t size)
dh = 1; dh = 1;
} }
else { else {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
} }
sp = get_combatspell(u, i); sp = get_combatspell(u, i);
if (sp) { if (sp) {
int sl = get_combatspelllevel(u, i); int sl = get_combatspelllevel(u, i);
bytes = strlcpy(bufp, spell_name(sp, u->faction->locale), size); bufp = STRLCPY(bufp, spell_name(sp, u->faction->locale), size);
if (bytes && wrptr(&bufp, &size, bytes) != 0) {
WARN_STATIC_BUFFER();
}
if (sl > 0) { if (sl > 0) {
result = _snprintf(bufp, size, " (%d)", sl); result = _snprintf(bufp, size, " (%d)", sl);
bytes = (size_t)result; if (wrptr(&bufp, &size, result) != 0)
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }
else { else {
bytes = strlcpy(bufp, LOC(f->locale, "nr_nospells"), size); bufp = STRLCPY(bufp, LOC(f->locale, "nr_nospells"), size);
if (bytes && wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
} }
@ -798,8 +706,8 @@ size_t size)
keyword_t kwd = getkeyword(ord); keyword_t kwd = getkeyword(ord);
if (is_repeated(kwd)) { if (is_repeated(kwd)) {
if (printed < ORDERS_IN_NR) { if (printed < ORDERS_IN_NR) {
bytes = buforder(bufp, size, ord, printed++); int result = (int)buforder(bufp, size, ord, printed++);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, result) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
else else
@ -811,8 +719,8 @@ size_t size)
keyword_t kwd = getkeyword(ord); keyword_t kwd = getkeyword(ord);
if (is_repeated(kwd)) { if (is_repeated(kwd)) {
if (printed < ORDERS_IN_NR) { if (printed < ORDERS_IN_NR) {
bytes = buforder(bufp, size, ord, printed++); int result = (int)buforder(bufp, size, ord, printed++);
if (wrptr(&bufp, &size, bytes) != 0) if (wrptr(&bufp, &size, result) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
else else
@ -825,14 +733,8 @@ size_t size)
str = u_description(u, f->locale); str = u_description(u, f->locale);
if (str) { if (str) {
bytes = strlcpy(bufp, "; ", size); bufp = STRLCPY(bufp, "; ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, str, size);
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, str, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
i = str[strlen(str) - 1]; i = str[strlen(str) - 1];
} }
if (i != '!' && i != '?' && i != '.') { if (i != '!' && i != '?' && i != '.') {
@ -843,15 +745,9 @@ size_t size)
} }
pzTmp = uprivate(u); pzTmp = uprivate(u);
if (u->faction == f && pzTmp) { if (u->faction == f && pzTmp) {
bytes = strlcpy(bufp, " (Bem: ", size); bufp = STRLCPY(bufp, " (Bem: ", size);
if (wrptr(&bufp, &size, bytes) != 0) bufp = STRLCPY(bufp, pzTmp, size);
WARN_STATIC_BUFFER(); bufp = STRLCPY(bufp, ")", size);
bytes = strlcpy(bufp, pzTmp, size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, ")", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
dh = 0; dh = 0;
@ -877,8 +773,6 @@ const struct unit * u, struct skill * sv, int *dh, int days)
{ {
char *bufp = buffer; char *bufp = buffer;
int i, effsk; int i, effsk;
size_t bytes;
size_t tsize = 0;
if (!u->number) if (!u->number)
return 0; return 0;
@ -888,65 +782,35 @@ const struct unit * u, struct skill * sv, int *dh, int days)
} }
} }
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (!*dh) { if (!*dh) {
bytes = strlcpy(bufp, LOC(lang, "nr_skills"), size); bufp = STRLCPY(bufp, LOC(lang, "nr_skills"), size);
tsize += bytes; bufp = STRLCPY(bufp, ": ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, ": ", size);
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
*dh = 1; *dh = 1;
} }
bytes = strlcpy(bufp, skillname(sv->id, lang), size); bufp = STRLCPY(bufp, skillname(sv->id, lang), size);
tsize += bytes; bufp = STRLCPY(bufp, " ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, " ", size);
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (sv->id == SK_MAGIC) { if (sv->id == SK_MAGIC) {
sc_mage *mage = get_mage(u); sc_mage *mage = get_mage(u);
if (mage && mage->magietyp != M_GRAY) { if (mage && mage->magietyp != M_GRAY) {
bytes = bufp = STRLCPY(bufp, LOC(lang, mkname("school",
strlcpy(bufp, LOC(lang, mkname("school",
magic_school[mage->magietyp])), size); magic_school[mage->magietyp])), size);
tsize += bytes; bufp = STRLCPY(bufp, " ", size);
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
bytes = strlcpy(bufp, " ", size);
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
if (sv->id == SK_STEALTH && fval(u, UFL_STEALTH)) { if (sv->id == SK_STEALTH && fval(u, UFL_STEALTH)) {
i = u_geteffstealth(u); i = u_geteffstealth(u);
if (i >= 0) { if (i >= 0) {
bytes = slprintf(bufp, size, "%d/", i); if (wrptr(&bufp, &size, _snprintf(bufp, size, "%d/", i)) != 0)
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }
effsk = effskill(u, sv->id); effsk = effskill(u, sv->id);
bytes = slprintf(bufp, size, "%d", effsk); if (wrptr(&bufp, &size, _snprintf(bufp, size, "%d", effsk)) != 0)
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
if (u->faction->options & want(O_SHOWSKCHANGE)) { if (u->faction->options & want(O_SHOWSKCHANGE)) {
@ -961,13 +825,11 @@ const struct unit * u, struct skill * sv, int *dh, int days)
diff = effsk - oldeff; diff = effsk - oldeff;
if (diff != 0) { if (diff != 0) {
bytes = slprintf(bufp, size, " (%s%d)", (diff > 0) ? "+" : "", diff); if (wrptr(&bufp, &size, _snprintf(bufp, size, " (%s%d)", (diff > 0) ? "+" : "", diff)) != 0)
tsize += bytes;
if (wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }
return tsize; return bufp-buffer;
} }
void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned int width, char mark) void split_paragraph(strlist ** SP, const char *s, unsigned int indent, unsigned int width, char mark)
@ -1783,6 +1645,7 @@ int write_reports(faction * f, time_t ltime)
bool gotit = false; bool gotit = false;
struct report_context ctx; struct report_context ctx;
const char *encoding = "UTF-8"; const char *encoding = "UTF-8";
report_type *rtype;
if (noreports) { if (noreports) {
return false; return false;
@ -1798,36 +1661,42 @@ int write_reports(faction * f, time_t ltime)
get_seen_interval(&ctx); get_seen_interval(&ctx);
} }
get_addresses(&ctx); get_addresses(&ctx);
_mkdir(reportpath()); if (_access(reportpath(), 0) < 0) {
do { _mkdir(reportpath());
report_type *rtype = report_types; }
if (errno) {
log_warning("errno was %d before writing reports", errno);
errno = 0; errno = 0;
if (verbosity >= 2) { }
log_printf(stdout, "Reports for %s:", factionname(f)); if (verbosity >= 2) {
} log_printf(stdout, "Reports for %s:", factionname(f));
for (; rtype != NULL; rtype = rtype->next) { }
if (f->options & rtype->flag) { for (rtype = report_types; rtype != NULL; rtype = rtype->next) {
if (f->options & rtype->flag) {
int error;
do {
char filename[MAX_PATH]; char filename[MAX_PATH];
sprintf(filename, "%s/%d-%s.%s", reportpath(), turn, factionid(f), sprintf(filename, "%s/%d-%s.%s", reportpath(), turn, factionid(f),
rtype->extension); rtype->extension);
error = 0;
if (rtype->write(filename, &ctx, encoding) == 0) { if (rtype->write(filename, &ctx, encoding) == 0) {
gotit = true; gotit = true;
} }
} if (errno) {
char zText[64];
log_warning("retrying, error %d during %s report for faction %s", error, rtype->extension, factionname(f));
sprintf(zText, "waiting %u seconds before we retry", backup / 1000);
perror(zText);
_sleep(backup);
if (backup < maxbackup) {
backup *= 2;
}
error = errno;
errno = 0;
}
} while (error);
} }
}
if (errno) {
char zText[64];
log_warning("retrying, error %d during reports for faction %s", errno, factionname(f));
sprintf(zText, "waiting %u seconds before we retry", backup / 1000);
perror(zText);
_sleep(backup);
if (backup < maxbackup) {
backup *= 2;
}
}
} while (errno);
if (!gotit) { if (!gotit) {
log_warning("No report for faction %s!", factionid(f)); log_warning("No report for faction %s!", factionid(f));
} }
@ -2320,8 +2189,7 @@ static void eval_resources(struct opstack **stack, const void *userdata)
const char *rname = const char *rname =
resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0); resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0);
int result = _snprintf(bufp, size, "%d %s", res->number, LOC(lang, rname)); int result = _snprintf(bufp, size, "%d %s", res->number, LOC(lang, rname));
size_t bytes = (size_t)result; if (wrptr(&bufp, &size, result) != 0 || size < sizeof(buf) / 2) {
if (result < 0 || wrptr(&bufp, &size, bytes) != 0 || size < sizeof(buf) / 2) {
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
break; break;
} }
@ -2360,9 +2228,7 @@ static void eval_regions(struct opstack **stack, const void *userdata)
} }
for (i = begin; i < end; ++i) { for (i = begin; i < end; ++i) {
const char *rname = (const char *)regionname(regions->regions[i], report); const char *rname = (const char *)regionname(regions->regions[i], report);
size_t bytes = strlcpy(bufp, rname, size); bufp = STRLCPY(bufp, rname, size);
if (bytes && wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
if (i + 1 < end && size > 2) { if (i + 1 < end && size > 2) {
strcat(bufp, ", "); strcat(bufp, ", ");
@ -2396,22 +2262,16 @@ static void eval_trail(struct opstack **stack, const void *userdata)
region *r = regions->regions[i]; region *r = regions->regions[i];
const char *trail = trailinto(r, lang); const char *trail = trailinto(r, lang);
const char *rn = f_regionid_s(r, report); const char *rn = f_regionid_s(r, report);
int result = _snprintf(bufp, size, trail, rn);
size_t bytes = (size_t)result; if (wrptr(&bufp, &size, _snprintf(bufp, size, trail, rn)) != 0)
if (result < 0 || wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
if (i + 2 < end) { if (i + 2 < end) {
bytes = strlcpy(bufp, ", ", size); bufp = STRLCPY(bufp, ", ", size);
} }
else if (i + 1 < end) { else if (i + 1 < end) {
bytes = strlcpy(bufp, LOC(lang, "list_and"), size); bufp = STRLCPY(bufp, LOC(lang, "list_and"), size);
} }
else
bytes = 0;
if (bytes && wrptr(&bufp, &size, bytes) != 0)
WARN_STATIC_BUFFER();
} }
} }
*bufp = 0; *bufp = 0;

View File

@ -604,13 +604,15 @@ static int sp_summon_familiar(castorder * co)
else { else {
bytes = strlcpy(bufp, (const char *)", ", size); bytes = strlcpy(bufp, (const char *)", ", size);
} }
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
bytes = bytes =
strlcpy(bufp, (const char *)skillname((skill_t)sk, mage->faction->locale), strlcpy(bufp, (const char *)skillname((skill_t)sk, mage->faction->locale),
size); size);
if (wrptr(&bufp, &size, bytes) != 0) assert(bytes <= INT_MAX);
if (wrptr(&bufp, &size, (int)bytes) != 0)
WARN_STATIC_BUFFER(); WARN_STATIC_BUFFER();
} }
} }

View File

@ -24,20 +24,10 @@
#include <CuTest.h> #include <CuTest.h>
typedef enum {
M_BASE,
M_MAGE,
M_SKILLS,
M_FACTION,
M_ITEMS,
NUM_TYPES
} m_type;
typedef struct { typedef struct {
region *r; region *r;
unit *spy; unit *spy;
unit *victim; unit *victim;
const message_type *msg_types[NUM_TYPES];
} spy_fixture; } spy_fixture;
static void setup_spy(spy_fixture *fix) { static void setup_spy(spy_fixture *fix) {
@ -45,12 +35,6 @@ static void setup_spy(spy_fixture *fix) {
fix->r = test_create_region(0, 0, NULL); fix->r = test_create_region(0, 0, NULL);
fix->spy = test_create_unit(test_create_faction(NULL), fix->r); fix->spy = test_create_unit(test_create_faction(NULL), fix->r);
fix->victim = test_create_unit(test_create_faction(NULL), fix->r); fix->victim = test_create_unit(test_create_faction(NULL), fix->r);
fix->msg_types[M_BASE] = register_msg("spyreport", 3, "spy:unit", "target:unit", "status:string");
fix->msg_types[M_MAGE] = register_msg("spyreport_mage", 3, "spy:unit", "target:unit", "type:string");
fix->msg_types[M_SKILLS] = register_msg("spyreport_skills", 3, "spy:unit", "target:unit", "skills:string");
fix->msg_types[M_FACTION] = register_msg("spyreport_faction", 3, "spy:unit", "target:unit", "faction:faction");
fix->msg_types[M_ITEMS] = register_msg("spyreport_items", 3, "spy:unit", "target:unit", "items:items");
} }
static void test_simple_spy_message(CuTest *tc) { static void test_simple_spy_message(CuTest *tc) {
@ -60,8 +44,7 @@ static void test_simple_spy_message(CuTest *tc) {
spy_message(0, fix.spy, fix.victim); spy_message(0, fix.spy, fix.victim);
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 1, true, M_BASE); CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
test_cleanup(); test_cleanup();
} }
@ -92,12 +75,11 @@ static void test_all_spy_message(CuTest *tc) {
spy_message(99, fix.spy, fix.victim); spy_message(99, fix.spy, fix.victim);
assert_messages(tc, fix.spy->faction->msgs->begin, fix.msg_types, 5, true, CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport"));
M_BASE, CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_mage"));
M_MAGE, CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_skills"));
M_FACTION, CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_faction"));
M_SKILLS, CuAssertPtrNotNull(tc, test_find_messagetype(fix.spy->faction->msgs, "spyreport_items"));
M_ITEMS);
test_cleanup(); test_cleanup();
} }

View File

@ -80,6 +80,11 @@ void test_cleanup(void)
free_races(); free_races();
free_spellbooks(); free_spellbooks();
free_gamedata(); free_gamedata();
mt_clear();
if (!mt_find("missing_message")) {
mt_register(mt_new_va("missing_message", "name:string", 0));
mt_register(mt_new_va("missing_feedback", "unit:unit", "region:region", "command:order", "name:string", 0));
}
} }
terrain_type * terrain_type *
@ -255,59 +260,6 @@ struct message * test_find_messagetype(struct message_list *msgs, const char *na
return 0; return 0;
} }
const message_type *register_msg(const char *type, int n_param, ...) {
char **argv;
va_list args;
int i;
va_start(args, n_param);
argv = malloc(sizeof(char *) * (n_param + 1));
for (i = 0; i < n_param; ++i) {
argv[i] = va_arg(args, char *);
}
argv[n_param] = 0;
va_end(args);
return mt_register(mt_new(type, (const char **)argv));
}
void assert_messages(struct CuTest * tc, struct mlist *msglist, const message_type **types,
int num_msgs, bool exact_match, ...) {
char buf[100];
va_list args;
int found = 0, argc = -1;
struct message *msg;
bool match = true;
va_start(args, exact_match);
while (msglist) {
msg = msglist->msg;
if (found >= num_msgs) {
if (exact_match) {
slprintf(buf, sizeof(buf), "too many messages: %s", msg->type->name);
CuFail(tc, buf);
} else {
break;
}
}
if (exact_match || match)
argc = va_arg(args, int);
match = strcmp(types[argc]->name, msg->type->name) == 0;
if (match)
++found;
else if (exact_match)
CuAssertStrEquals(tc, types[argc]->name, msg->type->name);
msglist = msglist->next;
}
CuAssertIntEquals_Msg(tc, "not enough messages", num_msgs, found);
va_end(args);
}
void disabled_test(void *suite, void (*test)(CuTest *), const char *name) { void disabled_test(void *suite, void (*test)(CuTest *), const char *name) {
(void)test; (void)test;
fprintf(stderr, "%s: SKIP\n", name); fprintf(stderr, "%s: SKIP\n", name);

View File

@ -44,10 +44,6 @@ extern "C" {
struct message * test_find_messagetype(struct message_list *msgs, const char *name); struct message * test_find_messagetype(struct message_list *msgs, const char *name);
struct message * test_get_last_message(struct message_list *mlist); struct message * test_get_last_message(struct message_list *mlist);
const struct message_type *register_msg(const char *type, int n_param, ...);
void assert_messages(struct CuTest * tc, struct mlist *msglist, const struct message_type **types,
int num_msgs, bool exact_match, ...);
void disabled_test(void *suite, void (*)(struct CuTest *), const char *name); void disabled_test(void *suite, void (*)(struct CuTest *), const char *name);
#define DISABLE_TEST(SUITE, TEST) disabled_test(SUITE, TEST, #TEST) #define DISABLE_TEST(SUITE, TEST) disabled_test(SUITE, TEST, #TEST)

View File

@ -4,11 +4,23 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h> #include <stdarg.h>
#include <limits.h>
#include "bsdstring.h" #include "bsdstring.h"
#include "log.h"
int wrptr(char **ptr, size_t * size, size_t bytes) int wrptr(char **ptr, size_t * size, int result)
{ {
size_t bytes = (size_t)result;
if (result < 0) {
// _snprintf buffer was too small
if (*size > 0) {
**ptr = 0;
*size = 0;
}
errno = 0;
return ERANGE;
}
if (bytes == 0) { if (bytes == 0) {
return 0; return 0;
} }
@ -51,6 +63,23 @@ size_t strlcpy(char *dst, const char *src, size_t siz)
} }
#endif #endif
char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const char *file, int line)
{
size_t bytes = strlcpy(dst, src, *siz);
char * buf = dst;
assert(bytes <= INT_MAX);
if (wrptr(&buf, siz, (int)bytes) != 0) {
if (err) {
log_warning("%s: static buffer too small in %s:%d\n", err, file, line);
} else {
log_warning("static buffer too small in %s:%d\n", file, line);
}
}
return buf;
}
#ifndef HAVE_STRLCAT #ifndef HAVE_STRLCAT
#define HAVE_STRLCAT #define HAVE_STRLCAT
size_t strlcat(char *dst, const char *src, size_t siz) size_t strlcat(char *dst, const char *src, size_t siz)

View File

@ -2,21 +2,25 @@
#define UTIL_BSDSTRING_H #define UTIL_BSDSTRING_H
#include <stddef.h> #include <stddef.h>
extern int wrptr(char **ptr, size_t * size, size_t bytes); int wrptr(char **ptr, size_t * size, int bytes);
#ifndef HAVE_STRLCPY #ifndef HAVE_STRLCPY
extern size_t strlcpy(char *dst, const char *src, size_t siz); size_t strlcpy(char *dst, const char *src, size_t siz);
#endif #endif
char * strlcpy_w(char *dst, const char *src, size_t *siz, const char *err, const char *file, int line);
#ifndef HAVE_STRLCAT #ifndef HAVE_STRLCAT
extern size_t strlcat(char *dst, const char *src, size_t siz); size_t strlcat(char *dst, const char *src, size_t siz);
#endif #endif
#ifndef HAVE_SLPRINTF #ifndef HAVE_SLPRINTF
extern size_t slprintf(char * dst, size_t size, const char * format, ...); size_t slprintf(char * dst, size_t size, const char * format, ...);
#endif #endif
#define WARN_STATIC_BUFFER_EX(foo) log_warning("%s: static buffer too small in %s:%d\n", (foo), __FILE__, __LINE__)
#define WARN_STATIC_BUFFER() log_warning("static buffer too small in %s:%d\n", __FILE__, __LINE__) #define WARN_STATIC_BUFFER() log_warning("static buffer too small in %s:%d\n", __FILE__, __LINE__)
#define INFO_STATIC_BUFFER() log_info("static buffer too small in %s:%d\n", __FILE__, __LINE__) #define INFO_STATIC_BUFFER() log_info("static buffer too small in %s:%d\n", __FILE__, __LINE__)
#define STRLCPY(dst, src, siz) strlcpy_w((dst), (src), &(siz), 0, __FILE__, __LINE__)
#define STRLCPY_EX(dst, src, siz, err) strlcpy_w((dst), (src), (siz), (err), __FILE__, __LINE__)
#endif #endif

View File

@ -32,6 +32,32 @@ const char *mt_name(const message_type * mtype)
return mtype->name; return mtype->name;
} }
arg_type *argtypes = NULL;
void
register_argtype(const char *name, void(*free_arg) (variant),
variant(*copy_arg) (variant), variant_type type)
{
arg_type *atype = (arg_type *)malloc(sizeof(arg_type));
atype->name = name;
atype->next = argtypes;
atype->release = free_arg;
atype->copy = copy_arg;
atype->vtype = type;
argtypes = atype;
}
static arg_type *find_argtype(const char *name)
{
arg_type *atype = argtypes;
while (atype != NULL) {
if (strcmp(atype->name, name) == 0)
return atype;
atype = atype->next;
}
return NULL;
}
message_type *mt_new(const char *name, const char *args[]) message_type *mt_new(const char *name, const char *args[])
{ {
int i, nparameters = 0; int i, nparameters = 0;
@ -50,8 +76,8 @@ message_type *mt_new(const char *name, const char *args[])
mtype->name = _strdup(name); mtype->name = _strdup(name);
mtype->nparameters = nparameters; mtype->nparameters = nparameters;
if (nparameters > 0) { if (nparameters > 0) {
mtype->pnames = (const char **)malloc(sizeof(char *) * nparameters); mtype->pnames = (char **)malloc(sizeof(char *) * nparameters);
mtype->types = (const arg_type **)malloc(sizeof(arg_type *) * nparameters); mtype->types = (arg_type **)malloc(sizeof(arg_type *) * nparameters);
} }
else { else {
mtype->pnames = NULL; mtype->pnames = NULL;
@ -96,32 +122,6 @@ message_type *mt_new_va(const char *name, ...)
return mt_new(name, args); return mt_new(name, args);
} }
arg_type *argtypes = NULL;
void
register_argtype(const char *name, void(*free_arg) (variant),
variant(*copy_arg) (variant), variant_type type)
{
arg_type *atype = (arg_type *)malloc(sizeof(arg_type));
atype->name = name;
atype->next = argtypes;
atype->release = free_arg;
atype->copy = copy_arg;
atype->vtype = type;
argtypes = atype;
}
const arg_type *find_argtype(const char *name)
{
arg_type *atype = argtypes;
while (atype != NULL) {
if (strcmp(atype->name, name) == 0)
return atype;
atype = atype->next;
}
return NULL;
}
static variant copy_arg(const arg_type * atype, variant data) static variant copy_arg(const arg_type * atype, variant data)
{ {
assert(atype != NULL); assert(atype != NULL);
@ -161,6 +161,28 @@ message *msg_create(const struct message_type *mtype, variant args[])
#define MT_MAXHASH 1021 #define MT_MAXHASH 1021
static quicklist *messagetypes[MT_MAXHASH]; static quicklist *messagetypes[MT_MAXHASH];
static void mt_free(void *val) {
message_type *mtype = (message_type *)val;
int i;
for (i = 0; i != mtype->nparameters; ++i) {
free(mtype->pnames[i]);
}
free(mtype->pnames);
free(mtype->types);
free(mtype->name);
free(mtype);
}
void mt_clear(void) {
int i;
for (i = 0; i != MT_MAXHASH; ++i) {
quicklist *ql = messagetypes[i];
ql_foreach(ql, mt_free);
ql_free(ql);
messagetypes[i] = 0;
}
}
const message_type *mt_find(const char *name) const message_type *mt_find(const char *name)
{ {
unsigned int hash = hashstring(name) % MT_MAXHASH; unsigned int hash = hashstring(name) % MT_MAXHASH;

View File

@ -28,10 +28,10 @@ extern "C" {
typedef struct message_type { typedef struct message_type {
unsigned int key; unsigned int key;
const char *name; char *name;
int nparameters; int nparameters;
const char **pnames; char **pnames;
const struct arg_type **types; struct arg_type ** types;
} message_type; } message_type;
typedef struct message { typedef struct message {
@ -40,8 +40,9 @@ extern "C" {
int refcount; int refcount;
} message; } message;
extern struct message_type *mt_new(const char *name, const char **args); void mt_clear(void);
extern struct message_type *mt_new_va(const char *name, ...); struct message_type *mt_new(const char *name, const char **args);
struct message_type *mt_new_va(const char *name, ...);
/* mt_new("simple_sentence", "subject:string", "predicate:string", /* mt_new("simple_sentence", "subject:string", "predicate:string",
* "object:string", "lang:locale", NULL); */ * "object:string", "lang:locale", NULL); */
@ -61,9 +62,8 @@ extern "C" {
extern void register_argtype(const char *name, void(*free_arg) (variant), extern void register_argtype(const char *name, void(*free_arg) (variant),
variant(*copy_arg) (variant), variant_type); variant(*copy_arg) (variant), variant_type);
extern const struct arg_type *find_argtype(const char *name);
extern void(*msg_log_create) (const struct message * msg); void(*msg_log_create) (const struct message * msg);
#ifdef __cplusplus #ifdef __cplusplus
} }