From 990b39edec3a3720961915f34de9e6d03899cd3f Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Sun, 24 Feb 2008 22:15:10 +0100 Subject: [PATCH] Check subscript of rpt[] Make nws_vrb unsigned to simplify that. --- include/news.h | 2 +- src/lib/commands/head.c | 2 ++ src/lib/commands/news.c | 6 ++++-- src/lib/global/nsc.c | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/include/news.h b/include/news.h index 5985d26a..3a2a9b4b 100644 --- a/include/news.h +++ b/include/news.h @@ -50,7 +50,7 @@ struct nwsstr { short nws_uid; /* end of part matching struct empobj */ natid nws_ano; /* "actor" country # */ - signed char nws_vrb; /* action (verb) */ + unsigned char nws_vrb; /* action (verb) */ natid nws_vno; /* "victim" country # */ signed char nws_ntm; /* number of times */ time_t nws_when; /* time of action */ diff --git a/src/lib/commands/head.c b/src/lib/commands/head.c index e562b22e..ed506de6 100644 --- a/src/lib/commands/head.c +++ b/src/lib/commands/head.c @@ -84,6 +84,8 @@ head(void) snxtitem_all(&nstr, EF_NEWS); maxcnum = 0; while (nxtitem(&nstr, &news)) { + if (!news.nws_vrb || CANT_HAPPEN(news.nws_vrb > N_MAX_VERB)) + continue; news_age = now - news.nws_when; if (news_age > news_per) continue; diff --git a/src/lib/commands/news.c b/src/lib/commands/news.c index d6f019d8..8e3566b5 100644 --- a/src/lib/commands/news.c +++ b/src/lib/commands/news.c @@ -85,6 +85,8 @@ news(void) head(); pr("\nThe details of Empire news since %s", ctime(&then)); while (nxtitem(&nstr, &nws)) { + if (!nws.nws_vrb || CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB)) + continue; if (nws.nws_when < then) continue; if (opt_HIDDEN) { @@ -102,6 +104,8 @@ news(void) pr("\n\t === %s ===\n", page_headings[page].name); snxtitem_rewind(&nstr); while (nxtitem(&nstr, &nws)) { + if (CANT_HAPPEN(nws.nws_vrb > N_MAX_VERB)) + continue; if (rpt[(int)nws.nws_vrb].r_newspage != page) continue; if (nws.nws_when < then) @@ -191,8 +195,6 @@ preport(struct nwsstr *np) strcpy(cp, cname(np->nws_ano)); cp += strlen(cp); *cp++ = ' '; - if (np->nws_vrb < 1 || np->nws_vrb > N_MAX_VERB) - np->nws_vrb = 0; sprintf(cp, rpt[(int)np->nws_vrb].r_newstory[random() % NUM_RPTS], cname(np->nws_vno)); cp += strlen(cp); diff --git a/src/lib/global/nsc.c b/src/lib/global/nsc.c index b463e049..2aaa08ab 100644 --- a/src/lib/global/nsc.c +++ b/src/lib/global/nsc.c @@ -427,7 +427,7 @@ struct castr loan_ca[] = { struct castr news_ca[] = { /* no need for uid as long as it's not referenced from other tables */ {NSC_NATID, 0, 0, fldoff(nwsstr, nws_ano), "actor", EF_NATION}, - {NSC_CHAR, 0, 0, fldoff(nwsstr, nws_vrb), "action", EF_NEWS_CHR}, + {NSC_UCHAR, 0, 0, fldoff(nwsstr, nws_vrb), "action", EF_NEWS_CHR}, {NSC_NATID, 0, 0, fldoff(nwsstr, nws_vno), "victim", EF_NATION}, {NSC_CHAR, 0, 0, fldoff(nwsstr, nws_ntm), "times", EF_BAD}, {NSC_TIME, 0, 0, fldoff(nwsstr, nws_when), "time", EF_BAD},