- Do not trust strcheck() in Release

- Nobody needs strnzcpy() anymore, use bsdstring.h
This commit is contained in:
Enno Rehling 2007-02-25 18:49:28 +00:00
parent 034753d9b8
commit 426a8a34f2
7 changed files with 55 additions and 58 deletions

View File

@ -227,18 +227,19 @@ setalliance(struct faction * f, alliance * al)
const char * const char *
alliancename(const alliance * al) alliancename(const alliance * al)
{ {
typedef char name[OBJECTIDSIZE + 1]; typedef char name[OBJECTIDSIZE + 1];
static name idbuf[8]; static name idbuf[8];
static int nextbuf = 0; static int nextbuf = 0;
char *buf = idbuf[(++nextbuf) % 8]; char *ibuf = idbuf[(++nextbuf) % 8];
if (al && al->name) { if (al && al->name) {
sprintf(buf, "%s (%s)", strcheck(al->name, NAMESIZE), itoa36(al->id)); snprintf(ibuf, sizeof(name), "%s (%s)", strcheck(al->name, NAMESIZE), itoa36(al->id));
} else { ibuf[sizeof(name)] = 0;
return NULL; } else {
} return NULL;
return buf; }
return ibuf;
} }
void void

View File

@ -69,6 +69,7 @@
#include <util/translation.h> #include <util/translation.h>
#include <util/umlaut.h> #include <util/umlaut.h>
#include <util/xml.h> #include <util/xml.h>
#include <util/bsdstring.h>
/* libxml includes */ /* libxml includes */
#include <libxml/tree.h> #include <libxml/tree.h>
@ -96,14 +97,6 @@ const struct race * new_race[MAXRACES];
boolean sqlpatch = false; boolean sqlpatch = false;
int turn; int turn;
char *
strnzcpy(char * dst, const char *src, size_t len)
{
strncpy(dst, src, len);
dst[len]=0;
return dst;
}
static attrib_type at_creator = { static attrib_type at_creator = {
"creator" "creator"
/* Rest ist NULL; temporäres, nicht alterndes Attribut */ /* Rest ist NULL; temporäres, nicht alterndes Attribut */
@ -1190,7 +1183,7 @@ strcheck (const char *s, size_t maxlen)
assert(maxlen < 16 * 1024); assert(maxlen < 16 * 1024);
log_warning(("[strcheck] String wurde auf %d Zeichen verkürzt:\n%s\n", log_warning(("[strcheck] String wurde auf %d Zeichen verkürzt:\n%s\n",
(int)maxlen, s)); (int)maxlen, s));
strnzcpy(buffer, s, maxlen); strlcpy(buffer, s, maxlen);
return buffer; return buffer;
} }
return s; return s;
@ -1782,24 +1775,24 @@ freestrlist (strlist * s)
boolean lomem = false; boolean lomem = false;
/* - Namen der Strukturen -------------------------------------- */ /* - Namen der Strukturen -------------------------------------- */
typedef char name[OBJECTIDSIZE + 1]; typedef char name[OBJECTIDSIZE+1];
static name idbuf[8]; static name idbuf[8];
static int nextbuf = 0; static int nextbuf = 0;
char * char *
estring(const char *s) estring(const char *s)
{ {
char *buf = idbuf[(++nextbuf) % 8]; char *ibuf = idbuf[(++nextbuf) % 8];
char *r; char *r;
strcpy(buf,s); strlcpy(ibuf, s, sizeof(name));
r = buf; r = ibuf;
while(*buf) { while (*ibuf) {
if(*buf == ' ') { if(*ibuf == ' ') {
*buf = '~'; *ibuf = '~';
} }
buf++; ibuf++;
} }
return r; return r;
} }
@ -1807,17 +1800,17 @@ estring(const char *s)
char * char *
cstring(const char *s) cstring(const char *s)
{ {
char *buf = idbuf[(++nextbuf) % 8]; char *ibuf = idbuf[(++nextbuf) % 8];
char *r; char *r;
strcpy(buf,s); strlcpy(ibuf,s, sizeof(name));
r = buf; r = ibuf;
while(*buf) { while (*ibuf) {
if(*buf == '~') { if (*ibuf == '~') {
*buf = ' '; *ibuf = ' ';
} }
buf++; ibuf++;
} }
return r; return r;
} }
@ -1825,10 +1818,11 @@ cstring(const char *s)
const char * const char *
buildingname (const building * b) buildingname (const building * b)
{ {
char *buf = idbuf[(++nextbuf) % 8]; char *ibuf = idbuf[(++nextbuf) % 8];
sprintf(buf, "%s (%s)", strcheck(b->name, NAMESIZE), itoa36(b->no)); snprintf(ibuf, sizeof(ibuf), "%s (%s)", strcheck(b->name, NAMESIZE), itoa36(b->no));
return buf; ibuf[sizeof(name)] = 0;
return ibuf;
} }
building * building *
@ -1858,7 +1852,8 @@ const char *
unitname(const unit * u) unitname(const unit * u)
{ {
char *ubuf = idbuf[(++nextbuf) % 8]; char *ubuf = idbuf[(++nextbuf) % 8];
sprintf(ubuf, "%s (%s)", strcheck(u->name, NAMESIZE), itoa36(u->no)); snprintf(ubuf, sizeof(name), "%s (%s)", strcheck(u->name, NAMESIZE), itoa36(u->no));
ubuf[sizeof(name)] = 0;
return ubuf; return ubuf;
} }

