From a9d118f3abd30763a9a2ff7afc60fc638f3fe41f Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 4 Jan 2004 18:14:20 +0000 Subject: [PATCH] =?UTF-8?q?scramble()=20neu=20geschrieben=20Noch=20einen?= =?UTF-8?q?=20Sicherheitscheck=20in=20die=20NMR-Routine=20=C3=96fter=20mal?= =?UTF-8?q?=20log=5Ferror=20statt=20printf=20benutzen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/gamecode/economy.c | 69 +++++------------------------------ src/common/gamecode/laws.c | 2 +- src/common/gamecode/report.c | 42 ++++++--------------- src/common/kernel/message.c | 6 +-- src/common/kernel/resources.c | 4 +- src/common/util/util.vcproj | 12 +++--- 6 files changed, 32 insertions(+), 103 deletions(-) diff --git a/src/common/gamecode/economy.c b/src/common/gamecode/economy.c index 45d6e06d2..3d542db32 100644 --- a/src/common/gamecode/economy.c +++ b/src/common/gamecode/economy.c @@ -120,65 +120,18 @@ income(const unit * u) return 20 * u->number; } -/* ------------------------------------------------------------- */ - -/* ------------------------------------------------------------- */ - -static struct scramble { - int index; - int rnd; -} * vec; - -static int -scramblecmp(const void *p1, const void *p2) -{ - return ((struct scramble *)p1)->rnd - ((struct scramble *)p2)->rnd; -} - static void scramble(void *data, int n, size_t width) { - int i; - static int vecsize = 0; - void *buffer = NULL, *temp = NULL; - - if (n > vecsize) { - vecsize = n; - vec = (struct scramble *) realloc(vec, vecsize * sizeof(struct scramble)); - } - for (i = 0; i != n; i++) { - vec[i].rnd = rand(); - vec[i].index = i; - } - qsort(vec, n, sizeof(struct scramble), scramblecmp); - for (i = 0; i != n; i++) { - /* in vec[k].index steht, wohin der block k soll */ - /* src soll nach target geschoben werden. dafür wird target gemerkt, - * src verschoben, und target zum neuen src. ende, wenn k wieder i ist */ - if (vec[i].index!=i) { - char * src = ((char*)data)+width*i; - int k = i; - int dest = vec[k].index; - - if (temp==NULL) { - temp = malloc(width); - } - buffer = temp; - do { - char * target = ((char*)data)+width*dest; - memmove(buffer, target, width); - memmove(target, src, width); - k = dest; /* wo das gerettete target hin soll */ - dest = vec[dest].index; - vec[k].index = k; /* dest ist an der richtigen stelle */ - /* swap buffer and src. misuse target as intermediate var. */ - target = buffer; - buffer = src; - src = target; - } while (vec[i].index!=i); - } - } - if (buffer!=NULL) free(temp); + int j; + char temp[64]; + assert(width<=sizeof(temp)); + for (j=0;j!=n;++j) { + int k = rand() % n; + memcpy(temp, (char*)data+j*width, width); + memcpy((char*)data+j*width, (char*)data+k*width, width); + memcpy((char*)data+k*width, temp, width); + } } #if 0 #define MAX 6 @@ -3395,10 +3348,6 @@ produce(void) assert(rpeasants(r) >= 0); } - if (vec) { - free(vec); - vec = 0; - } } void diff --git a/src/common/gamecode/laws.c b/src/common/gamecode/laws.c index b78c75861..1520667f4 100644 --- a/src/common/gamecode/laws.c +++ b/src/common/gamecode/laws.c @@ -3699,7 +3699,7 @@ count_migrants (const faction * f) u = u->nextF; } if (f->num_migrants != n) - puts("FEHLER: Anzahl Migranten falsch"); + log_error(("Anzahl Migranten für (%s) ist falsch: %n statt %n.\n", factionid(f), f->num_migrants, n)); #endif return f->num_migrants; } diff --git a/src/common/gamecode/report.c b/src/common/gamecode/report.c index 2fc861a83..82d777a4a 100644 --- a/src/common/gamecode/report.c +++ b/src/common/gamecode/report.c @@ -2400,7 +2400,7 @@ openbatch(void) if (f->email) { sprintf(buf, "%s/mailit", reportpath()); if ((BAT = fopen(buf, "w")) == NULL) - puts("* Fehler: mailit konnte nicht geöffnet werden!"); + log_error(("mailit konnte nicht geöffnet werden!\n")); else fprintf(BAT, "#!/bin/sh\n" @@ -3666,7 +3666,7 @@ report_summary(summary * s, summary * o, boolean full) fprintf(F, "\n\n"); - for (i = 0; i != NMRTimeout()+1; ++i) { + for (i = 0; i <= NMRTimeout(); ++i) { nmrs[i] = 0; } @@ -3674,12 +3674,18 @@ report_summary(summary * s, summary * o, boolean full) if (f->age <= 1 && turn - f->lastorders == 1) { newplayers++; } else if (f->no != MONSTER_FACTION) { - nmrs[min(NMRTimeout(),turn-f->lastorders)]++; + int nmr = turn-f->lastorders; + if (nmr<0 || nmr>NMRTimeout()) { + log_error(("faction %s has %d NMRS\n", factionid(f), nmr)); + nmr = max(0, nmr); + nmr = min(nmr, NMRTimeout()); + } + nmrs[nmr]++; } } - for (i = 0; i != NMRTimeout()+1; ++i) { - if(i == NMRTimeout()) { + for (i = 0; i <= NMRTimeout(); ++i) { + if (i == NMRTimeout()) { fprintf(F, "+ NMRs:\t\t %d\n", nmrs[i]); } else { fprintf(F, "%d %s:\t\t %d\n", i, @@ -3723,10 +3729,6 @@ report_summary(summary * s, summary * o, boolean full) fclose(F); if (full) { -#ifdef PLAYER_CSV - FILE *F; - region * r; -#endif printf("Schreibe Liste der Adressen (adressen)...\n"); writeadresses(); writenewssubscriptions(); @@ -3735,28 +3737,6 @@ report_summary(summary * s, summary * o, boolean full) printf("writing date & turn\n"); writeturn(); -#ifdef PLAYER_CSV - { - strcpy(zText, "%s/players", basepath()); - F = cfopen(zText, "w"); - } - if (!F) return; - printf("Schreibe Spielerliste (players)...\n"); - r = findregion(0, 0); - fputs("id;name;email;info;age;x;y;nmr;score;race;magic;units;people;money", F); - if (r) { - fputs("id;name;email;info;age;x;y;nmr;score;race;magic;units;people;money", F); - for (f=factions;f;f=f->next) { - fprintf(F, "%s;\"%s\";\"%s\";\"%s\";\"%s\";%d;%d;%d;%d;%d;" - "\"%s\";\"%s\";" - "%d;%d;%d\n", - factionid(f), f->name, f->email, f->banner, f->passw, f->age, region_x(r, f), region_y(r, f), turn-f->lastorders, f->score, - f->race->name[0], neue_gebiete[f->magiegebiet], - f->no_units, f->number, f->money); - } - } - fclose(F); -#endif writemonument(); } free(nmrs); diff --git a/src/common/kernel/message.c b/src/common/kernel/message.c index 1d69473b7..7af6fe39f 100644 --- a/src/common/kernel/message.c +++ b/src/common/kernel/message.c @@ -360,7 +360,7 @@ msg_error(const struct unit * u, const char * cmd, const char * name, const char assert(cmd!=u->thisorder || !"only use entries from u->orders - memory corruption imminent."); if (!mtype) { - fprintf(stderr, "trying to create message of unknown type \"%s\"\n", name); + log_error(("trying to create message of unknown type \"%s\"\n", name)); return NULL; } @@ -395,7 +395,7 @@ msg_message(const char * name, const char* sig, ...) memset(args, 0, sizeof(args)); if (!mtype) { - fprintf(stderr, "trying to create message of unknown type \"%s\"\n", name); + log_error(("trying to create message of unknown type \"%s\"\n", name)); return NULL; } @@ -435,7 +435,7 @@ new_message(struct faction * receiver, const char* sig, ...) mtype = mt_find(buffer); if (!mtype) { - fprintf(stderr, "trying to create message of unknown type \"%s\"\n", buffer); + log_error(("trying to create message of unknown type \"%s\"\n", buffer)); return NULL; } diff --git a/src/common/kernel/resources.c b/src/common/kernel/resources.c index a7796e529..04b84a75b 100644 --- a/src/common/kernel/resources.c +++ b/src/common/kernel/resources.c @@ -63,8 +63,8 @@ terraform_resources(region * r) res->type = tdata->rawmaterials[i].type; res->amount = (int)(res->base * (1+(res->level-res->startlevel)*(res->divisor/100.0))); #ifdef RESOURCE_FACTOR - res->amount = res->amount * RESOURCE_FACTOR; - if(res->amount == 0) res->amount = 1; + res->amount = (int)(res->amount * RESOURCE_FACTOR); + if (res->amount == 0) res->amount = 1; #endif assert(res->amount>0); res->type->terraform(res, r); diff --git a/src/common/util/util.vcproj b/src/common/util/util.vcproj index dbf402dee..3923a911a 100644 --- a/src/common/util/util.vcproj +++ b/src/common/util/util.vcproj @@ -221,9 +221,6 @@ - - @@ -267,6 +264,12 @@ + + + + @@ -294,9 +297,6 @@ - -