Skip to content

Commit f25df99

Browse files
committed
fix sign casting and over flows
1 parent 68be1bc commit f25df99

13 files changed

Lines changed: 43 additions & 25 deletions

src/hack.apply.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static void use_camera(struct obj *obj) {
7777
struct monst *mtmp;
7878
(void)obj;
7979
if (!getdir(1)) { /* ask: in what direction? */
80-
flags.move = multi = 0;
80+
flags.move = (unsigned char)(multi = 0); /* MODERN: Cast for bit field assignment */
8181
return;
8282
}
8383
if (u.uswallow) {
@@ -212,7 +212,7 @@ static struct monst *bchit(int ddx, int ddy, int range, char sym) {
212212
break;
213213
}
214214
if (sym)
215-
Tmp_at(bchx, bchy);
215+
Tmp_at((schar)bchx, (schar)bchy); /* MODERN: Safe cast - coordinates bounded by map size */
216216
}
217217
if (sym)
218218
Tmp_at(-1, -1);

src/hack.do.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,19 @@ void goto_level(int newlevel, boolean at_stairs) {
188188

189189
if (at_stairs) {
190190
if (up) {
191-
u.ux = xdnstair;
192-
u.uy = ydnstair;
191+
u.ux = (xchar)xdnstair; /* MODERN: Safe cast - map coords are 0-79, within xchar range */
192+
u.uy = (xchar)ydnstair; /* MODERN: Safe cast - map coords are 0-21, within xchar range */
193193
if (!u.ux) { /* entering a maze from below? */
194-
u.ux = xupstair; /* this will confuse the player! */
195-
u.uy = yupstair;
194+
u.ux = (xchar)xupstair; /* this will confuse the player! */ /* MODERN: Safe cast */
195+
u.uy = (xchar)yupstair; /* MODERN: Safe cast */
196196
}
197197
if (Punished && !Levitation) {
198198
pline("With great effort you climb the stairs.");
199199
placebc(1);
200200
}
201201
} else {
202-
u.ux = xupstair;
203-
u.uy = yupstair;
202+
u.ux = (xchar)xupstair; /* MODERN: Safe cast - map coords within xchar range */
203+
u.uy = (xchar)yupstair; /* MODERN: Safe cast - map coords within xchar range */
204204
if (inv_weight() + 5 > 0 || Punished) {
205205
pline("You fall down the stairs."); /* %% */
206206
losehp(rnd(3), "fall");

src/hack.eat.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include "hack.h"
13+
#include <limits.h> /* MODERN: For SCHAR_MAX to prevent stat overflow */
1314
#include <stdio.h>
1415
char POISONOUS[] = "ADKSVabhks";
1516
extern const char
@@ -95,8 +96,12 @@ int opentin(void) {
9596
} else {
9697
pline("It contains spinach - this makes you feel like Popeye!");
9798
lesshungry(600);
98-
if (u.ustr < 118)
99-
u.ustr += rnd(((u.ustr < 17) ? 19 : 118) - u.ustr);
99+
if (u.ustr < 118) {
100+
int new_str = u.ustr + rnd(((u.ustr < 17) ? 19 : 118) - u.ustr);
101+
/* MODERN: Prevent strength overflow - cap at max schar value */
102+
if (new_str > SCHAR_MAX) new_str = SCHAR_MAX;
103+
u.ustr = (schar)new_str;
104+
}
100105
if (u.ustr > u.ustrmax)
101106
u.ustrmax = u.ustr;
102107
flags.botl = 1;

src/hack.engrave.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ int doengrave(void) {
251251
otmp->spe = -3;
252252
nomovemsg = "You cannot engrave more.";
253253
} else {
254-
otmp->spe -= len / 2;
254+
otmp->spe -= (schar)(len / 2); /* MODERN: Safe cast - engraving length bounded */
255255
nomovemsg = "You finished engraving.";
256256
}
257257
multi = -len;

src/hack.fight.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ int hitmm(struct monst *magr, struct monst *mdef) {
6868
"You have a peculiarly sad feeling for a moment, then it passes.");
6969
monstone(mdef);
7070
hit = 2;
71-
} else if ((mdef->mhp -= d(pa->damn, pa->damd)) < 1) {
72-
magr->mhpmax += 1 + rn2(pd->mlevel + 1);
71+
} else if ((mdef->mhp -= (schar)d(pa->damn, pa->damd)) < 1) { /* MODERN: Safe cast - damage capped by dice */
72+
magr->mhpmax += (schar)(1 + rn2(pd->mlevel + 1)); /* MODERN: Safe cast - level-based increment */
7373
if (magr->mtame && magr->mhpmax > 8 * pa->mlevel) {
7474
if (pa == &li_dog)
7575
magr->data = pa = &dog;
@@ -245,14 +245,19 @@ boolean hmon(struct monst *mon, struct obj *obj,
245245
}
246246
if (tmp < 1)
247247
tmp = 1;
248-
mon->mhp -= tmp;
248+
mon->mhp -= (schar)tmp; /* MODERN: Safe cast - tmp bounded above by weapon damage */
249249
if (mon->mhp < 1) {
250250
killed(mon);
251251
return (FALSE);
252252
}
253253
if (mon->mtame && (!mon->mflee || mon->mfleetim)) {
254254
mon->mflee = 1; /* Rick Richardson */
255-
mon->mfleetim += 10 * rnd(tmp);
255+
int flee_add = 10 * rnd(tmp);
256+
/* MODERN: Cap flee time to 7-bit field max (127) */
257+
if (mon->mfleetim + flee_add > 127)
258+
mon->mfleetim = 127;
259+
else
260+
mon->mfleetim += flee_add;
256261
}
257262

258263
if (!hittxt) {

src/hack.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ typedef struct {
7676

7777
extern void *alloc(unsigned lth);
7878
/* MODERN: CONST-CORRECTNESS: panic message is read-only */
79-
extern void panic(const char *str, ...);
79+
/* MODERN: noreturn attribute tells compiler panic() never returns */
80+
extern void panic(const char *str, ...) __attribute__((noreturn));
8081

8182
/* Critical missing function prototypes */
8283
extern int carrying(int type);

src/hack.main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ int main(int argc, char *argv[]) {
335335

336336
(void)signal(SIGINT, done1);
337337
mklev();
338-
u.ux = xupstair;
339-
u.uy = yupstair;
338+
u.ux = (xchar)xupstair; /* MODERN: Safe cast - map coords are 0-79, within xchar range */
339+
u.uy = (xchar)yupstair; /* MODERN: Safe cast - map coords are 0-21, within xchar range */
340340
(void)inshop();
341341
setsee();
342342
flags.botlx = 1;

src/hack.mklev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int makerooms() {
206206
hix = lowx + dx;
207207
hiy = lowy + dy;
208208

209-
if (maker(lowx, dx, lowy, dy)) {
209+
if (maker((schar)lowx, (schar)dx, (schar)lowy, (schar)dy)) { /* MODERN: Safe cast - room coordinates bounded by map */
210210
if (secret)
211211
return (1);
212212
addrs(lowx - 1, lowy - 1, hix + 1, hiy + 1);

src/hack.mkobj.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,10 @@ int otyp;
8888
if (otmp->otyp == TIN)
8989
otmp->spe = rnd(...);
9090
#endif /* NOT_YET_IMPLEMENTED */
91-
/* fallthrough */
91+
/* FALLTHROUGH */
9292
case GEM_SYM:
9393
otmp->quan = rn2(6) ? 1 : 2;
94+
/* FALLTHROUGH */
9495
case TOOL_SYM:
9596
case CHAIN_SYM:
9697
case BALL_SYM:

src/hack.potion.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ int dodrink(void) {
130130
pline("Wow do you feel strong!");
131131
if (u.ustr >= 118)
132132
break; /* > 118 is impossible */
133-
if (u.ustr > 17)
134-
u.ustr += rnd(118 - u.ustr);
135-
else
133+
if (u.ustr > 17) {
134+
int new_str = u.ustr + rnd(118 - u.ustr);
135+
/* MODERN: Prevent strength overflow - cap at max schar value */
136+
if (new_str > 127) new_str = 127;
137+
u.ustr = (schar)new_str;
138+
} else
136139
u.ustr++;
137140
if (u.ustr > u.ustrmax)
138141
u.ustrmax = u.ustr;

0 commit comments

Comments
 (0)