View File

@ -62,18 +62,19 @@ random_unit_in_faction(const faction *f)
const char * const char *
factionname(const faction * f) factionname(const faction * f)
{ {
typedef char name[OBJECTIDSIZE + 1]; typedef char name[OBJECTIDSIZE+1];
static name idbuf[8]; static name idbuf[8];
static int nextbuf = 0; static int nextbuf = 0;
char *buf = idbuf[(++nextbuf) % 8]; char *ibuf = idbuf[(++nextbuf) % 8];
if (f && f->name) { if (f && f->name) {
sprintf(buf, "%s (%s)", strcheck(f->name, NAMESIZE), itoa36(f->no)); snprintf(ibuf, sizeof(name), "%s (%s)", strcheck(f->name, NAMESIZE), itoa36(f->no));
ibuf[sizeof(name)] = 0;
} else { } else {
sprintf(buf, "Unbekannte Partei (?)"); strcpy(ibuf, "Unbekannte Partei (?)");
} }
return buf; return ibuf;
} }
void * void *

View File

@ -54,6 +54,7 @@
/* util includes */ /* util includes */
#include <util/attrib.h> #include <util/attrib.h>
#include <util/base36.h> #include <util/base36.h>
#include <util/bsdstring.h>
#include <util/event.h> #include <util/event.h>
#include <util/goodies.h> #include <util/goodies.h>
#include <util/resolve.h> #include <util/resolve.h>
@ -533,11 +534,10 @@ unitorders(FILE * F, struct faction * f)
static faction * static faction *
factionorders(void) factionorders(void)
{ {
char b[16]; char fid[16];
char * fid = strnzcpy(b, getstrtoken(), 15); faction * f = NULL;
const char * pass = getstrtoken();
faction *f = NULL;
strlcpy(fid, getstrtoken(), sizeof(fid));
#ifdef FUZZY_BASE36 #ifdef FUZZY_BASE36
int id = atoi36(fid); int id = atoi36(fid);
if (id!=0) f = findfaction(id); if (id!=0) f = findfaction(id);
@ -550,6 +550,7 @@ factionorders(void)
#endif #endif
if (f!=NULL) { if (f!=NULL) {
const char * pass = getstrtoken();
/* Kontrolliere, ob das Passwort richtig eingegeben wurde. Es /* Kontrolliere, ob das Passwort richtig eingegeben wurde. Es
* muß in "Gänsefüßchen" stehen!! */ * muß in "Gänsefüßchen" stehen!! */

View File

@ -205,12 +205,12 @@ destroy_ship(ship * sh)
const char * const char *
shipname(const ship * sh) shipname(const ship * sh)
{ {
typedef char name[OBJECTIDSIZE + 1]; typedef char name[OBJECTIDSIZE + 1];
static name idbuf[8]; static name idbuf[8];
static int nextbuf = 0; static int nextbuf = 0;
char *buf = idbuf[(++nextbuf) % 8]; char *ibuf = idbuf[(++nextbuf) % 8];
sprintf(buf, "%s (%s)", strcheck(sh->name, NAMESIZE), itoa36(sh->no)); sprintf(ibuf, "%s (%s)", strcheck(sh->name, NAMESIZE), itoa36(sh->no));
return buf; return ibuf;
} }
int int

View File

@ -252,7 +252,6 @@ extern char * strdup(const char *s);
#ifndef INLINE_FUNCTION #ifndef INLINE_FUNCTION
# define INLINE_FUNCTION # define INLINE_FUNCTION
#endif #endif
/* this function must be implemented in a .o file */
extern char * strnzcpy(char * dst, const char *src, size_t len);
#endif #endif

View File

@ -18,14 +18,14 @@ function process(orders)
-- run the turn: -- run the turn:
plan_monsters()
process_orders()
-- create new monsters: -- create new monsters:
spawn_dragons() spawn_dragons()
spawn_undead() spawn_undead()
-- (no more braineaters) spawn_braineaters(0.25) -- (no more braineaters) spawn_braineaters(0.25)
plan_monsters()
process_orders()
-- post-turn updates: -- post-turn updates:
update_guards() update_guards()
update_scores() update_scores()