Potential buffer overruns galore.

This commit is contained in:
Enno Rehling 2005-06-17 18:12:58 +00:00
parent 23ab322ee1
commit 0b8c2c94ef
8 changed files with 466 additions and 157 deletions

View File

@ -72,6 +72,7 @@
/* util includes */
#include <util/base36.h>
#include <util/bsdstring.h>
#include <util/event.h>
#include <util/goodies.h>
#include <util/log.h>
@ -2264,6 +2265,8 @@ display_item(faction *f, unit *u, const item_type * itype)
const char *name;
const char *info;
const char *key;
char * bufp = buf;
size_t size = sizeof(buf), rsize;
if (u && *i_find(&u->items, itype) == NULL) return false;
@ -2287,10 +2290,14 @@ display_item(faction *f, unit *u, const item_type * itype)
if (fp!=NULL) {
buf[0]='\0';
while (fgets(t, NAMESIZE, fp) != NULL) {
if (t[strlen(t) - 1] == '\n') {
t[strlen(t) - 1] = 0;
size_t len = strlen(t);
if (len>0 && t[len - 1] == '\n') {
t[len - 1] = 0;
}
strcat(buf, t);
rsize = strlcpy(bufp, t, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
fclose(fp);
info = buf;
@ -2336,11 +2343,16 @@ display_race(faction *f, unit *u, const race * rc)
const char *name;
int a, at_count;
char buf2[2048];
char * bufp = buf;
size_t size = sizeof(buf), rsize;
if (u && u->race != rc) return false;
name = rc_name(rc, 0);
sprintf(buf, "%s: ", LOC(f->locale, name));
rsize = slprintf(bufp, size, "%s: ", LOC(f->locale, name));
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
sprintf(filename, "showdata/%s", LOC(default_locale, name));
fp = fopen(filename, "r");
@ -2349,29 +2361,52 @@ display_race(faction *f, unit *u, const race * rc)
if (t[strlen(t) - 1] == '\n') {
t[strlen(t) - 1] = 0;
}
strcat(buf, t);
rsize = strlcpy(bufp, t, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
fclose(fp);
strcat(buf, ". ");
rsize = strlcpy(bufp, ". ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
/* hp_p : Trefferpunkte */
sprintf(buf2, "Trefferpunkte: %d", rc->hitpoints);
strcat(buf, buf2);
rsize = strlcpy(bufp, buf2, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
/* b_armor : Rüstung */
if (rc->armor > 0){
sprintf(buf2, ", Rüstung: %d", rc->armor);
strcat(buf, buf2);
rsize += strlcpy(bufp, buf2, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
/* b_attacke : Angriff */
sprintf(buf2, ", Angriff: %d", (rc->at_default+rc->at_bonus));
strcat(buf, buf2);
rsize = strlcpy(bufp, buf2, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
/* b_defense : Verteidigung */
sprintf(buf2, ", Verteidigung: %d", (rc->df_default+rc->df_bonus));
strcat(buf, buf2);
rsize = strlcpy(bufp, buf2, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
strcat(buf, ".");
if (size>1) {
strcpy(bufp++, ".");
--size;
}
/* b_damage : Schaden */
at_count=0;
@ -2381,26 +2416,43 @@ display_race(faction *f, unit *u, const race * rc)
}
}
if (rc->battle_flags & BF_EQUIPMENT) {
strcat(buf, " Kann Waffen benutzen.");
rsize = strlcpy(bufp, " Kann Waffen benutzen.", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (rc->battle_flags & BF_RES_PIERCE) {
strcat(buf, " Ist durch Stichwaffen, Bögen und Armbrüste schwer zu verwunden.");
rsize = strlcpy(bufp, " Ist durch Stichwaffen, Bögen und Armbrüste schwer zu verwunden.", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (rc->battle_flags & BF_RES_CUT) {
strcat(buf, " Ist durch Hiebwaffen schwer zu verwunden.");
rsize = strlcpy(bufp, " Ist durch Hiebwaffen schwer zu verwunden.", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (rc->battle_flags & BF_RES_BASH) {
strcat(buf, " Ist durch Schlagwaffen und Katapulte schwer zu verwunden.");
rsize = strlcpy(bufp, " Ist durch Schlagwaffen und Katapulte schwer zu verwunden.", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
sprintf(buf2, " Hat %d Angriff%s", at_count, (at_count>1)?"e":"");
strcat(buf, buf2);
rsize = strlcpy(bufp, buf2, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
for (a = 0; a < 6; a++) {
if (rc->attack[a].type != AT_NONE){
if (a!=0){
strcat(buf, ", ");
} else {
strcat(buf, ": ");
if (size>2) {
if (a!=0) strcat(bufp, ", ");
else strcat(bufp, ": ");
size -= 2;
bufp += 2;
}
switch(rc->attack[a].type) {
case AT_STANDARD:
@ -2418,11 +2470,17 @@ display_race(faction *f, unit *u, const race * rc)
case AT_STRUCTURAL:
sprintf(buf2, "ein Angriff, der %s Gebäudeschaden verursacht", rc->attack[a].data.dice);
}
strcat(buf, buf2);
rsize = strlcpy(bufp, buf2, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
strcat(buf, ".");
if (size>1) {
strcat(bufp++, ".");
--size;
}
addmessage(0, f, buf, MSG_EVENT, ML_IMPORTANT);

View File

@ -2749,7 +2749,7 @@ print_stats(battle * b)
faction * f = bf->faction;
const char * loc_army = LOC(f->locale, "battle_army");
fbattlerecord(b, f, " ");
sprintf(buf, "%s %d: %s", loc_army, side->index,
slprintf(buf, sizeof(buf), "%s %d: %s", loc_army, side->index,
seematrix(f, side)
? sidename(side,false) : LOC(f->locale, "unknown_faction"));
fbattlerecord(b, f, buf);
@ -2758,7 +2758,7 @@ print_stats(battle * b)
cv_foreach(s2, b->sides) {
if (enemy(s2, side)) {
const char * abbrev = seematrix(f, s2)?sideabkz(s2, false):"-?-";
sprintf(buf, "%s%s %s %d(%s)", buf, komma++ ? "," : "", loc_army,
slprintf(buf, sizeof(buf), "%s%s %s %d(%s)", buf, komma++ ? "," : "", loc_army,
s2->index, abbrev);
}
}
@ -2769,7 +2769,7 @@ print_stats(battle * b)
cv_foreach(s2, b->sides) {
if (side->enemy[s2->index] & E_ATTACKING) {
const char * abbrev = seematrix(f, s2)?sideabkz(s2, false):"-?-";
sprintf(buf, "%s%s %s %d(%s)", buf, komma++ ? "," : "", loc_army,
slprintf(buf, sizeof(buf), "%s%s %s %d(%s)", buf, komma++ ? "," : "", loc_army,
s2->index, abbrev);
}
}
@ -2781,10 +2781,10 @@ print_stats(battle * b)
battlerecord(b, buf);
if (side->bf->faction) {
if (side->bf->faction->alliance) {
sprintf(buf, "##### %s (%s/%d)", side->bf->faction->name, itoa36(side->bf->faction->no),
slprintf(buf, sizeof(buf), "##### %s (%s/%d)", side->bf->faction->name, itoa36(side->bf->faction->no),
side->bf->faction->alliance?side->bf->faction->alliance->id:0);
} else {
sprintf(buf, "##### %s (%s)", side->bf->faction->name, itoa36(side->bf->faction->no));
slprintf(buf, sizeof(buf), "##### %s (%s)", side->bf->faction->name, itoa36(side->bf->faction->no));
}
battledebug(buf);
}
@ -3037,7 +3037,7 @@ make_fighter(battle * b, unit * u, side * s1, boolean attack)
while (*s) *c++ = (char)toupper(*s++);
*c = 0;
fig->person[0].hp = unit_max_hp(u) * 3;
sprintf(buf, "Eine Stimme ertönt über dem Schlachtfeld. 'DIESES %sKIND IST MEIN. IHR SOLLT ES NICHT HABEN.'. Eine leuchtende Aura umgibt %s", lbuf, unitname(u));
slprintf(buf, sizeof(buf), "Eine Stimme ertönt über dem Schlachtfeld. 'DIESES %sKIND IST MEIN. IHR SOLLT ES NICHT HABEN.'. Eine leuchtende Aura umgibt %s", lbuf, unitname(u));
battlerecord(b, buf);
}
@ -3305,6 +3305,7 @@ battle_report(battle * b)
for (bf=b->factions;bf;bf=bf->next) {
faction * fac = bf->faction;
char * bufp = buf;
size_t size = sizeof(buf), rsize;
message * m;
fbattlerecord(b, fac, " ");
@ -3323,23 +3324,42 @@ battle_report(battle * b)
const char * loc_army = LOC(fac->locale, "battle_army");
char buffer[32];
if (komma) bufp += strlcpy(bufp, ", ", sizeof(buf) - (bufp - buf));
if (komma) {
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
snprintf(buffer, sizeof(buffer), "%s %2d(%s): ",
loc_army, s->index, abbrev);
buffer[sizeof(buffer)-1] = 0;
bufp += strlcpy(bufp, buffer, sizeof(buf) - (bufp - buf));
rsize = strlcpy(bufp, buffer, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
for (r=FIGHT_ROW;r!=NUMROWS;++r) {
if (alive[r]) {
if (l!=FIGHT_ROW) {
bufp += strlcpy(bufp, "+", sizeof(buf) - (bufp - buf));
rsize = strlcpy(bufp, "+", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
while (k--) {
bufp += strlcpy(bufp, "0+", sizeof(buf) - (bufp - buf));
rsize = strlcpy(bufp, "0+", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
sprintf(buffer, "%d", alive[r]);
bufp += strlcpy(bufp, buffer, sizeof(buf) - (bufp - buf));
rsize = strlcpy(bufp, buffer, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
k = 0;
l = r+1;
} else ++k;

View File

@ -3339,9 +3339,11 @@ freadstr(FILE * F, char * start, size_t size)
}
break;
default:
if ((size_t)(str-start+1)<size) {
*str++ = (char)c;
}
}
}
}
/** writes a quoted string to the file

View File

@ -36,7 +36,8 @@
#include <string.h>
/* util includes */
#include <base36.h>
#include <util/base36.h>
#include <util/bsdstring.h>
/* Untote */
@ -293,15 +294,15 @@ zombie_name(const unit * u)
/* nur 50% aller Namen haben "Nach-Teil", wenn kein Vor-Teil */
if (uv < ZOM_VOR) {
strcpy(name, zombie_vor[uv]);
strlcpy(name, zombie_vor[uv], sizeof(name));
} else {
name[0] = 0;
}
strcat(name, zombie[uu]);
strlcat(name, zombie[uu], sizeof(name));
if (un < ZOM_NACH)
strcat(name, zombie_nach[un]);
strlcat(name, zombie_nach[un], sizeof(name));
return name;
}

View File

@ -16,6 +16,8 @@
#include "order.h"
#include "skill.h"
#include <util/bsdstring.h>
/* libc includes */
#include <assert.h>
#include <ctype.h>
@ -107,15 +109,18 @@ getcommand(const order * ord)
#ifdef SHORT_STRINGS
if (kwd!=NOKEYWORD) {
const struct locale * lang = ORD_LOCALE(ord);
strcpy(str, LOC(lang, keywords[kwd]));
str += strlen(str);
size_t size = sizeof(sbuffer)-(str-sbuffer);
if (text) --size;
str += strlcpy(str, LOC(lang, keywords[kwd]), size);
if (text) {
*str++ = ' ';
*str = 0;
}
}
#endif
if (text) strcpy(str, text);
if (text) {
str += strlcpy(str, text, sizeof(sbuffer)-(str-sbuffer));
}
return strdup(sbuffer);
}

View File

@ -43,8 +43,9 @@
#endif
/* util includes */
#include <base36.h>
#include <goodies.h>
#include <util/bsdstring.h>
#include <util/base36.h>
#include <util/goodies.h>
/* libc includes */
#include <assert.h>
@ -56,8 +57,6 @@
#include <attributes/otherfaction.h>
#include <attributes/racename.h>
#include <util/bsdstring.h>
const char * g_reportdir;
const char *neue_gebiete[] = {
@ -166,19 +165,30 @@ report_item(const unit * owner, const item * i, const faction * viewer, const ch
}
size_t
static size_t
buforder(char * bufp, size_t size, const order * ord)
{
char * cmd = getcommand(ord);
size_t len = 0;
len += strlcpy(bufp+len, ", \"", size);
len += strlcpy(bufp+len, cmd, size-len);
if (len>=2) {
strcpy(bufp+len, "\"");
++len;
size_t tsize = 0, rsize;
rsize = strlcpy(bufp, ", \"", size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, cmd, size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (size>1) {
strcpy(bufp, "\"");
++tsize;
}
free(cmd);
return len;
return tsize;
}
int
@ -200,6 +210,7 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
boolean itemcloak = false;
static const curse_type * itemcloak_ct = 0;
static boolean init = false;
size_t size = sizeof(buf), rsize;
if (!init) {
init = true;
@ -212,7 +223,10 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
if (fspecial(u->faction, FS_HIDDEN))
a_fshidden = a_find(u->attribs, &at_fshidden);
bufp += strlcpy(bufp, unitname(u), sizeof(buf));
rsize = strlcpy(bufp, unitname(u), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (!isbattle) {
attrib *a_otherfaction = a_find(u->attribs, &at_otherfaction);
@ -221,31 +235,62 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
attrib *a = a_find(u->attribs, &at_group);
if (a) {
group * g = (group*)a->data.v;
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, groupid(g, f), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, groupid(g, f), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
if (getarnt) {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, LOC(f->locale, "anonymous"), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, LOC(f->locale, "anonymous"), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
} else if (a_otherfaction) {
faction * otherfaction = get_otherfaction(a_otherfaction);
if (otherfaction) {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, factionname(otherfaction), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, factionname(otherfaction), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
} else {
if (getarnt) {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, LOC(f->locale, "anonymous"), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, LOC(f->locale, "anonymous"), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
} else {
if (a_otherfaction && alliedunit(u, f, HELP_FSTEALTH)) {
faction * f = get_otherfaction(a_otherfaction);
bufp += sprintf(bufp, ", %s (%s)", factionname(f), factionname(u->faction));
bufp += snprintf(bufp, size, ", %s (%s)", factionname(f), factionname(u->faction));
size = sizeof(buf)-(bufp-buf);
} else {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, factionname(fv), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, factionname(fv), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
}
@ -256,81 +301,164 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
if (a) {
ugroup *ug = findugroupid(u->faction, a->data.i);
if (is_ugroupleader(u, ug)) {
if (size>1) {
strcpy(bufp++, "*");
--size;
}
bufp += strlcpy(bufp, itoa36(ug->id), sizeof(buf)-(bufp-buf));
}
rsize = strlcpy(bufp, itoa36(ug->id), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
#endif
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (u->faction != f && a_fshidden && a_fshidden->data.ca[0] == 1 && effskill(u, SK_STEALTH) >= 6) {
bufp += strlcpy(bufp, "? ", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, "? ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
} else {
bufp += sprintf(bufp, "%d ", u->number);
bufp += snprintf(bufp, size, "%d ", u->number);
size = sizeof(buf)-(bufp-buf);
}
pzTmp = get_racename(u->attribs);
if (pzTmp) {
bufp += strlcpy(bufp, pzTmp, sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, pzTmp, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (u->faction==f && fval(u->race, RCF_SHAPESHIFTANY)) {
bufp += strlcpy(bufp, " (", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, racename(f->locale, u, u->race), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, " (", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, racename(f->locale, u, u->race), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (size>1) {
strcpy(bufp++, ")");
--size;
}
}
} else {
bufp += strlcpy(bufp, racename(f->locale, u, u->irace), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, racename(f->locale, u, u->irace), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (u->faction==f && u->irace!=u->race) {
bufp += strlcpy(bufp, " (", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, racename(f->locale, u, u->race), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, " (", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, racename(f->locale, u, u->race), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (size>1) {
strcpy(bufp++, ")");
--size;
}
}
}
#ifdef HEROES
if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, LOC(f->locale, "hero"), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, LOC(f->locale, "hero"), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
#endif
/* status */
if (u->number && (u->faction == f || telepath_see || isbattle)) {
const char * c = locale_string(f->locale, hp_status(u));
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, report_kampfstatus(u, f->locale), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, report_kampfstatus(u, f->locale), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (c || fval(u, UFL_HUNGER)) {
bufp += strlcpy(bufp, " (", sizeof(buf)-(bufp-buf));
if (c) bufp += strlcpy(bufp, c, sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, " (", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (c) {
rsize = strlcpy(bufp, c, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (fval(u, UFL_HUNGER)) {
if (c) bufp += strlcpy(bufp, ", hungert", sizeof(buf)-(bufp-buf));
else bufp += strlcpy(bufp, "hungert", sizeof(buf)-(bufp-buf));
if (c) rsize = strlcpy(bufp, ", hungert", size);
else rsize = strlcpy(bufp, "hungert", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (size>1) {
strcpy(bufp++, ")");
--size;
}
}
if (getguard(u)) bufp += strlcpy(bufp, ", bewacht die Region", sizeof(buf)-(bufp-buf));
}
if (getguard(u)) {
rsize = strlcpy(bufp, ", bewacht die Region", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (u->faction==f || telepath_see) {
attrib * a = a_find(u->attribs, &at_follow);
if (a) {
unit * uf = (unit*)a->data.v;
if (uf) {
bufp += strlcpy(bufp, ", folgt ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, itoa36(uf->no), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", folgt ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, itoa36(uf->no), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
}
if ((b = usiege(u))!=NULL) {
bufp += strlcpy(bufp, ", belagert ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, buildingname(b), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", belagert ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, buildingname(b), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
dh = 0;
if (u->faction == f || telepath_see) {
for (sk = 0; sk != MAXSKILLS; sk++) {
bufp += spskill(bufp, sizeof(buf)-(bufp-buf), f->locale, u, sk, &dh, 1);
rsize = spskill(bufp, size, f->locale, u, sk, &dh, 1);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
@ -370,17 +498,24 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
int in;
report_item(u, itm, f, &ic, NULL, &in, false);
if (in==0 || ic==NULL) continue;
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (!dh) {
bufp += sprintf(bufp, "%s: ", LOC(f->locale, "nr_inventory"));
bufp += snprintf(bufp, size, "%s: ", LOC(f->locale, "nr_inventory"));
size = sizeof(buf)-(bufp-buf);
dh = 1;
}
if (in == 1) {
bufp += strlcpy(bufp, ic, sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ic, size);
} else {
bufp += sprintf(bufp, "%d %s", in, ic);
rsize = snprintf(bufp, size, "%d %s", in, ic);
}
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
if (show!=u->items) while (show) i_free(i_remove(&show, show));
@ -388,23 +523,29 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
dh = 0;
if (is_mage(u) == true) {
bufp += sprintf(bufp, ". Aura %d/%d", get_spellpoints(u), max_spellpoints(u->region,u));
{
spell_ptr *spt;
int t = effskill(u, SK_MAGIC);
bufp += snprintf(bufp, size, ". Aura %d/%d", get_spellpoints(u), max_spellpoints(u->region,u));
size = sizeof(buf)-(bufp-buf);
for (spt = get_mage(u)->spellptr;spt; spt = spt->next){
for (spt = get_mage(u)->spellptr;spt; spt = spt->next) {
sp = find_spellbyid(spt->spellid);
if (sp->level > t) continue;
if (!dh) {
bufp += sprintf(bufp, ", %s: ", LOC(f->locale, "nr_spells"));
rsize = snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells"));
dh = 1;
} else {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
}
bufp += strlcpy(bufp, spell_name(sp, f->locale), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
}
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, spell_name(sp, f->locale), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
dh = 0;
for (i = 0; i < MAXCOMBATSPELLS; i++){
sp = get_combatspell(u,i);
@ -412,31 +553,46 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
dh = 1;
}
}
if(dh){
if (dh) {
dh = 0;
bufp += sprintf(bufp, ", %s: ", LOC(f->locale, "nr_combatspells"));
bufp += snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_combatspells"));
size = sizeof(buf)-(bufp-buf);
for (i = 0; i < MAXCOMBATSPELLS; i++){
if (!dh){
dh = 1;
} else {
bufp += strlcpy(bufp, ", ", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
sp = get_combatspell(u,i);
if (sp) {
int sl;
bufp += strlcpy(bufp, spell_name(sp, u->faction->locale), sizeof(buf)-(bufp-buf));
if ((sl = get_combatspelllevel(u,i)) > 0) {
bufp += sprintf(bufp, " (%d)", sl);
int sl = get_combatspelllevel(u, i);
rsize = strlcpy(bufp, spell_name(sp, u->faction->locale), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (sl > 0) {
bufp += snprintf(bufp, size, " (%d)", sl);
size = sizeof(buf)-(bufp-buf);
}
} else {
bufp += strlcpy(bufp, LOC(f->locale, "nr_nospells"), sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, LOC(f->locale, "nr_nospells"), size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
}
}
#ifdef LASTORDER
if (!isbattle && u->lastorder) {
bufp += buforder(bufp, sizeof(buf)-(bufp-buf), u->lastorder);
rsize = buforder(bufp, size, u->lastorder);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
#else
if (!isbattle) {
@ -447,10 +603,13 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
while (ord) {
if (is_repeated(ord)) {
if (printed==0) {
bufp += buforder(bufp, sizeof(buf)-(bufp-buf), ord);
rsize = buforder(bufp, size, ord);
} else if (printed==1) {
bufp += strlcpy(bufp, ", ...", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, ", ...", size);
}
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
++printed;
break;
}
@ -464,19 +623,38 @@ bufunit(const faction * f, const unit * u, int indent, int mode)
i = 0;
if (u->display && u->display[0]) {
bufp += strlcpy(bufp, "; ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, u->display, sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, "; ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, u->display, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
i = u->display[strlen(u->display) - 1];
}
if (i != '!' && i != '?' && i != '.')
if (i != '!' && i != '?' && i != '.') {
if (size>1) {
strcpy(bufp++, ".");
--size;
}
}
pzTmp = uprivate(u);
if (u->faction == f && pzTmp) {
bufp += strlcpy(bufp, " (Bem: ", sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, pzTmp, sizeof(buf)-(bufp-buf));
bufp += strlcpy(bufp, ")", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, " (Bem: ", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, pzTmp, size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, ")", size);
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
dh=0;
@ -652,41 +830,83 @@ bufunit_ugroupleader(const faction * f, const unit * u, int indent, int mode)
#endif
size_t
spskill(char * buffer, size_t siz, const struct locale * lang, const struct unit * u, skill_t sk, int *dh, int days)
spskill(char * buffer, size_t size, const struct locale * lang, const struct unit * u, skill_t sk, int *dh, int days)
{
char * bufp = buffer;
int i, effsk;
size_t rsize;
size_t tsize = 0;
if (!u->number) return 0;
if (!has_skill(u, sk)) return 0;
bufp += strlcpy(bufp, ", ", siz);
rsize = strlcpy(bufp, ", ", size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (!*dh) {
bufp += strlcpy(bufp, LOC(lang, "nr_skills"), siz-(bufp-buffer));
bufp += strlcpy(bufp, ": ", sizeof(buf)-(bufp-buf));
rsize = strlcpy(bufp, LOC(lang, "nr_skills"), size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, ": ", size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
*dh = 1;
}
bufp += strlcpy(bufp, skillname(sk, lang), siz-(bufp-buffer));
strcpy(bufp++, " ");
rsize = strlcpy(bufp, skillname(sk, lang), size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, " ", size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if (sk == SK_MAGIC){
if (find_magetype(u) != M_GRAU){
bufp += strlcpy(bufp, LOC(lang, mkname("school", magietypen[find_magetype(u)])), siz-(bufp-buffer));
strcpy(bufp++, " ");
rsize = strlcpy(bufp, LOC(lang, mkname("school", magietypen[find_magetype(u)])), size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
rsize = strlcpy(bufp, " ", size);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
if (sk == SK_STEALTH && fval(u, UFL_STEALTH)) {
i = u_geteffstealth(u);
if(i>=0) {
bufp += sprintf(bufp, "%d/", i);
if (i>=0) {
rsize = slprintf(bufp, size, "%d/", i);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
effsk = effskill(u, sk);
bufp += sprintf(bufp, "%d", effsk);
rsize = slprintf(bufp, size, "%d", effsk);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
if(u->faction->options & Pow(O_SHOWSKCHANGE)) {
skill *skill = get_skill(u, sk);
@ -700,11 +920,15 @@ spskill(char * buffer, size_t siz, const struct locale * lang, const struct unit
oldeff = max(0, oldeff);
diff = effsk - oldeff;
if(diff != 0) {
bufp += sprintf(bufp, " (%s%d)", (diff>0)?"+":"", diff);
if (diff != 0) {
rsize = slprintf(bufp, size, " (%s%d)", (diff>0)?"+":"", diff);
tsize += rsize;
if (rsize>size) rsize = size-1;
size -= rsize;
bufp += rsize;
}
}
return bufp-buffer;
return tsize;
}
void

View File

@ -223,7 +223,6 @@ rds(FILE * F, char **ds)
fprintf(stderr, "Die Datei bricht vorzeitig ab.\n");
abort();
}
assert(s <= buffer + DISPLAYSIZE + 1);
rc(F);
}
@ -231,26 +230,20 @@ rds(FILE * F, char **ds)
while (nextc != '"') {
if (nextc == EOF) {
assert(s <= buffer + DISPLAYSIZE + 1);
*s = 0;
fprintf(stderr, "Die Datei bricht vorzeitig ab.\n");
abort();
}
if (s - buffer < DISPLAYSIZE) {
*s++ = (char)nextc;
if (s - buffer > DISPLAYSIZE) {
assert(s <= buffer + DISPLAYSIZE + 1);
*s = 0;
log_error(("\nDer String %s wurde nicht terminiert.\n", s));
exit(1);
}
rc(F);
}
rc(F);
assert(s <= buffer + DISPLAYSIZE + 1);
*s = 0;
if (ds) {
(*ds) = realloc(*ds, sizeof(char) * (strlen(buffer) + 1));
*ds = realloc(*ds, sizeof(char) * (strlen(buffer) + 1));
strcpy(*ds, buffer);
}
}

View File

@ -8,6 +8,12 @@
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
# define HAVE_STRLCPY
#endif
#if !defined(HAVE_STRLPRINTF)
# define HAVE_STRLPRINTF
# define slprintf snprintf
#endif
#endif