Skip to content

Commit ef7ec27

Browse files
committed
fixed casting issues
1 parent b1a4fa3 commit ef7ec27

13 files changed

Lines changed: 150 additions & 137 deletions

src/hack.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void seeoff(int mode) /* 1 to redo @, 0 to leave them */
6868
if (u.udispl && mode) {
6969
u.udispl = 0;
7070
/* Original 1984: levl[u.udisx][u.udisy].scrsym = news0(u.udisx,u.udisy); */
71-
levl[(unsigned char)u.udisx][(unsigned char)u.udisy].scrsym =
71+
levl[(int)u.udisx][(int)u.udisy].scrsym =
7272
news0(u.udisx, u.udisy); /* MODERN: safe array indexing */
7373
}
7474
#ifndef QUEST
@@ -131,8 +131,8 @@ void domove(void) {
131131
}
132132

133133
/* Original 1984: ust = &levl[u.ux][u.uy]; */
134-
ust = &levl[(unsigned char)u.ux]
135-
[(unsigned char)u.uy]; /* MODERN: safe array indexing */
134+
ust = &levl[(int)u.ux]
135+
[(int)u.uy]; /* MODERN: safe array indexing */
136136
oldx = u.ux;
137137
oldy = u.uy;
138138
if (!u.uswallow && (trap = t_at(u.ux + u.dx, u.uy + u.dy)) && trap->tseen)
@@ -176,8 +176,8 @@ void domove(void) {
176176
return;
177177
}
178178
/* Original 1984: tmpr = &levl[u.ux+u.dx][u.uy+u.dy]; */
179-
tmpr = &levl[(unsigned char)(u.ux + u.dx)]
180-
[(unsigned char)(u.uy + u.dy)]; /* MODERN: safe array indexing */
179+
tmpr = &levl[(int)(u.ux + u.dx)]
180+
[(int)(u.uy + u.dy)]; /* MODERN: safe array indexing */
181181
if (IS_ROCK(tmpr->typ) ||
182182
(u.dx && u.dy && (tmpr->typ == DOOR || ust->typ == DOOR))) {
183183
flags.move = 0;
@@ -191,8 +191,8 @@ void domove(void) {
191191
/* Original 1984: if(isok(rx,ry) && !IS_ROCK(levl[rx][ry].typ) &&
192192
* (levl[rx][ry].typ != DOOR || !(u.dx && u.dy)) && */
193193
if (isok(rx, ry) &&
194-
!IS_ROCK(levl[(unsigned char)rx][(unsigned char)ry].typ) &&
195-
(levl[(unsigned char)rx][(unsigned char)ry].typ != DOOR ||
194+
!IS_ROCK(levl[(int)rx][(int)ry].typ) &&
195+
(levl[(int)rx][(int)ry].typ != DOOR ||
196196
!(u.dx && u.dy)) && /* MODERN: safe array indexing */
197197
!sobj_at(ENORMOUS_ROCK, rx, ry)) {
198198
if (m_at(rx, ry)) {
@@ -215,8 +215,8 @@ void domove(void) {
215215
}
216216
/* Original 1984: if(levl[rx][ry].typ == POOL) { levl[rx][ry].typ = ROOM;
217217
*/
218-
if (levl[(unsigned char)rx][(unsigned char)ry].typ == POOL) {
219-
levl[(unsigned char)rx][(unsigned char)ry].typ =
218+
if (levl[(int)rx][(int)ry].typ == POOL) {
219+
levl[(int)rx][(int)ry].typ =
220220
ROOM; /* MODERN: safe array indexing */
221221
mnewsym(rx, ry);
222222
prl(rx, ry);
@@ -250,8 +250,8 @@ void domove(void) {
250250
IS_ROCK(levl[u.ux+u.dx][u.uy].typ))){ */
251251
(!u.dx || !u.dy ||
252252
(IS_ROCK(
253-
levl[(unsigned char)u.ux][(unsigned char)(u.uy + u.dy)].typ) &&
254-
IS_ROCK(levl[(unsigned char)(u.ux + u.dx)][(unsigned char)u.uy]
253+
levl[(int)u.ux][(int)(u.uy + u.dy)].typ) &&
254+
IS_ROCK(levl[(int)(u.ux + u.dx)][(int)u.uy]
255255
.typ)))) { /* MODERN: safe array indexing */
256256
pline("However, you can squeeze yourself into a small opening.");
257257
break;
@@ -262,8 +262,8 @@ void domove(void) {
262262
/* Original 1984: if(u.dx && u.dy && IS_ROCK(levl[u.ux][u.uy+u.dy].typ) &&
263263
* IS_ROCK(levl[u.ux+u.dx][u.uy].typ) && */
264264
if (u.dx && u.dy &&
265-
IS_ROCK(levl[(unsigned char)u.ux][(unsigned char)(u.uy + u.dy)].typ) &&
266-
IS_ROCK(levl[(unsigned char)(u.ux + u.dx)][(unsigned char)u.uy]
265+
IS_ROCK(levl[(int)u.ux][(int)(u.uy + u.dy)].typ) &&
266+
IS_ROCK(levl[(int)(u.ux + u.dx)][(int)u.uy]
267267
.typ) && /* MODERN: safe array indexing */
268268
invent &&
269269
inv_weight() + 40 > 0) {
@@ -542,7 +542,7 @@ void lookaround(void) {
542542
/* Original 1984: if(flags.run == 1 && levl[u.ux][u.uy].typ == ROOM) return;
543543
*/
544544
if (flags.run == 1 &&
545-
levl[(unsigned char)u.ux][(unsigned char)u.uy].typ == ROOM)
545+
levl[(int)u.ux][(int)u.uy].typ == ROOM)
546546
return; /* MODERN: safe array indexing */
547547
#ifdef QUEST
548548
if (u.ux0 == u.ux + u.dx && u.uy0 == u.uy + u.dy)
@@ -763,7 +763,7 @@ void setsee(void) {
763763
return;
764764
}
765765
/* Original 1984: if(!levl[u.ux][u.uy].lit) { */
766-
if (!levl[(unsigned char)u.ux][(unsigned char)u.uy]
766+
if (!levl[(int)u.ux][(int)u.uy]
767767
.lit) { /* Bounds already checked above */
768768
seelx = u.ux - 1;
769769
seehx = u.ux + 1;
@@ -773,25 +773,25 @@ void setsee(void) {
773773
/* Original 1984: for(seelx = u.ux; levl[seelx-1][u.uy].lit; seelx--); etc.
774774
*/
775775
/* MODERN: Add bounds checking to prevent array access beyond levl bounds */
776-
for (seelx = u.ux; seelx > 1 && levl[seelx - 1][(unsigned char)u.uy].lit;
776+
for (seelx = u.ux; seelx > 1 && levl[seelx - 1][(int)u.uy].lit;
777777
seelx--)
778778
;
779779
for (seehx = u.ux;
780-
seehx < COLNO - 2 && levl[seehx + 1][(unsigned char)u.uy].lit; seehx++)
780+
seehx < COLNO - 2 && levl[seehx + 1][(int)u.uy].lit; seehx++)
781781
;
782-
for (seely = u.uy; seely > 1 && levl[(unsigned char)u.ux][seely - 1].lit;
782+
for (seely = u.uy; seely > 1 && levl[(int)u.ux][seely - 1].lit;
783783
seely--)
784784
;
785785
for (seehy = u.uy;
786-
seehy < ROWNO - 2 && levl[(unsigned char)u.ux][seehy + 1].lit; seehy++)
786+
seehy < ROWNO - 2 && levl[(int)u.ux][seehy + 1].lit; seehy++)
787787
;
788788
}
789789
for (y = seely; y <= seehy; y++)
790790
for (x = seelx; x <= seehx; x++) {
791791
prl(x, y);
792792
}
793793
/* Original 1984: if(!levl[u.ux][u.uy].lit) seehx = 0; */
794-
if (!levl[(unsigned char)u.ux][(unsigned char)u.uy].lit)
794+
if (!levl[(int)u.ux][(int)u.uy].lit)
795795
seehx = 0; /* Bounds already validated above */
796796
else {
797797
if (seely == u.uy)

src/hack.do.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void goto_level(int newlevel, boolean at_stairs) {
172172
glo(dlevel);
173173

174174
/* Original 1984: if(!level_exists[dlevel]) */
175-
if (!level_exists[(unsigned char)dlevel]) /* MODERN: safe array indexing */
175+
if (dlevel >= 0 && dlevel <= MAXLEVEL && !level_exists[(int)dlevel]) /* MODERN: safe array indexing */
176176
mklev();
177177
else {
178178
extern int hackpid;
@@ -225,7 +225,7 @@ void goto_level(int newlevel, boolean at_stairs) {
225225
u.uy = rn2(ROWNO);
226226
/* Original 1984: } while(levl[u.ux][u.uy].typ != ROOM ||
227227
* m_at(u.ux,u.uy)); */
228-
} while (levl[(unsigned char)u.ux][(unsigned char)u.uy].typ != ROOM ||
228+
} while (levl[(int)u.ux][(int)u.uy].typ != ROOM ||
229229
m_at(u.ux, u.uy)); /* MODERN: safe array indexing */
230230
if (Punished) {
231231
if (uwep != uball && !up /* %% */ && rn2(5)) {

src/hack.makemon.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct monst *makemon(struct permonst *ptr, int x, int y) {
4848
if (index(fut_geno, ptr->mlet))
4949
return ((struct monst *)0);
5050
} else {
51-
ct = CMNUM - strlen(fut_geno);
51+
ct = CMNUM - (int)strlen(fut_geno); /* MODERN: cast strlen to int */
5252
if (index(fut_geno, 'm'))
5353
ct++; /* make only 1 minotaur */
5454
if (index(fut_geno, '@'))
@@ -85,11 +85,11 @@ struct monst *makemon(struct permonst *ptr, int x, int y) {
8585
if (ptr->mlet == 'D')
8686
mtmp->mhpmax = mtmp->mhp = 80;
8787
else if (!ptr->mlevel)
88-
mtmp->mhpmax = mtmp->mhp = rnd(4);
88+
mtmp->mhpmax = mtmp->mhp = (schar)rnd(4); /* MODERN: cast to schar */
8989
else
90-
mtmp->mhpmax = mtmp->mhp = d(ptr->mlevel, 8);
91-
mtmp->mx = x;
92-
mtmp->my = y;
90+
mtmp->mhpmax = mtmp->mhp = (schar)d(ptr->mlevel, 8); /* MODERN: cast to schar */
91+
mtmp->mx = (xchar)x; /* MODERN: cast to xchar */
92+
mtmp->my = (xchar)y; /* MODERN: cast to xchar */
9393
mtmp->mcansee = 1;
9494
if (ptr->mlet == 'M') {
9595
mtmp->mimic = 1;
@@ -133,10 +133,10 @@ struct monst *makemon(struct permonst *ptr, int x, int y) {
133133
if (ptr->mlet == 'O' || ptr->mlet == 'k') {
134134
coord mm;
135135
int cnt = rnd(10);
136-
mm.x = x;
137-
mm.y = y;
136+
mm.x = (unsigned char)x; /* MODERN: cast to coord field type */
137+
mm.y = (unsigned char)y; /* MODERN: cast to coord field type */
138138
while (cnt--) {
139-
mm = enexto(mm.x, mm.y);
139+
mm = enexto((xchar)mm.x, (xchar)mm.y); /* MODERN: cast to xchar for function */
140140
(void)makemon(ptr, mm.x, mm.y);
141141
}
142142
}
@@ -152,38 +152,38 @@ coord enexto(xchar xx, xchar yy) {
152152
tfoo = foo;
153153
range = 1;
154154
do { /* full kludge action. */
155-
for (x = xx - range; x <= xx + range; x++)
155+
for (x = (xchar)(xx - range); x <= xx + range; x++) /* MODERN: cast to xchar */
156156
if (goodpos(x, yy - range)) {
157-
tfoo->x = x;
158-
tfoo++->y = yy - range;
157+
tfoo->x = (unsigned char)x; /* MODERN: cast to coord field type */
158+
tfoo++->y = (unsigned char)(yy - range); /* MODERN: cast to coord field type */
159159
if (tfoo == &foo[15])
160160
goto foofull;
161161
}
162-
for (x = xx - range; x <= xx + range; x++)
162+
for (x = (xchar)(xx - range); x <= xx + range; x++) /* MODERN: cast to xchar */
163163
if (goodpos(x, yy + range)) {
164-
tfoo->x = x;
165-
tfoo++->y = yy + range;
164+
tfoo->x = (unsigned char)x; /* MODERN: cast to coord field type */
165+
tfoo++->y = (unsigned char)(yy + range); /* MODERN: cast to coord field type */
166166
if (tfoo == &foo[15])
167167
goto foofull;
168168
}
169-
for (y = yy + 1 - range; y < yy + range; y++)
169+
for (y = (xchar)(yy + 1 - range); y < yy + range; y++) /* MODERN: cast to xchar */
170170
if (goodpos(xx - range, y)) {
171-
tfoo->x = xx - range;
172-
tfoo++->y = y;
171+
tfoo->x = (unsigned char)(xx - range); /* MODERN: cast to coord field type */
172+
tfoo++->y = (unsigned char)y; /* MODERN: cast to coord field type */
173173
if (tfoo == &foo[15])
174174
goto foofull;
175175
}
176-
for (y = yy + 1 - range; y < yy + range; y++)
176+
for (y = (xchar)(yy + 1 - range); y < yy + range; y++) /* MODERN: cast to xchar */
177177
if (goodpos(xx + range, y)) {
178-
tfoo->x = xx + range;
179-
tfoo++->y = y;
178+
tfoo->x = (unsigned char)(xx + range); /* MODERN: cast to coord field type */
179+
tfoo++->y = (unsigned char)y; /* MODERN: cast to coord field type */
180180
if (tfoo == &foo[15])
181181
goto foofull;
182182
}
183183
range++;
184184
} while (tfoo == foo);
185185
foofull:
186-
return (foo[rn2(tfoo - foo)]);
186+
return (foo[rn2((int)(tfoo - foo))]); /* MODERN: cast pointer diff to int */
187187
}
188188

189189
int goodpos(int x, int y) /* used only in mnexto and rloc */
@@ -205,12 +205,12 @@ void rloc(struct monst *mtmp) {
205205
tx = rn1(COLNO - 3, 2);
206206
ty = rn2(ROWNO);
207207
} while (!goodpos(tx, ty));
208-
mtmp->mx = tx;
209-
mtmp->my = ty;
208+
mtmp->mx = (xchar)tx; /* MODERN: cast to xchar */
209+
mtmp->my = (xchar)ty; /* MODERN: cast to xchar */
210210
if (u.ustuck == mtmp) {
211211
if (u.uswallow) {
212-
u.ux = tx;
213-
u.uy = ty;
212+
u.ux = (xchar)tx; /* MODERN: cast to xchar */
213+
u.uy = (xchar)ty; /* MODERN: cast to xchar */
214214
docrt();
215215
} else
216216
u.ustuck = 0;

src/hack.mkobj.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ struct obj *mkobj_at(let, x, y)
1818
int let, x, y;
1919
{
2020
struct obj *otmp = mkobj(let);
21-
otmp->ox = x;
22-
otmp->oy = y;
21+
otmp->ox = (xchar)x; /* MODERN: explicit cast to xchar for coordinate */
22+
otmp->oy = (xchar)y; /* MODERN: explicit cast to xchar for coordinate */
2323
otmp->nobj = fobj;
2424
fobj = otmp;
2525
return (otmp);
@@ -29,8 +29,8 @@ struct obj *mksobj_at(otyp, x, y)
2929
int otyp, x, y;
3030
{
3131
struct obj *otmp = mksobj(otyp);
32-
otmp->ox = x;
33-
otmp->oy = y;
32+
otmp->ox = (xchar)x; /* MODERN: explicit cast to xchar for coordinate */
33+
otmp->oy = (xchar)y; /* MODERN: explicit cast to xchar for coordinate */
3434
otmp->nobj = fobj;
3535
fobj = otmp;
3636
return (otmp);
@@ -58,7 +58,7 @@ int otyp;
5858
impossible("mksobj called with invalid otyp %d", otyp, 0);
5959
otyp = STRANGE_OBJECT; /* Safe fallback */
6060
}
61-
unsigned char let =
61+
unsigned char let = (unsigned char)
6262
objects[otyp].oc_olet; /* MODERN: Use unsigned char to prevent negative
6363
values in switch */
6464

@@ -67,17 +67,17 @@ int otyp;
6767
otmp->age = moves;
6868
otmp->o_id = flags.ident++;
6969
otmp->quan = 1;
70-
otmp->olet = let;
71-
otmp->otyp = otyp;
70+
otmp->olet = (char)let; /* MODERN: explicit cast from unsigned char to char */
71+
otmp->otyp = (uchar)otyp; /* MODERN: explicit cast to uchar */
7272
otmp->dknown = index("/=!?*", let) ? 0 : 1;
7373
switch (let) {
7474
case WEAPON_SYM:
75-
otmp->quan = (otmp->otyp <= ROCK) ? rn1(6, 6) : 1;
75+
otmp->quan = (uchar)((otmp->otyp <= ROCK) ? rn1(6, 6) : 1); /* MODERN: cast to uchar */
7676
if (!rn2(11))
77-
otmp->spe = rnd(3);
77+
otmp->spe = (schar)rnd(3); /* MODERN: cast to schar */
7878
else if (!rn2(10)) {
7979
otmp->cursed = 1;
80-
otmp->spe = -rnd(3);
80+
otmp->spe = (schar)(-rnd(3)); /* MODERN: cast to schar */
8181
}
8282
break;
8383
case FOOD_SYM:
@@ -103,25 +103,25 @@ int otyp;
103103
if (!rn2(8))
104104
otmp->cursed = 1;
105105
if (!rn2(10))
106-
otmp->spe = rnd(3);
106+
otmp->spe = (schar)rnd(3); /* MODERN: cast to schar */
107107
else if (!rn2(9)) {
108-
otmp->spe = -rnd(3);
108+
otmp->spe = (schar)(-rnd(3)); /* MODERN: cast to schar */
109109
otmp->cursed = 1;
110110
}
111111
break;
112112
case WAND_SYM:
113113
if (otmp->otyp == WAN_WISHING)
114114
otmp->spe = 3;
115115
else
116-
otmp->spe = rn1(5, (objects[otmp->otyp].bits & NODIR) ? 11 : 4);
116+
otmp->spe = (schar)rn1(5, (objects[otmp->otyp].bits & NODIR) ? 11 : 4); /* MODERN: cast to schar */
117117
break;
118118
case RING_SYM:
119119
if (objects[otmp->otyp].bits & SPEC) {
120120
if (!rn2(3)) {
121121
otmp->cursed = 1;
122-
otmp->spe = -rnd(2);
122+
otmp->spe = (schar)(-rnd(2)); /* MODERN: cast to schar */
123123
} else
124-
otmp->spe = rnd(2);
124+
otmp->spe = (schar)rnd(2); /* MODERN: cast to schar */
125125
} else if (otmp->otyp == RIN_TELEPORTATION ||
126126
otmp->otyp == RIN_AGGRAVATE_MONSTER ||
127127
otmp->otyp == RIN_HUNGER || !rn2(9))
@@ -132,7 +132,7 @@ int otyp;
132132
(let >= 32 && let < 127) ? let : '?', let,
133133
otyp); /* MODERN: Better error reporting */
134134
}
135-
otmp->owt = weight(otmp);
135+
otmp->owt = (uchar)weight(otmp); /* MODERN: cast weight() result to uchar */
136136
return (otmp);
137137
}
138138

@@ -165,8 +165,8 @@ int x, y;
165165
else {
166166
gold = newgold();
167167
gold->ngold = fgold;
168-
gold->gx = x;
169-
gold->gy = y;
168+
gold->gx = (xchar)x; /* MODERN: explicit cast to xchar for coordinate */
169+
gold->gy = (xchar)y; /* MODERN: explicit cast to xchar for coordinate */
170170
gold->amount = amount;
171171
fgold = gold;
172172
/* do sth with display? */

0 commit comments

Comments
 (0)