From a1865ee2fa9906b407852f4425c56db38d1e0637 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sat, 1 Sep 2007 23:40:58 +0000 Subject: [PATCH] - Undo modifications to strlcpy from rev 3631 - Many more warnings when buffers too small. --- src/common/gamecode/laws.c | 129 ++++++-------- src/common/gamecode/monster.c | 18 +- src/common/gamecode/report.c | 315 ++++++++++++++++++---------------- src/common/kernel/battle.c | 56 +++--- src/common/kernel/eressea.c | 2 +- src/common/kernel/move.c | 40 +++-- src/common/kernel/order.c | 55 ++++-- src/common/kernel/race.c | 20 ++- src/common/kernel/reports.c | 227 ++++++++++-------------- src/common/kernel/reports.h | 1 - src/common/spells/spells.c | 14 +- src/common/util/bsdstring.c | 23 ++- src/common/util/bsdstring.h | 20 ++- src/common/util/translation.c | 16 +- 14 files changed, 478 insertions(+), 458 deletions(-) diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index 6ee0b9af7..185dcd014 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -1914,7 +1914,7 @@ email_cmd(unit * u, struct order * ord) static int password_cmd(unit * u, struct order * ord) { - char pbuf[32]; + char pwbuf[32]; int i; const char * s; boolean pwok = true; @@ -1924,14 +1924,14 @@ password_cmd(unit * u, struct order * ord) s = getstrtoken(); if (!s || !*s) { - for(i=0; i<6; i++) pbuf[i] = (char)(97 + rng_int() % 26); - pbuf[6] = 0; + for(i=0; i<6; i++) pwbuf[i] = (char)(97 + rng_int() % 26); + pwbuf[6] = 0; } else { char *c; - strlcpy(pbuf, (const char *)s, 31); - pbuf[31] = 0; - c = pbuf; + strlcpy(pwbuf, (const char *)s, 31); + pwbuf[31] = 0; + c = pwbuf; while (*c && pwok) { if (!isalnum(*c)) pwok = false; c++; @@ -1942,7 +1942,7 @@ password_cmd(unit * u, struct order * ord) cmistake(u, ord, 283, MSG_EVENT); u->faction->passw = strdup(itoa36(rng_int())); } else { - u->faction->passw = strdup(pbuf); + u->faction->passw = strdup(pwbuf); } fset(u->faction, FFL_OVERRIDE); ADDMSG(&u->faction->msgs, msg_message("changepasswd", @@ -2064,18 +2064,15 @@ display_race(faction *f, unit *u, const race * rc) const char *name, *key; const char *info; int a, at_count; - char buf[2048]; - char buf2[128]; - char * bufp = buf; - size_t size = sizeof(buf), rsize; + char buf[2048], * bufp = buf; + size_t size = sizeof(buf) - 1; + int bytes; if (u && u->race != rc) return false; name = rc_name(rc, 0); - rsize = slprintf(bufp, size, "%s: ", LOC(f->locale, name)); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = slprintf(bufp, size, "%s: ", LOC(f->locale, name)); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); key = mkname("raceinfo", rc->_name[0]); info = locale_getstring(f->locale, key); @@ -2083,46 +2080,31 @@ display_race(faction *f, unit *u, const race * rc) info = locale_string(f->locale, mkname("raceinfo", "no_info")); } - rsize = strlcpy(bufp, info, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, info, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); /* hp_p : Trefferpunkte */ - sprintf(buf2, " %d %s", rc->hitpoints, LOC(f->locale, "stat_hitpoints")); - - rsize = strlcpy(bufp, buf2, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, " %d %s", rc->hitpoints, LOC(f->locale, "stat_hitpoints")); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); /* b_attacke : Angriff */ - sprintf(buf2, ", %s: %d", LOC(f->locale, "stat_attack"), (rc->at_default+rc->at_bonus)); - rsize = strlcpy(bufp, buf2, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_attack"), (rc->at_default+rc->at_bonus)); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); /* b_defense : Verteidigung */ - sprintf(buf2, ", %s: %d", LOC(f->locale, "stat_defense"), (rc->df_default+rc->df_bonus)); - rsize = strlcpy(bufp, buf2, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_defense"), (rc->df_default+rc->df_bonus)); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); /* b_armor : Rüstung */ if (rc->armor > 0) { - sprintf(buf2, ", %s: %d", LOC(f->locale, "stat_armor"), rc->armor); - rsize = strlcpy(bufp, buf2, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, ", %s: %d", LOC(f->locale, "stat_armor"), rc->armor); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (size>1) { - strcpy(bufp++, "."); + *bufp++ ='.'; --size; - } + } else WARN_STATIC_BUFFER(); /* b_damage : Schaden */ at_count=0; @@ -2132,72 +2114,61 @@ display_race(faction *f, unit *u, const race * rc) } } if (rc->battle_flags & BF_EQUIPMENT) { - rsize = snprintf(bufp, size, " %s", LOC(f->locale, "stat_equipment")); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, " %s", LOC(f->locale, "stat_equipment")); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (rc->battle_flags & BF_RES_PIERCE) { - rsize = snprintf(bufp, size, " %s", LOC(f->locale, "stat_pierce")); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, " %s", LOC(f->locale, "stat_pierce")); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (rc->battle_flags & BF_RES_CUT) { - rsize = snprintf(bufp, size, " %s", LOC(f->locale, "stat_cut")); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, " %s", LOC(f->locale, "stat_cut")); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (rc->battle_flags & BF_RES_BASH) { - rsize = snprintf(bufp, size, " %s", LOC(f->locale, "stat_bash")); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = snprintf(bufp, size, " %s", LOC(f->locale, "stat_bash")); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - sprintf(buf2, " %d %s", at_count, LOC(f->locale, (at_count==1)?"stat_attack":"stat_attacks")); - rsize = strlcpy(bufp, buf2, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = 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(); for (a = 0; a < 6; a++) { if (rc->attack[a].type != AT_NONE){ - if (size>2) { - if (a!=0) strcat(bufp, ", "); - else strcat(bufp, ": "); - size -= 2; - bufp += 2; - } + if (a!=0) bytes = (int)strlcpy(bufp, ", ", size); + else strlcpy(bufp, ": ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + switch(rc->attack[a].type) { case AT_STANDARD: - sprintf(buf2, "%s (%s)", LOC(f->locale, "attack_standard"), rc->def_damage); + bytes = snprintf(bufp, size, "%s (%s)", LOC(f->locale, "attack_standard"), rc->def_damage); break; case AT_NATURAL: - sprintf(buf2, "%s (%s)", LOC(f->locale, "attack_natural"), rc->attack[a].data.dice); + bytes = snprintf(bufp, size, "%s (%s)", LOC(f->locale, "attack_natural"), rc->attack[a].data.dice); break; case AT_SPELL: case AT_COMBATSPELL: case AT_DRAIN_ST: case AT_DAZZLE: - sprintf(buf2, "%s", LOC(f->locale, "attack_natural")); + bytes = snprintf(bufp, size, "%s", LOC(f->locale, "attack_natural")); break; case AT_STRUCTURAL: - sprintf(buf2, "%s (%s)", LOC(f->locale, "attack_structural"), rc->attack[a].data.dice); + bytes = snprintf(bufp, size, "%s (%s)", LOC(f->locale, "attack_structural"), rc->attack[a].data.dice); + break; + default: + bytes = 0; } - rsize = strlcpy(bufp, buf2, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + + if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } if (size>1) { - strcat(bufp++, "."); + *bufp++ = '.'; --size; - } + } else WARN_STATIC_BUFFER(); + *bufp = 0; addmessage(0, f, buf, MSG_EVENT, ML_IMPORTANT); return true; diff --git a/src/common/gamecode/monster.c b/src/common/gamecode/monster.c index 7baacdb1d..c6107b488 100644 --- a/src/common/gamecode/monster.c +++ b/src/common/gamecode/monster.c @@ -452,26 +452,32 @@ make_movement_order(unit * u, const region * target, int moves, boolean (*allowe { region * r = u->region; region ** plan; - int position = 0; - char zOrder[128]; - char * c = zOrder; + int bytes, position = 0; + char zOrder[128], * bufp = zOrder; + size_t size = sizeof(zOrder) - 1; if (is_waiting(u)) return NULL; plan = path_find(r, target, DRAGON_RANGE*5, allowed); if (plan==NULL) return NULL; - c += strlcpy(c, (const char *)LOC(u->faction->locale, keywords[K_MOVE]), sizeof(zOrder)); + bytes = (int)strlcpy(bufp, (const char *)LOC(u->faction->locale, keywords[K_MOVE]), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); while (position!=moves && plan[position+1]) { region * prev = plan[position]; region * next = plan[++position]; direction_t dir = reldirection(prev, next); assert(dir!=NODIRECTION && dir!=D_SPECIAL); - *c++ = ' '; - c += strlcpy(c, (const char *)LOC(u->faction->locale, directions[dir]), sizeof(zOrder)-(c-(const char *)zOrder)); + if (size>1) { + *bufp++ = ' '; + --size; + } + bytes = (int)strlcpy(bufp, (const char *)LOC(u->faction->locale, directions[dir]), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } + *bufp = 0; return parse_order(zOrder, u->faction->locale); } diff --git a/src/common/gamecode/report.c b/src/common/gamecode/report.c index 6802ea6aa..454a55f73 100644 --- a/src/common/gamecode/report.c +++ b/src/common/gamecode/report.c @@ -101,9 +101,6 @@ extern int *storms; extern int weeks_per_month; extern int months_per_year; -#define WARN_STATIC_BUFFER() \ - log_warning(("static buffer too small in %s:%d\n", __FILE__, __LINE__)) - static char * gamedate_season(const struct locale * lang) { @@ -231,18 +228,18 @@ report_spell(FILE * F, spell *sp, const struct locale * lang) rparagraph(F, LOC(lang, "nr_spell_description"), 0, 0, 0); rparagraph(F, spell_info(sp, lang), 2, 0, 0); - bytes = strlcpy(bufp, LOC(lang, "nr_spell_type"), size); + bytes = (int)strlcpy(bufp, LOC(lang, "nr_spell_type"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (size) { *bufp++ = ' '; --size; } if (sp->sptyp & PRECOMBATSPELL) { - bytes = strlcpy(bufp, LOC(lang, "sptype_precombat"), size); + bytes = (int)strlcpy(bufp, LOC(lang, "sptype_precombat"), size); } else if (sp->sptyp & COMBATSPELL) { - bytes = strlcpy(bufp, LOC(lang, "sptype_combat"), size); + bytes = (int)strlcpy(bufp, LOC(lang, "sptype_combat"), size); } else if (sp->sptyp & POSTCOMBATSPELL) { - bytes = strlcpy(bufp, LOC(lang, "sptype_postcombat"), size); + bytes = (int)strlcpy(bufp, LOC(lang, "sptype_postcombat"), size); } else { - bytes = strlcpy(bufp, LOC(lang, "sptype_normal"), size); + bytes = (int)strlcpy(bufp, LOC(lang, "sptype_normal"), size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); *bufp = 0; @@ -283,44 +280,44 @@ report_spell(FILE * F, spell *sp, const struct locale * lang) size = sizeof(buf) - 1; bufp = buf; - bytes = strlcpy(buf, LOC(lang, "nr_spell_modifiers"), size); + bytes = (int)strlcpy(buf, LOC(lang, "nr_spell_modifiers"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (sp->sptyp & FARCASTING) { - bytes = strlcpy(bufp, " Fernzauber", size); + bytes = (int)strlcpy(bufp, " Fernzauber", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); dh = 1; } if (sp->sptyp & OCEANCASTABLE) { if (dh == 1) { - bytes = strlcpy(bufp, ",", size); + bytes = (int)strlcpy(bufp, ",", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - bytes = strlcpy(bufp, " Seezauber", size); + bytes = (int)strlcpy(bufp, " Seezauber", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); dh = 1; } if (sp->sptyp & ONSHIPCAST) { if (dh == 1){ - bytes = strlcpy(bufp, ",", size); + bytes = (int)strlcpy(bufp, ",", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - bytes = strlcpy(bufp, " Schiffszauber", size); + bytes = (int)strlcpy(bufp, " Schiffszauber", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); dh = 1; } if (sp->sptyp & NOTFAMILIARCAST) { if (dh == 1) { - bytes = strlcpy(bufp, ", k", size); + bytes = (int)strlcpy(bufp, ", k", size); } else { - bytes = strlcpy(bufp, " K", size); + bytes = (int)strlcpy(bufp, " K", size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, "ann nicht vom Vertrauten gezaubert werden", size); + bytes = (int)strlcpy(bufp, "ann nicht vom Vertrauten gezaubert werden", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); dh = 1; } if (dh == 0) { - bytes = strlcpy(bufp, " Keine", size); + bytes = (int)strlcpy(bufp, " Keine", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } *bufp = 0; @@ -331,9 +328,9 @@ report_spell(FILE * F, spell *sp, const struct locale * lang) bufp = buf; size = sizeof(buf) - 1; if (sp->sptyp & ISCOMBATSPELL) { - bytes = strlcpy(buf, LOC(lang, keywords[K_COMBAT]), size); + bytes = (int)strlcpy(buf, LOC(lang, keywords[K_COMBAT]), size); } else { - bytes = strlcpy(buf, LOC(lang, keywords[K_CAST]), size); + bytes = (int)strlcpy(buf, LOC(lang, keywords[K_CAST]), size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); @@ -347,33 +344,33 @@ report_spell(FILE * F, spell *sp, const struct locale * lang) } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, " \"", size); + bytes = (int)strlcpy(bufp, " \"", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, spell_name(sp, lang), size); + bytes = (int)strlcpy(bufp, spell_name(sp, lang), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, "\" ", size); + bytes = (int)strlcpy(bufp, "\" ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (sp->sptyp & ONETARGET){ if (sp->sptyp & UNITSPELL) { - bytes = strlcpy(bufp, "", size); + bytes = (int)strlcpy(bufp, "", size); } else if (sp->sptyp & SHIPSPELL) { - bytes = strlcpy(bufp, "", size); + bytes = (int)strlcpy(bufp, "", size); } else if (sp->sptyp & BUILDINGSPELL) { - bytes = strlcpy(bufp, "", size); + bytes = (int)strlcpy(bufp, "", size); } else { bytes = 0; } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { if (sp->sptyp & UNITSPELL) { - bytes = strlcpy(bufp, " [ ...]", size); + bytes = (int)strlcpy(bufp, " [ ...]", size); } else if (sp->sptyp & SHIPSPELL) { - bytes = strlcpy(bufp, " [ ...]", size); + bytes = (int)strlcpy(bufp, " [ ...]", size); } else if (sp->sptyp & BUILDINGSPELL) { - bytes = strlcpy(bufp, " [ ...]", size); + bytes = (int)strlcpy(bufp, " [ ...]", size); } else { bytes = 0; } @@ -685,40 +682,40 @@ prices(FILE * F, const region * r, const faction * f) m = msg_message("nr_market_sale", "product price", sale->itype->rtype, sale->price); - bytes = nr_render(m, f->locale, bufp, size, f); + bytes = (int)nr_render(m, f->locale, bufp, size, f); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); msg_release(m); if (n > 0) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "nr_trade_intro"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_trade_intro"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); for (dmd=r->land->demands;dmd;dmd=dmd->next) if(dmd->value > 0) { m = msg_message("nr_market_price", "product price", dmd->type->itype->rtype, dmd->value * dmd->type->price); - bytes = nr_render(m, f->locale, bufp, size, f); + bytes = (int)nr_render(m, f->locale, bufp, size, f); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); msg_release(m); n--; if (n == 0) { - bytes = strlcpy(bufp, LOC(f->locale, "nr_trade_end"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_trade_end"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else if (n == 1) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "nr_trade_final"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_trade_final"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { - bytes = strlcpy(bufp, LOC(f->locale, "nr_trade_next"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_trade_next"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } @@ -803,28 +800,28 @@ describe(FILE * F, const region * r, int partial, faction * f) } } - bytes = f_regionid(r, f, bufp, size); + bytes = (int)f_regionid(r, f, bufp, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (partial == 1) { - bytes = strlcpy(bufp, " (durchgereist)", size); + bytes = (int)strlcpy(bufp, " (durchgereist)", size); } else if (partial == 3) { - bytes = strlcpy(bufp, " (benachbart)", size); + bytes = (int)strlcpy(bufp, " (benachbart)", size); } else if (partial == 2) { - bytes = strlcpy(bufp, " (vom Turm erblickt)", size); + bytes = (int)strlcpy(bufp, " (vom Turm erblickt)", size); } else { bytes = 0; } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); /* Terrain */ - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); tname = terrain_name(r); - bytes = strlcpy(bufp, LOC(f->locale, tname), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, tname), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); /* Trees */ @@ -837,15 +834,15 @@ describe(FILE * F, const region * r, int partial, faction * f) if (fval(r, RF_MALLORN)) { if (trees == 1) { - bytes= strlcpy(bufp, LOC(f->locale, "nr_mallorntree"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_mallorntree"), size); } else { - bytes = strlcpy(bufp, LOC(f->locale, "nr_mallorntree_p"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_mallorntree_p"), size); } } else if (trees == 1) { - bytes = strlcpy(bufp, LOC(f->locale, "nr_tree"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_tree"), size); } else { - bytes = strlcpy(bufp, LOC(f->locale, "nr_tree_p"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_tree_p"), size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } @@ -893,21 +890,21 @@ describe(FILE * F, const region * r, int partial, faction * f) if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (fval(r, RF_ORCIFIED)) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, n==1?"rc_orc":"rc_orc_p"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, n==1?"rc_orc":"rc_orc_p"), size); } else { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, n==1?"peasant":"peasant_p"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, n==1?"peasant":"peasant_p"), size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (rmoney(r) && partial == 0) { bytes = snprintf(bufp, size, ", %d ", rmoney(r)); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, resourcename(oldresourcetype[R_SILVER], rmoney(r)!=1)), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, resourcename(oldresourcetype[R_SILVER], rmoney(r)!=1)), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } /* Pferde */ @@ -915,22 +912,22 @@ describe(FILE * F, const region * r, int partial, faction * f) if (rhorses(r)) { bytes = snprintf(bufp, size, ", %d ", rhorses(r)); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, resourcename(oldresourcetype[R_HORSE], (rhorses(r)>1)?GR_PLURAL:0)), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, resourcename(oldresourcetype[R_HORSE], (rhorses(r)>1)?GR_PLURAL:0)), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - bytes = strlcpy(bufp, ".", size); + bytes = (int)strlcpy(bufp, ".", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (r->display && r->display[0]) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, r->display, size); + bytes = (int)strlcpy(bufp, r->display, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); n = r->display[strlen(r->display) - 1]; if (n != '!' && n != '?' && n != '.') { - bytes = strlcpy(bufp, ".", size); + bytes = (int)strlcpy(bufp, ".", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } @@ -946,9 +943,9 @@ describe(FILE * F, const region * r, int partial, faction * f) a = a_find(r->attribs, &at_overrideroads); if (a) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, (char *)a->data.v, size); + bytes = (int)strlcpy(bufp, (char *)a->data.v, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { int nrd = 0; @@ -967,23 +964,23 @@ describe(FILE * F, const region * r, int partial, faction * f) if (dh) { char regname[4096]; if (nrd == 0) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "nr_nb_final"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_nb_final"), size); } else { - bytes = strlcpy(bufp, LOC(f->locale, "nr_nb_next"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_nb_next"), size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, directions[d]), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, directions[d]), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp++, " ", size); + bytes = (int)strlcpy(bufp++, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); f_regionid(r2, f, regname, sizeof(regname)); bytes = snprintf(bufp, size, trailinto(r2, f->locale), regname); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); MSG(("nr_vicinitystart", "dir region", d, r2), bufp, size, f->locale, f); bufp += strlen(bufp); @@ -993,17 +990,17 @@ describe(FILE * F, const region * r, int partial, faction * f) /* Spezielle Richtungen */ for (a = a_find(r->attribs, &at_direction);a && a->type==&at_direction;a=a->next) { spec_direction * d = (spec_direction *)(a->data.v); - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, d->desc), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, d->desc), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, " (\"", size); + bytes = (int)strlcpy(bufp, " (\"", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, d->keyword), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, d->keyword), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, "\")", size); + bytes = (int)strlcpy(bufp, "\")", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, ".", size); + bytes = (int)strlcpy(bufp, ".", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); dh = 1; } @@ -1021,19 +1018,19 @@ describe(FILE * F, const region * r, int partial, faction * f) if (rl) { bufp = buf; size = sizeof(buf) - 1; - bytes = strlcpy(bufp, "Schemen der Regionen ", size); + bytes = (int)strlcpy(bufp, "Schemen der Regionen ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); rl2 = rl; while (rl2) { - bytes = f_regionid(rl2->data, f, bufp, size); + bytes = (int)f_regionid(rl2->data, f, bufp, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); rl2 = rl2->next; if (rl2) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } - strlcpy(bufp, " sind erkennbar.", size); + bytes = (int)strlcpy(bufp, " sind erkennbar.", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); free_regionlist(rl); /* Schreibe Paragraphen */ @@ -1062,21 +1059,21 @@ describe(FILE * F, const region * r, int partial, faction * f) size = sizeof(buf) - 1; for (d=0;d!=MAXDIRECTIONS;++d) { if (!e->exist[d]) continue; - if (first) bytes = strlcpy(bufp, "Im ", size); - else if (e->lastd==d) bytes = strlcpy(bufp, " und im ", size); - else bytes= strlcpy(bufp, ", im ", size ); + if (first) bytes = (int)strlcpy(bufp, "Im ", size); + else if (e->lastd==d) bytes = (int)strlcpy(bufp, " und im ", size); + else bytes = (int)strlcpy(bufp, ", im ", size ); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, directions[d]), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, directions[d]), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); first = false; } - if (!e->transparent) bytes = strlcpy(bufp, " versperrt ", size); - else bytes = strlcpy(bufp, " befindet sich ", size); + if (!e->transparent) bytes = (int)strlcpy(bufp, " versperrt ", size); + else bytes = (int)strlcpy(bufp, " befindet sich ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, e->name, size); + bytes = (int)strlcpy(bufp, e->name, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - if (!e->transparent) bytes = strlcpy(bufp, " die Sicht.", size); - else bytes = strlcpy(bufp, ".", size); + if (!e->transparent) bytes = (int)strlcpy(bufp, " die Sicht.", size); + else bytes = (int)strlcpy(bufp, ".", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); *bufp = 0; rparagraph(F, buf, 0, 0, 0); @@ -1202,31 +1199,31 @@ durchreisende(FILE * F, const region * r, const faction * f) ++counter; if (u->ship != NULL) { if (counter == 1) { - bytes = strlcpy(bufp, "Die ", size); + bytes = (int)strlcpy(bufp, "Die ", size); } else { - bytes = strlcpy(bufp, "die ", size); + bytes = (int)strlcpy(bufp, "die ", size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, shipname(u->ship), size); + bytes = (int)strlcpy(bufp, shipname(u->ship), size); } else { - bytes = strlcpy(bufp, unitname(u), size); + bytes = (int)strlcpy(bufp, unitname(u), size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (counter + 1 < maxtravel) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else if (counter + 1 == maxtravel) { - bytes = strlcpy(bufp, LOC(f->locale, "list_and"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } } } if (maxtravel == 1) { - bytes = strlcpy(bufp, " hat die Region durchquert.", size); + bytes = (int)strlcpy(bufp, " hat die Region durchquert.", size); } else { - bytes = strlcpy(bufp, " haben die Region durchquert.", size); + bytes = (int)strlcpy(bufp, " haben die Region durchquert.", size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); *bufp = 0; @@ -1348,26 +1345,26 @@ report_template(const char * filename, report_context * ctx, const char * charse int cost = buildingmaintenance(b, r_silver); if (cost > 0) { - bytes = strlcpy(bufp, ",U", size); + bytes = (int)strlcpy(bufp, ",U", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, itoa10(cost), size); + bytes = (int)strlcpy(bufp, itoa10(cost), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } else if (u->ship) { if (fval(u, UFL_OWNER)) { - bytes = strlcpy(bufp, ",S", size); + bytes = (int)strlcpy(bufp, ",S", size); } else { - bytes = strlcpy(bufp, ",s", size); + bytes = (int)strlcpy(bufp, ",s", size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, shipid(u->ship), size); + bytes = (int)strlcpy(bufp, shipid(u->ship), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (lifestyle(u) == 0) { - bytes = strlcpy(bufp, ",I", size); + bytes = (int)strlcpy(bufp, ",I", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - bytes = strlcpy(bufp, "]", size); + bytes = (int)strlcpy(bufp, "]", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); *bufp = 0; @@ -1420,9 +1417,11 @@ show_allies(FILE * F, const faction * f, const ally * allies, char * buf, size_t { int allierte = 0; int i=0, h, hh = 0; - int dh = 0; + int bytes, dh = 0; const ally * sf; - char * bufp = buf + strlen(buf); /* buf already contains data */ + char * bufp = buf; /* buf already contains data */ + + --size; /* leave room for a null-terminator */ for (sf = allies; sf; sf = sf->next) { int mode = alliedgroup(NULL, f, sf->faction, sf, HELP_ALL); @@ -1434,17 +1433,22 @@ show_allies(FILE * F, const faction * f, const ally * allies, char * buf, size_t if (mode <= 0) continue; i++; if (dh) { - if (i == allierte) - bufp += strlcpy(bufp, LOC(f->locale, "list_and"), size-(bufp-buf)); - else - bufp += strlcpy(bufp, ", ", size-(bufp-buf)); + if (i == allierte) { + bytes = (int)strlcpy(bufp, LOC(f->locale, "list_and"), size); + } else { + bytes = (int)strlcpy(bufp, ", ", size); + } + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } dh = 1; hh = 0; - bufp += strlcpy(bufp, factionname(sf->faction), size-(bufp-buf)); - bufp += strlcpy(bufp, " (", size-(bufp-buf)); + bytes = (int)strlcpy(bufp, factionname(sf->faction), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, " (", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if ((mode & HELP_ALL) == HELP_ALL) { - bufp += strlcpy(bufp, "Alles", size-(bufp-buf)); + bytes = (int)strlcpy(bufp, "Alles", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { for (h = 1; h < HELP_ALL; h *= 2) { int p = MAXPARAMS; @@ -1471,15 +1475,22 @@ show_allies(FILE * F, const faction * f, const ally * allies, char * buf, size_t } } if (p!=MAXPARAMS) { - if (hh) bufp += strlcpy(bufp, ", ", size-(bufp-buf)); - bufp += strlcpy(bufp, parameters[p], size-(bufp-buf)); + if (hh) { + bytes = (int)strlcpy(bufp, ", ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + } + bytes = (int)strlcpy(bufp, parameters[p], size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); hh = 1; } } } - bufp += strlcpy(bufp, ")", size-(bufp-buf)); + bytes = (int)strlcpy(bufp, ")", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - bufp += strlcpy(bufp, ".", size-(bufp-buf)); + bytes = (int)strlcpy(bufp, ".", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + *bufp = 0; rparagraph(F, buf, 0, 0, 0); rnl(F); } @@ -1491,22 +1502,28 @@ allies(FILE * F, const faction * f) char buf[16384]; if (f->allies) { + int bytes; + size_t size = sizeof(buf); if (!f->allies->next) { - strcpy(buf, "Wir helfen der Partei "); + bytes = (int)strlcpy(buf, "Wir helfen der Partei ", size); } else { - strcpy(buf, "Wir helfen den Parteien "); + bytes = (int)strlcpy(buf, "Wir helfen den Parteien ", size); } - show_allies(F, f, f->allies, buf, sizeof(buf)); + size -= bytes; + show_allies(F, f, f->allies, buf + bytes, size); } while (g) { if (g->allies) { + int bytes; + size_t size = sizeof(buf); if (!g->allies->next) { - sprintf(buf, "%s hilft der Partei ", g->name); + bytes = snprintf(buf, size, "%s hilft der Partei ", g->name); } else { - sprintf(buf, "%s hilft den Parteien ", g->name); + bytes = snprintf(buf, size, "%s hilft den Parteien ", g->name); } - show_allies(F, f, g->allies, buf, sizeof(buf)); + size -= bytes; + show_allies(F, f, g->allies, buf + bytes, size); } g = g->next; } @@ -1576,26 +1593,26 @@ guards(FILE * F, const region * r, const faction * see) size_t size = sizeof(buf) - 1; int bytes; - bytes = strlcpy(bufp, "Die Region wird von ", size); + bytes = (int)strlcpy(bufp, "Die Region wird von ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); for (i = 0; i!=nextguard+(tarned?1:0); ++i) { if (i!=0) { if (i == nextguard-(tarned?0:1)) { - bytes = strlcpy(bufp, LOC(see->locale, "list_and"), size); + bytes = (int)strlcpy(bufp, LOC(see->locale, "list_and"), size); } else { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); } if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (isize)); - bytes = strlcpy(bufp, bname, size); + bytes = (int)strlcpy(bufp, bname, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (type!=b->type) { unit * owner = buildingowner(r, b); @@ -1687,27 +1704,27 @@ report_building(FILE *F, const region * r, const building * b, const faction * f } if (b->size < type->maxsize) { - bytes = strlcpy(bufp, " (im Bau)", size); + bytes = (int)strlcpy(bufp, " (im Bau)", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (b->besieged > 0 && mode>=see_lighthouse) { - bytes = strlcpy(bufp, ", belagert von ", size); + bytes = (int)strlcpy(bufp, ", belagert von ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, itoa10(b->besieged), size); + bytes = (int)strlcpy(bufp, itoa10(b->besieged), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, " Personen ", size); + bytes = (int)strlcpy(bufp, " Personen ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (b->besieged >= b->size * SIEGEFACTOR) { - bytes = strlcpy(bufp, "(abgeschnitten)", size); + bytes = (int)strlcpy(bufp, "(abgeschnitten)", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } i = 0; if (b->display && b->display[0]) { - bytes = strlcpy(bufp, "; ", size); + bytes = (int)strlcpy(bufp, "; ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, b->display, size); + bytes = (int)strlcpy(bufp, b->display, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); i = b->display[strlen(b->display) - 1]; } @@ -1765,7 +1782,7 @@ report_building(FILE *F, const region * r, const building * b, const faction * f #else if (i != '!' && i != '?' && i != '.') { - bytes = strlcpy(bufp, ".", size); + bytes = (int)strlcpy(bufp, ".", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } @@ -1953,9 +1970,9 @@ report_plaintext(const char * filename, report_context * ctx, const char * chars if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); for (op = 0; op != MAXOPTIONS; op++) { if (f->options & want(op)) { - bytes = strlcpy(bufp, " ", size); + bytes = (int)strlcpy(bufp, " ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, options[op]), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, options[op]), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); #ifdef AT_OPTION if(op == O_NEWS) { @@ -1966,20 +1983,20 @@ report_plaintext(const char * filename, report_context * ctx, const char * chars } else { int sec = a->data.i; int i; - bytes = strlcpy(bufp, "(", size); + bytes = (int)strlcpy(bufp, "(", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); for (i=1; sec != 0; i *= 2) { if(sec & i) { - bytes = strlcpy(bufp, itoa10(i), size); + bytes = (int)strlcpy(bufp, itoa10(i), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); sec = sec & ~i; if (sec) { - bytes = strlcpy(bufp, ",", size); + bytes = (int)strlcpy(bufp, ",", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } } - bytes = strlcpy(bufp, ")", size); + bytes = (int)strlcpy(bufp, ")", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } @@ -2032,11 +2049,11 @@ report_plaintext(const char * filename, report_context * ctx, const char * chars bytes = snprintf(bufp, size, "%s: ", LOC(f->locale, "nr_herbsrequired")); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); while (m->number) { - bytes = strlcpy(bufp, resourcename(m->rtype, 0), size); + bytes = (int)strlcpy(bufp, resourcename(m->rtype, 0), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); ++m; if (m->number) - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } *bufp = 0; @@ -2175,22 +2192,22 @@ report_plaintext(const char * filename, report_context * ctx, const char * chars } if (!fval(r->terrain, SEA_REGION)) { if (sh->coast != NODIRECTION) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, coasts[sh->coast]), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, coasts[sh->coast]), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } ch = 0; if (sh->display && sh->display[0]) { - bytes = strlcpy(bufp, "; ", size); + bytes = (int)strlcpy(bufp, "; ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, sh->display, size); + bytes = (int)strlcpy(bufp, sh->display, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); ch = sh->display[strlen(sh->display) - 1]; } if (ch != '!' && ch != '?' && ch != '.') { - bytes = strlcpy(bufp, ".", size); + bytes = (int)strlcpy(bufp, ".", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } *bufp = 0; diff --git a/src/common/kernel/battle.c b/src/common/kernel/battle.c index b10e1c0b9..495406235 100644 --- a/src/common/kernel/battle.c +++ b/src/common/kernel/battle.c @@ -2638,14 +2638,18 @@ print_header(battle * b) boolean first = false; side * s; char * bufp = zText; + size_t size = sizeof(zText) - 1; + int bytes; for (s=b->sides; s; s=s->next) { fighter *df; for (df=s->fighters;df;df=df->next) { if (is_attacker(df)) { - if (first) bufp += strlcpy(bufp, ", ", sizeof(zText) - (bufp-zText)); + if (first) bytes = (int)strlcpy(bufp, ", ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (lastf) { - bufp += strlcpy(bufp, (const char *)lastf, sizeof(zText) - (bufp-zText)); + bytes = (int)strlcpy(bufp, (const char *)lastf, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); first = true; } if (seematrix(f, s) == true) @@ -2657,11 +2661,17 @@ print_header(battle * b) } } if (first) { - bufp += strlcpy(bufp, " ", sizeof(zText) - (bufp-zText)); - bufp += strlcpy(bufp, (const char *)LOC(f->locale, "and"), sizeof(zText) - (bufp-zText)); - bufp += strlcpy(bufp, " ", sizeof(zText) - (bufp-zText)); + bytes = (int)strlcpy(bufp, " ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, (const char *)LOC(f->locale, "and"), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, " ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + } + if (lastf) { + bytes = (int)strlcpy(bufp, (const char *)lastf, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - if (lastf) bufp += strlcpy(bufp, (const char *)lastf, sizeof(zText) - (bufp-zText)); m = msg_message("battle::starters", "factions", zText); message_faction(b, f, m); @@ -3310,7 +3320,8 @@ battle_report(battle * b) faction * fac = bf->faction; char buf[32*MAXSIDES]; char * bufp = buf; - size_t size = sizeof(buf), rsize; + int bytes; + size_t size = sizeof(buf) - 1; message * m; message_faction(b, fac, msg_separator); @@ -3330,40 +3341,30 @@ battle_report(battle * b) char buffer[32]; if (komma) { - rsize = strlcpy(bufp, ", ", size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, ", ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } snprintf(buffer, sizeof(buffer), "%s %2d(%s): ", loc_army, army_index(s), abbrev); buffer[sizeof(buffer)-1] = 0; - rsize = strlcpy(bufp, buffer, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, buffer, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); for (r=FIGHT_ROW;r!=NUMROWS;++r) { if (alive[r]) { if (l!=FIGHT_ROW) { - rsize = strlcpy(bufp, "+", size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, "+", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } while (k--) { - rsize = strlcpy(bufp, "0+", size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, "0+", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } sprintf(buffer, "%d", alive[r]); - rsize = strlcpy(bufp, buffer, size); - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, buffer, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); k = 0; l = r+1; @@ -3373,6 +3374,7 @@ battle_report(battle * b) komma = true; } } + *bufp = 0; fbattlerecord(b, fac, buf); } return cont; diff --git a/src/common/kernel/eressea.c b/src/common/kernel/eressea.c index 5159041dd..b095d35d2 100644 --- a/src/common/kernel/eressea.c +++ b/src/common/kernel/eressea.c @@ -1597,7 +1597,7 @@ cstring(const char *s) { char *ibuf = idbuf[(++nextbuf) % 8]; - strlcpy(ibuf,s, sizeof(name)); + strlcpy(ibuf, s, sizeof(name)); return cstring_i(ibuf); } diff --git a/src/common/kernel/move.c b/src/common/kernel/move.c index afc0e6135..faf167081 100644 --- a/src/common/kernel/move.c +++ b/src/common/kernel/move.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -902,15 +903,15 @@ static const char *shortdirections[MAXDIRECTIONS] = static void cycle_route(order * ord, unit *u, int gereist) { - int cm = 0; - char tail[1024]; + int bytes, cm = 0; + char tail[1024], * bufp = tail; char neworder[2048]; const char *token; direction_t d = NODIRECTION; boolean paused = false; boolean pause; order * norder; - char * tail_end = tail; + size_t size = sizeof(tail) - 1; if (get_keyword(ord) != K_ROUTE) return; tail[0] = '\0'; @@ -933,18 +934,20 @@ cycle_route(order * ord, unit *u, int gereist) /* hier sollte keine PAUSE auftreten */ assert(!pause); if (!pause) { - size_t size = sizeof(tail)-(tail_end-tail); const char * loc = LOC(lang, shortdirections[d]); - *tail_end++ = ' '; - tail_end += strlcpy(tail_end, loc, size-1); + bytes = (int)strlcpy(bufp, " ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, loc, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } else if (strlen(neworder)>sizeof(neworder)/2) break; else if (cm == gereist && !paused && pause) { - size_t size = sizeof(tail)-(tail_end-tail); const char * loc = LOC(lang, parameters[P_PAUSE]); - *tail_end++ = ' '; - tail_end += strlcpy(tail_end, loc, size-1); + bytes = (int)strlcpy(bufp, " ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, loc, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); paused = true; } else if (pause) { @@ -2256,9 +2259,9 @@ static int hunt(unit *u, order * ord) { region *rc = u->region; - int moves, id, speed; - char command[256]; - char * bufp = command; + int bytes, moves, id, speed; + char command[256], * bufp = command; + size_t size = sizeof(command); direction_t dir; if (fval(u, UFL_NOTMOVING)) { @@ -2296,8 +2299,11 @@ hunt(unit *u, order * ord) return 0; } - bufp = command + sprintf(command, "%s %s", locale_string(u->faction->locale, keywords[K_MOVE]), - locale_string(u->faction->locale, directions[dir])); + bufp = command; + bytes = snprintf(bufp, size, "%s %s", LOC(u->faction->locale, keywords[K_MOVE]), + LOC(u->faction->locale, directions[dir])); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + moves = 1; speed = getuint(); @@ -2310,8 +2316,10 @@ hunt(unit *u, order * ord) rc = rconnect(rc, dir); while (moves < speed && (dir = hunted_dir(rc->attribs, id)) != NODIRECTION) { - bufp += strlcpy(bufp, " ", sizeof(command) - (bufp-command)); - bufp += strlcpy(bufp, LOC(u->faction->locale, directions[dir]), sizeof(command) - (bufp-command)); + bytes = (int)strlcpy(bufp, " ", size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, LOC(u->faction->locale, directions[dir]), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); moves++; rc = rconnect(rc, dir); } diff --git a/src/common/kernel/order.c b/src/common/kernel/order.c index 636ef10ad..7b28709fe 100644 --- a/src/common/kernel/order.c +++ b/src/common/kernel/order.c @@ -19,6 +19,7 @@ #include #include #include +#include #include /* libc includes */ @@ -86,25 +87,35 @@ get_keyword(const order * ord) } static char * -get_command(const order * ord, char * sbuffer, size_t bufsize) +get_command(const order * ord, char * sbuffer, size_t size) { - char * str = sbuffer; + char * bufp = sbuffer; const char * text = ORD_STRING(ord); keyword_t kwd = ORD_KEYWORD(ord); + int bytes; - if (ord->_persistent) *str++ = '@'; - if (kwd!=NOKEYWORD) { - const struct locale * lang = ORD_LOCALE(ord); - size_t size = bufsize-(str-sbuffer); - if (text) --size; - str += strlcpy(str, (const char*)LOC(lang, keywords[kwd]), size); - if (text) { - *str++ = ' '; + if (ord->_persistent) { + if (size>0) { + *bufp++ = '@'; + --size; + } else { + WARN_STATIC_BUFFER(); } } - if (text) { - str += strlcpy(str, (const char *)text, bufsize-(str-sbuffer)); + if (kwd!=NOKEYWORD) { + const struct locale * lang = ORD_LOCALE(ord); + if (size>0) { + if (text) --size; + bytes = (int)strlcpy(bufp, (const char*)LOC(lang, keywords[kwd]), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + if (text) *bufp++ = ' '; + } else WARN_STATIC_BUFFER(); } + if (text) { + bytes = (int)strlcpy(bufp, (const char *)text, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + } + *bufp = 0; return sbuffer; } @@ -267,7 +278,9 @@ create_order(keyword_t kwd, const struct locale * lang, const char * params, ... { char zBuffer[DISPLAYSIZE]; if (params) { - char * sptr = zBuffer; + char * bufp = zBuffer; + int bytes; + size_t size = sizeof(zBuffer) - 1; va_list marker; va_start(marker, params); @@ -279,26 +292,30 @@ create_order(keyword_t kwd, const struct locale * lang, const char * params, ... switch (*params) { case 's': s = va_arg(marker, const char *); - sptr += strlcpy(sptr, s, sizeof(zBuffer)-(sptr-zBuffer)); + bytes = (int)strlcpy(bufp, s, size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); break; case 'd': i = va_arg(marker, int); - sptr += strlcpy(sptr, itoa10(i), sizeof(zBuffer)-(sptr-zBuffer)); + bytes = (int)strlcpy(bufp, itoa10(i), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); break; case 'i': i = va_arg(marker, int); - sptr += strlcpy(sptr, itoa36(i), sizeof(zBuffer)-(sptr-zBuffer)); + bytes = (int)strlcpy(bufp, itoa36(i), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); break; default: assert(!"unknown format-character in create_order"); } - } else { - *sptr++ = *params; + } else if (size>0) { + *bufp++ = *params; + --size; } ++params; } va_end(marker); - *sptr = 0; + *bufp = 0; } else { zBuffer[0] = 0; } diff --git a/src/common/kernel/race.c b/src/common/kernel/race.c index 6d08f918a..c85975e73 100644 --- a/src/common/kernel/race.c +++ b/src/common/kernel/race.c @@ -252,13 +252,19 @@ racename(const struct locale *loc, const unit *u, const race * rc) if (prefix!=NULL) { static char lbuf[80]; - char * s = lbuf; - int ch; - s += strlcpy(lbuf, LOC(loc, mkname("prefix", prefix)), sizeof(lbuf)); - strlcpy(s, LOC(loc, rc_name(rc, u->number != 1)), sizeof(lbuf)-(s-lbuf)); - assert(~s[0] & 0x80|| !"unicode/not implemented"); - ch = tolower(*(unsigned char *)s); - *s = (char)ch; + char * bufp = lbuf; + size_t size = sizeof(lbuf) - 1; + int ch, bytes; + + bytes = (int)strlcpy(bufp, LOC(loc, mkname("prefix", prefix)), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + bytes = (int)strlcpy(bufp, LOC(loc, rc_name(rc, u->number != 1)), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); + *bufp = 0; + + assert(~lbuf[0] & 0x80|| !"unicode/not implemented"); + ch = tolower(*(unsigned char *)lbuf); + lbuf[0] = (char)ch; return lbuf; } return LOC(loc, rc_name(rc, u->number != 1)); diff --git a/src/common/kernel/reports.c b/src/common/kernel/reports.c index 3cf4cc5b2..cb8e7c706 100644 --- a/src/common/kernel/reports.c +++ b/src/common/kernel/reports.c @@ -71,9 +71,6 @@ boolean nocr = false; boolean nonr = false; boolean noreports = false; -#define WARN_STATIC_BUFFER() \ - log_warning(("static buffer too small in %s:%d\n", __FILE__, __LINE__)) - const char * g_reportdir; const char * visibility[] = { "none", @@ -95,21 +92,6 @@ const char *coasts[MAXDIRECTIONS] = "coast::w" }; -int -wrptr(char ** ptr, size_t * size, int bytes) -{ - if (bytes<=*size) { - *ptr += bytes; - *size -= bytes; - return 0; - } - else { - *ptr += *size; - *size = 0; - return 1; - } -} - const char * reportpath(void) { @@ -231,29 +213,30 @@ update_nmrs(void) static size_t buforder(char * bufp, size_t size, const order * ord, int mode) { - size_t tsize = 0, rsize; + size_t tsize = 0; + int bytes; - rsize = strlcpy(bufp, ", \"", size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, ", \"", size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (modesize) rsize = size-1; - size -= rsize; - bufp += rsize; + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (size>1) { - strcpy(bufp, "\""); - ++tsize; + *bufp++ ='\"'; + --size; + } else { + WARN_STATIC_BUFFER(); } + ++tsize; + return tsize; } @@ -291,7 +274,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz telepath_see = fspecial(f, FS_TELEPATHY); #endif /* KARMA_MODULE */ - bytes = strlcpy(bufp, unitname(u), size); + bytes = (int)strlcpy(bufp, unitname(u), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (!isbattle) { @@ -301,31 +284,31 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz attrib *a = a_find(u->attribs, &at_group); if (a) { group * g = (group*)a->data.v; - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, groupid(g, f), size); + bytes = (int)strlcpy(bufp, groupid(g, f), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } if (getarnt) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "anonymous"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "anonymous"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else if (a_otherfaction) { faction * otherfaction = get_otherfaction(a_otherfaction); if (otherfaction) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, factionname(otherfaction), size); + bytes = (int)strlcpy(bufp, factionname(otherfaction), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } } else { if (getarnt) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "anonymous"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "anonymous"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { if (a_otherfaction && alliedunit(u, f, HELP_FSTEALTH)) { @@ -333,20 +316,20 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz bytes = snprintf(bufp, size, ", %s (%s)", factionname(f), factionname(u->faction)); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } else { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, factionname(fv), size); + bytes = (int)strlcpy(bufp, factionname(fv), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } } } - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1 && effskill(u, SK_STEALTH) >= 6) { - bytes = strlcpy(bufp, "? ", size); + bytes = (int)strlcpy(bufp, "? ", size); } else { bytes = snprintf(bufp, size, "%d ", u->number); } @@ -354,12 +337,12 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz pzTmp = get_racename(u->attribs); if (pzTmp) { - bytes = strlcpy(bufp, pzTmp, size); + bytes = (int)strlcpy(bufp, pzTmp, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (u->faction==f && fval(u->race, RCF_SHAPESHIFTANY)) { - bytes = strlcpy(bufp, " (", size); + bytes = (int)strlcpy(bufp, " (", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, racename(f->locale, u, u->race), size); + bytes = (int)strlcpy(bufp, racename(f->locale, u, u->race), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (size>1) { strcpy(bufp++, ")"); @@ -367,12 +350,12 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz } } } else { - bytes = strlcpy(bufp, racename(f->locale, u, u->irace), size); + bytes = (int)strlcpy(bufp, racename(f->locale, u, u->irace), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (u->faction==f && u->irace!=u->race) { - bytes = strlcpy(bufp, " (", size); + bytes = (int)strlcpy(bufp, " (", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, racename(f->locale, u, u->race), size); + bytes = (int)strlcpy(bufp, racename(f->locale, u, u->race), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (size>1) { strcpy(bufp++, ")"); @@ -383,9 +366,9 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz #ifdef HEROES if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "hero"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "hero"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } #endif @@ -393,20 +376,20 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz if (u->number && (u->faction == f || telepath_see || isbattle)) { const char * c = locale_string(f->locale, hp_status(u)); - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, report_kampfstatus(u, f->locale), size); + bytes = (int)strlcpy(bufp, report_kampfstatus(u, f->locale), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (c || fval(u, UFL_HUNGER)) { - bytes = strlcpy(bufp, " (", size); + bytes = (int)strlcpy(bufp, " (", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (c) { - bytes = strlcpy(bufp, c, size); + bytes = (int)strlcpy(bufp, c, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (fval(u, UFL_HUNGER)) { - if (c) bytes = strlcpy(bufp, ", hungert", size); - else bytes = strlcpy(bufp, "hungert", size); + if (c) bytes = (int)strlcpy(bufp, ", hungert", size); + else bytes = (int)strlcpy(bufp, "hungert", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if (size>1) { @@ -416,16 +399,16 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz } } if (getguard(u)) { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, LOC(f->locale, "unit_guards"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "unit_guards"), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } if ((b = usiege(u))!=NULL) { - bytes = strlcpy(bufp, ", belagert ", size); + bytes = (int)strlcpy(bufp, ", belagert ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, buildingname(b), size); + bytes = (int)strlcpy(bufp, buildingname(b), size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } @@ -433,7 +416,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz if (u->faction == f || telepath_see) { skill * sv; for (sv = u->skills;sv!=u->skills+u->skill_size;++sv) { - bytes = spskill(bufp, size, f->locale, u, sv, &dh, 1); + bytes = (int)spskill(bufp, size, f->locale, u, sv, &dh, 1); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } @@ -474,7 +457,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz int in, bytes; report_item(u, itm, f, &ic, NULL, &in, false); if (in==0 || ic==NULL) continue; - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (!dh) { @@ -483,7 +466,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz dh = 1; } if (in == 1) { - bytes = strlcpy(bufp, ic, size); + bytes = (int)strlcpy(bufp, ic, size); } else { bytes = snprintf(bufp, size, "%d %s", in, ic); } @@ -507,10 +490,10 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz bytes = snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells")); dh = 1; } else { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); } if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, spell_name(sp, f->locale), size); + bytes = (int)strlcpy(bufp, spell_name(sp, f->locale), size); if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } @@ -527,7 +510,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz if (!dh){ dh = 1; } else { - bytes = strlcpy(bufp, ", ", size); + bytes = (int)strlcpy(bufp, ", ", size); if (bytes && wrptr(&bufp, &size, bytes)!=0) { WARN_STATIC_BUFFER(); } @@ -535,7 +518,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz sp = get_combatspell(u,i); if (sp) { int sl = get_combatspelllevel(u, i); - bytes = strlcpy(bufp, spell_name(sp, u->faction->locale), size); + bytes = (int)strlcpy(bufp, spell_name(sp, u->faction->locale), size); if (bytes && wrptr(&bufp, &size, bytes)!=0) { WARN_STATIC_BUFFER(); } @@ -545,7 +528,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } else { - bytes = strlcpy(bufp, LOC(f->locale, "nr_nospells"), size); + bytes = (int)strlcpy(bufp, LOC(f->locale, "nr_nospells"), size); if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } @@ -563,7 +546,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz for (ord=u->old_orders;ord;ord=ord->next) { if (is_repeated(ord)) { if (printedorders;ord;ord=ord->next) { if (is_repeated(ord)) { if (printedlocale); if (str) { - bytes = strlcpy(bufp, "; ", size); + bytes = (int)strlcpy(bufp, "; ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, str, size); + bytes = (int)strlcpy(bufp, str, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); i = str[strlen(str) - 1]; @@ -599,11 +582,11 @@ bufunit(const faction * f, const unit * u, int indent, int mode, char * buf, siz } pzTmp = uprivate(u); if (u->faction == f && pzTmp) { - bytes = strlcpy(bufp, " (Bem: ", size); + bytes = (int)strlcpy(bufp, " (Bem: ", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, pzTmp, size); + bytes = (int)strlcpy(bufp, pzTmp, size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - bytes = strlcpy(bufp, ")", size); + bytes = (int)strlcpy(bufp, ")", size); if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } @@ -629,7 +612,7 @@ spskill(char * buffer, size_t size, const struct locale * lang, const struct uni { char * bufp = buffer; int i, effsk; - size_t rsize; + int bytes; size_t tsize = 0; if (!u->number) return 0; @@ -639,72 +622,54 @@ spskill(char * buffer, size_t size, const struct locale * lang, const struct uni } } - rsize = strlcpy(bufp, ", ", size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, ", ", size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (!*dh) { - rsize = strlcpy(bufp, LOC(lang, "nr_skills"), size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, LOC(lang, "nr_skills"), size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - rsize = strlcpy(bufp, ": ", size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, ": ", size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); *dh = 1; } - rsize = strlcpy(bufp, skillname(sv->id, lang), size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, skillname(sv->id, lang), size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - rsize = strlcpy(bufp, " ", size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, " ", size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (sv->id == SK_MAGIC){ if (find_magetype(u) != M_GRAU){ - rsize = strlcpy(bufp, LOC(lang, mkname("school", magietypen[find_magetype(u)])), size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, LOC(lang, mkname("school", magietypen[find_magetype(u)])), size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); - rsize = strlcpy(bufp, " ", size); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = (int)strlcpy(bufp, " ", size); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } if (sv->id == SK_STEALTH && fval(u, UFL_STEALTH)) { i = u_geteffstealth(u); if (i>=0) { - rsize = slprintf(bufp, size, "%d/", i); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = slprintf(bufp, size, "%d/", i); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } effsk = effskill(u, sv->id); - rsize = slprintf(bufp, size, "%d", effsk); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = slprintf(bufp, size, "%d", effsk); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (u->faction->options & want(O_SHOWSKCHANGE)) { int oldeff = 0; @@ -718,11 +683,9 @@ spskill(char * buffer, size_t size, const struct locale * lang, const struct uni diff = effsk - oldeff; if (diff != 0) { - rsize = slprintf(bufp, size, " (%s%d)", (diff>0)?"+":"", diff); - tsize += rsize; - if (rsize>size) rsize = size-1; - size -= rsize; - bufp += rsize; + bytes = slprintf(bufp, size, " (%s%d)", (diff>0)?"+":"", diff); + tsize += bytes; + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } return tsize; @@ -1846,7 +1809,7 @@ eval_regions(struct opstack ** stack, const void * userdata) /* order -> string } for (i=begin;iregions[i], report); - int bytes = strlcpy(bufp, rname, size); + int bytes = (int)strlcpy(bufp, rname, size); if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (i+12) { @@ -1881,9 +1844,9 @@ eval_trail(struct opstack ** stack, const void * userdata) /* order -> string */ if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); if (i+2locale, "list_and"), size); + bytes = (int)strlcpy(bufp, LOC(report->locale, "list_and"), size); } else bytes = 0; if (bytes && wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); diff --git a/src/common/kernel/reports.h b/src/common/kernel/reports.h index a2c8a1f4c..22859f828 100644 --- a/src/common/kernel/reports.h +++ b/src/common/kernel/reports.h @@ -46,7 +46,6 @@ void lparagraph(struct strlist ** SP, char *s, int indent, char mark); const char *hp_status(const struct unit * u); extern size_t spskill(char * pbuf, size_t siz, const struct locale * lang, const struct unit * u, struct skill * sv, int *dh, int days); /* mapper */ extern void spunit(struct strlist ** SP, const struct faction * f, const struct unit * u, int indent, int mode); -extern int wrptr(char ** ptr, size_t * size, int bytes); extern int reports(void); extern int write_reports(struct faction * f, time_t ltime); diff --git a/src/common/spells/spells.c b/src/common/spells/spells.c index d6fe93961..ff1f5e3b2 100644 --- a/src/common/spells/spells.c +++ b/src/common/spells/spells.c @@ -542,11 +542,11 @@ sp_summon_familiar(castorder *co) int cast_level = co->level; const race * rc; skill_t sk; - int dh, dh1; + int dh, dh1, bytes; direction_t d; message * msg; - char zText[NAMESIZE]; - char * bufp = zText; + char zText[NAMESIZE], * bufp = zText; + size_t size = sizeof(zText) - 1; if (get_familiar(mage) != NULL ) { cmistake(mage, co->order, 199, MSG_MAGIC); @@ -601,12 +601,14 @@ sp_summon_familiar(castorder *co) dh1 = 1; } else { if (dh == 0) { - bufp += strlcpy(bufp, (const char*)LOC(mage->faction->locale, "list_and"), sizeof(zText) - (bufp-zText)); + bytes = (int)strlcpy(bufp, (const char*)LOC(mage->faction->locale, "list_and"), size); } else { - bufp += strlcpy(bufp, (const char*)", ", sizeof(zText) - (bufp-zText)); + bytes = (int)strlcpy(bufp, (const char*)", ", size); } + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } - bufp += strlcpy(bufp, (const char*)skillname(sk, mage->faction->locale), sizeof(zText) - (bufp-zText)); + bytes = (int)strlcpy(bufp, (const char*)skillname(sk, mage->faction->locale), size); + if (wrptr(&bufp, &size, bytes)!=0) WARN_STATIC_BUFFER(); } } ADDMSG(&mage->faction->msgs, msg_message("familiar_describe", diff --git a/src/common/util/bsdstring.c b/src/common/util/bsdstring.c index 6e40a5055..4e1c8e7a2 100644 --- a/src/common/util/bsdstring.c +++ b/src/common/util/bsdstring.c @@ -1,6 +1,24 @@ #include #include +#include + +#ifndef HAVE_INLINE #include "bsdstring.h" +#endif + +INLINE_FUNCTION int +wrptr(char ** ptr, size_t * size, int bytes) +{ + if (bytes<=*(int*)size) { + *ptr += bytes; + *size -= bytes; + return 0; + } + + *ptr += *size; + *size = 0; + return ENAMETOOLONG; +} #if !defined(HAVE_STRLCPY) INLINE_FUNCTION size_t @@ -22,10 +40,11 @@ strlcpy(char *dst, const char *src, size_t siz) /* copied from OpenBSD source if (n == 0) { if (siz != 0) *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; } -/* return(s - src - 1); count does not include NUL */ - return siz - n - 1; + return(s - src - 1); /* count does not include NUL */ } diff --git a/src/common/util/bsdstring.h b/src/common/util/bsdstring.h index c94643a06..ca0e94188 100644 --- a/src/common/util/bsdstring.h +++ b/src/common/util/bsdstring.h @@ -1,16 +1,20 @@ #ifndef UTIL_BSDSTRING_H #define UTIL_BSDSTRING_H -#if !defined(HAVE_STRLCPY) -# ifdef HAVE_INLINE -# include "bsdstring.c" -# define HAVE_STRLCPY -# else - extern size_t strlcpy(char *dst, const char *src, size_t siz); - extern size_t strlcat(char * dst, const char * src, size_t siz); -# endif +#ifdef HAVE_INLINE +# include "bsdstring.c" +#else + extern size_t strlcpy(char *dst, const char *src, size_t siz); + extern size_t strlcat(char * dst, const char * src, size_t siz); + extern int wrptr(char ** ptr, size_t * size, int bytes); #endif +#if !defined(HAVE_STRLCPY) +# define HAVE_STRLCPY +#endif + +#define WARN_STATIC_BUFFER() log_warning(("static buffer too small in %s:%d\n", __FILE__, __LINE__)) + #if !defined(HAVE_STRLPRINTF) # define HAVE_STRLPRINTF # define slprintf snprintf diff --git a/src/common/util/translation.c b/src/common/util/translation.c index 60da497a5..6c1675ccb 100644 --- a/src/common/util/translation.c +++ b/src/common/util/translation.c @@ -249,6 +249,7 @@ parse_string(opstack ** stack, const char* in, const void * userdata) /* (char*) { char * c; char * buffer = balloc(TOKENSIZE); + size_t size = TOKENSIZE - 1; const char * ic = in; char * oc = buffer; /* mode flags */ @@ -261,16 +262,18 @@ parse_string(opstack ** stack, const char* in, const void * userdata) /* (char*) f_escape = false; switch (*ic) { case 'n': - *oc++='\n'; + if (size>0) { *oc++='\n'; --size; } break; case 't': - *oc++='\t'; + if (size>0) { *oc++='\t'; --size; } break; default: - *oc++=*ic; + if (size>0) { *oc++=*ic; --size; } } } else { int ch = (unsigned char)(*ic); + int bytes; + switch (ch) { case '\\': f_escape = true; @@ -284,11 +287,14 @@ parse_string(opstack ** stack, const char* in, const void * userdata) /* (char*) ic = parse_symbol(stack, ++ic, userdata); if (ic==NULL) return NULL; c = (char*)opop_v(stack); - oc += strlcpy(oc, c, TOKENSIZE-(oc-buffer)); + bytes = (int)strlcpy(oc, c, size); + if (bytes<(int)size) oc += bytes; + else oc += size; bfree(c); break; default: - *oc++=*ic++; + if (size>0) { *oc++=*ic++; --size; } + else ++ic; } } }