valgrind trap complaint

I think this should fix one of the valgrind complaints.  Traps which
didn't use the trap->vl union field never initialized it, leaving a
bit of random garbage in the malloc'd trap structure.  (And traps
which overwrote existing ones that did use it didn't reinitialize it
so kept stale data around.)  Since those fields weren't in use by
the traps that don't care about them, this didn't provoke any actual
trouble.

Also reformatting....
This commit is contained in:
PatR
2015-11-20 18:31:17 -08:00
parent 9518e74c93
commit d3d332e606
+56 -43
View File
@@ -1,4 +1,4 @@
/* NetHack 3.6 trap.c $NHDT-Date: 1446713644 2015/11/05 08:54:04 $ $NHDT-Branch: master $:$NHDT-Revision: 1.244 $ */ /* NetHack 3.6 trap.c $NHDT-Date: 1448073071 2015/11/21 02:31:11 $ $NHDT-Branch: master $:$NHDT-Revision: 1.247 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */ /* NetHack may be freely redistributed. See license for details. */
@@ -320,6 +320,7 @@ struct trap *
maketrap(x, y, typ) maketrap(x, y, typ)
register int x, y, typ; register int x, y, typ;
{ {
static union vlaunchinfo zero_vl;
register struct trap *ttmp; register struct trap *ttmp;
register struct rm *lev; register struct rm *lev;
boolean oldplace; boolean oldplace;
@@ -334,35 +335,39 @@ register int x, y, typ;
|| (u.utraptype == TT_PIT && typ != PIT || (u.utraptype == TT_PIT && typ != PIT
&& typ != SPIKED_PIT))) && typ != SPIKED_PIT)))
u.utrap = 0; u.utrap = 0;
/* old <tx,ty> remain valid */
} else { } else {
oldplace = FALSE; oldplace = FALSE;
ttmp = newtrap(); ttmp = newtrap();
ttmp->ntrap = 0;
ttmp->tx = x; ttmp->tx = x;
ttmp->ty = y; ttmp->ty = y;
ttmp->launch.x = -1; /* force error if used before set */
ttmp->launch.y = -1;
} }
/* [re-]initialize all fields except ntrap (handled below) and <tx,ty> */
ttmp->vl = zero_vl;
ttmp->launch.x = ttmp->launch.y = -1; /* force error if used before set */
ttmp->dst.dnum = ttmp->dst.dlevel = -1;
ttmp->madeby_u = 0;
ttmp->once = 0;
ttmp->tseen = (typ == HOLE); /* hide non-holes */
ttmp->ttyp = typ; ttmp->ttyp = typ;
switch (typ) { switch (typ) {
case SQKY_BOARD: { case SQKY_BOARD: {
int tavail[12], tpick[12], tcnt = 0, k; int tavail[12], tpick[12], tcnt = 0, k;
struct trap *t; struct trap *t;
for (k = 0; k < 12; ++k) for (k = 0; k < 12; ++k)
tavail[k] = 0; tavail[k] = tpick[k] = 0;
for (t = ftrap; t; t = t->ntrap) for (t = ftrap; t; t = t->ntrap)
if (t->ttyp == SQKY_BOARD && t != ttmp) if (t->ttyp == SQKY_BOARD && t != ttmp)
tavail[t->tnote] = 1; tavail[t->tnote] = 1;
/* now populate tpick[] with the available indices */
/* Now populate tpick with the available indexes */ for (k = 0; k < 12; ++k)
for (k = 0; k < 12; ++k) {
if (tavail[k] == 0) if (tavail[k] == 0)
tpick[tcnt++] = k; tpick[tcnt++] = k;
} /* choose an unused note; if all are in use, pick a random one */
if (tcnt > 0) ttmp->tnote = (short) ((tcnt > 0) ? tpick[rn2(tcnt)] : rn2(12));
ttmp->tnote = (short) tpick[rn2(tcnt)];
else
ttmp->tnote = (short) rn2(12); /* all in use anyway */
break; break;
} }
case STATUE_TRAP: { /* create a "living" statue */ case STATUE_TRAP: { /* create a "living" statue */
@@ -375,8 +380,8 @@ register int x, y, typ;
mptr = &mons[rndmonnum()]; mptr = &mons[rndmonnum()];
} while (--trycount > 0 && is_unicorn(mptr) } while (--trycount > 0 && is_unicorn(mptr)
&& sgn(u.ualign.type) == sgn(mptr->maligntyp)); && sgn(u.ualign.type) == sgn(mptr->maligntyp));
statue = statue = mkcorpstat(STATUE, (struct monst *) 0, mptr, x, y,
mkcorpstat(STATUE, (struct monst *) 0, mptr, x, y, CORPSTAT_NONE); CORPSTAT_NONE);
mtmp = makemon(&mons[statue->corpsenm], 0, 0, MM_NOCOUNTBIRTH); mtmp = makemon(&mons[statue->corpsenm], 0, 0, MM_NOCOUNTBIRTH);
if (!mtmp) if (!mtmp)
break; /* should never happen */ break; /* should never happen */
@@ -425,11 +430,7 @@ register int x, y, typ;
unearth_objs(x, y); unearth_objs(x, y);
break; break;
} }
ttmp->tseen = (ttmp->ttyp == HOLE); /* hide non-holes */
ttmp->once = 0;
ttmp->madeby_u = 0;
ttmp->dst.dnum = -1;
ttmp->dst.dlevel = -1;
if (!oldplace) { if (!oldplace) {
ttmp->ntrap = ftrap; ttmp->ntrap = ftrap;
ftrap = ttmp; ftrap = ttmp;
@@ -523,6 +524,7 @@ boolean td; /* td == TRUE : trap door or hole */
if (!td) if (!td)
Sprintf(msgbuf, "The hole in the %s above you closes up.", Sprintf(msgbuf, "The hole in the %s above you closes up.",
ceiling(u.ux, u.uy)); ceiling(u.ux, u.uy));
schedule_goto(&dtmp, FALSE, TRUE, 0, (char *) 0, schedule_goto(&dtmp, FALSE, TRUE, 0, (char *) 0,
!td ? msgbuf : (char *) 0); !td ? msgbuf : (char *) 0);
} }
@@ -774,6 +776,7 @@ struct obj *objchn, *saddle;
while (objchn) { while (objchn) {
if (objchn->otyp == CORPSE && has_omonst(objchn)) { if (objchn->otyp == CORPSE && has_omonst(objchn)) {
struct monst *mtmp = OMONST(objchn); struct monst *mtmp = OMONST(objchn);
if (mtmp->m_id == steed_mid) { if (mtmp->m_id == steed_mid) {
/* move saddle */ /* move saddle */
xchar x, y; xchar x, y;
@@ -812,8 +815,8 @@ unsigned trflags;
nomul(0); nomul(0);
/* KMH -- You can't escape the Sokoban level traps */ /* KMH -- You can't escape the Sokoban level traps */
if (Sokoban && (ttype == PIT || ttype == SPIKED_PIT || ttype == HOLE if (Sokoban && (ttype == PIT || ttype == SPIKED_PIT
|| ttype == TRAPDOOR)) { || ttype == HOLE || ttype == TRAPDOOR)) {
/* The "air currents" message is still appropriate -- even when /* The "air currents" message is still appropriate -- even when
* the hero isn't flying or levitating -- because it conveys the * the hero isn't flying or levitating -- because it conveys the
* reason why the player cannot escape the trap with a dexterity * reason why the player cannot escape the trap with a dexterity
@@ -867,9 +870,9 @@ unsigned trflags;
otmp->quan = 1L; otmp->quan = 1L;
otmp->owt = weight(otmp); otmp->owt = weight(otmp);
otmp->opoisoned = 0; otmp->opoisoned = 0;
if (u.usteed && !rn2(2) && steedintrap(trap, otmp)) /* nothing */ if (u.usteed && !rn2(2) && steedintrap(trap, otmp)) { /* nothing */
; ;
else if (thitu(8, dmgval(otmp, &youmonst), otmp, "arrow")) { } else if (thitu(8, dmgval(otmp, &youmonst), otmp, "arrow")) {
obfree(otmp, (struct obj *) 0); obfree(otmp, (struct obj *) 0);
} else { } else {
place_object(otmp, u.ux, u.uy); place_object(otmp, u.ux, u.uy);
@@ -896,9 +899,9 @@ unsigned trflags;
if (!rn2(6)) if (!rn2(6))
otmp->opoisoned = 1; otmp->opoisoned = 1;
oldumort = u.umortality; oldumort = u.umortality;
if (u.usteed && !rn2(2) && steedintrap(trap, otmp)) /* nothing */ if (u.usteed && !rn2(2) && steedintrap(trap, otmp)) { /* nothing */
; ;
else if (thitu(7, dmgval(otmp, &youmonst), otmp, "little dart")) { } else if (thitu(7, dmgval(otmp, &youmonst), otmp, "little dart")) {
if (otmp->opoisoned) if (otmp->opoisoned)
poisoned("dart", A_CON, "little dart", poisoned("dart", A_CON, "little dart",
/* if damage triggered life-saving, /* if damage triggered life-saving,
@@ -1097,6 +1100,7 @@ unsigned trflags;
} }
if (!Sokoban) { if (!Sokoban) {
char verbbuf[BUFSZ]; char verbbuf[BUFSZ];
if (u.usteed) { if (u.usteed) {
if ((trflags & RECURSIVETRAP) != 0) if ((trflags & RECURSIVETRAP) != 0)
Sprintf(verbbuf, "and %s fall", Sprintf(verbbuf, "and %s fall",
@@ -1119,10 +1123,12 @@ unsigned trflags;
&& In_quest(&u.uz) && Is_qlocate(&u.uz)) { && In_quest(&u.uz) && Is_qlocate(&u.uz)) {
pline("Fortunately it has a bottom after all..."); pline("Fortunately it has a bottom after all...");
trap->once = 1; trap->once = 1;
} else if (u.umonnum == PM_PIT_VIPER || u.umonnum == PM_PIT_FIEND) } else if (u.umonnum == PM_PIT_VIPER || u.umonnum == PM_PIT_FIEND) {
pline("How pitiful. Isn't that the pits?"); pline("How pitiful. Isn't that the pits?");
}
if (ttype == SPIKED_PIT) { if (ttype == SPIKED_PIT) {
const char *predicament = "on a set of sharp iron spikes"; const char *predicament = "on a set of sharp iron spikes";
if (u.usteed) { if (u.usteed) {
pline("%s %s %s!", pline("%s %s %s!",
upstart(x_monnam(u.usteed, steed_article, "poor", upstart(x_monnam(u.usteed, steed_article, "poor",
@@ -1302,8 +1308,9 @@ unsigned trflags;
Your("body absorbs some of the magical energy!"); Your("body absorbs some of the magical energy!");
u.uen = (u.uenmax += 2); u.uen = (u.uenmax += 2);
break; break;
} else } else {
domagictrap(); domagictrap();
}
(void) steedintrap(trap, (struct obj *) 0); (void) steedintrap(trap, (struct obj *) 0);
break; break;
@@ -1346,6 +1353,7 @@ unsigned trflags;
case POLY_TRAP: { case POLY_TRAP: {
char verbbuf[BUFSZ]; char verbbuf[BUFSZ];
seetrap(trap); seetrap(trap);
if (u.usteed) if (u.usteed)
Sprintf(verbbuf, "lead %s", Sprintf(verbbuf, "lead %s",
@@ -1372,6 +1380,7 @@ unsigned trflags;
case LANDMINE: { case LANDMINE: {
unsigned steed_mid = 0; unsigned steed_mid = 0;
struct obj *saddle = 0; struct obj *saddle = 0;
if ((Levitation || Flying) && !forcetrap) { if ((Levitation || Flying) && !forcetrap) {
if (!already_seen && rn2(3)) if (!already_seen && rn2(3))
break; break;
@@ -1440,7 +1449,7 @@ unsigned trflags;
break; break;
case VIBRATING_SQUARE: case VIBRATING_SQUARE:
seetrap(trap); feeltrap(trap);
/* messages handled elsewhere; the trap symbol is merely to mark the /* messages handled elsewhere; the trap symbol is merely to mark the
* square for future reference */ * square for future reference */
break; break;
@@ -1461,6 +1470,7 @@ boolean noprefix;
*tnnames[12] = { "C note", "D flat", "D note", "E flat", *tnnames[12] = { "C note", "D flat", "D note", "E flat",
"E note", "F note", "F sharp", "G note", "E note", "F note", "F sharp", "G note",
"G sharp", "A note", "B flat", "B note" }; "G sharp", "A note", "B flat", "B note" };
tnbuf[0] = '\0'; tnbuf[0] = '\0';
tn = tnnames[trap->tnote]; tn = tnnames[trap->tnote];
if (!noprefix) if (!noprefix)
@@ -1518,9 +1528,9 @@ struct obj *otmp;
break; break;
case PIT: case PIT:
case SPIKED_PIT: case SPIKED_PIT:
trapkilled = trapkilled = (steed->mhp <= 0
(steed->mhp <= 0 || thitm(0, steed, (struct obj *) 0, || thitm(0, steed, (struct obj *) 0,
rnd((tt == PIT) ? 6 : 10), FALSE)); rnd((tt == PIT) ? 6 : 10), FALSE));
steedhit = TRUE; steedhit = TRUE;
break; break;
case POLY_TRAP: case POLY_TRAP:
@@ -1950,6 +1960,7 @@ long ocount;
success = isclearpath(&cc, distance, dx, dy); success = isclearpath(&cc, distance, dx, dy);
if (ttmp->ttyp == ROLLING_BOULDER_TRAP) { if (ttmp->ttyp == ROLLING_BOULDER_TRAP) {
boolean success_otherway; boolean success_otherway;
bcc.x = x; bcc.x = x;
bcc.y = y; bcc.y = y;
success_otherway = isclearpath(&bcc, distance, -(dx), -(dy)); success_otherway = isclearpath(&bcc, distance, -(dx), -(dy));
@@ -2527,6 +2538,7 @@ register struct monst *mtmp;
break; /* monsters usually don't set it off */ break; /* monsters usually don't set it off */
if (is_flyer(mptr)) { if (is_flyer(mptr)) {
boolean already_seen = trap->tseen; boolean already_seen = trap->tseen;
if (in_sight && !already_seen) { if (in_sight && !already_seen) {
pline("A trigger appears in a pile of soil below %s.", pline("A trigger appears in a pile of soil below %s.",
mon_nam(mtmp)); mon_nam(mtmp));
@@ -2726,7 +2738,7 @@ float_up()
You("float up, only your %s is still stuck.", body_part(LEG)); You("float up, only your %s is still stuck.", body_part(LEG));
} }
#if 0 #if 0
} else if(Is_waterlevel(&u.uz)) { } else if (Is_waterlevel(&u.uz)) {
pline("It feels as though you've lost some weight."); pline("It feels as though you've lost some weight.");
#endif #endif
} else if (u.uinwater) { } else if (u.uinwater) {
@@ -2895,7 +2907,7 @@ long hmask, emask; /* might cancel timeout */
case TRAPDOOR: case TRAPDOOR:
if (!Can_fall_thru(&u.uz) || u.ustuck) if (!Can_fall_thru(&u.uz) || u.ustuck)
break; break;
/* fall into next case */ /*FALLTHRU*/
default: default:
if (!u.utrap) /* not already in the trap */ if (!u.utrap) /* not already in the trap */
dotrap(trap, 0); dotrap(trap, 0);
@@ -3206,7 +3218,8 @@ xchar x, y;
setnotworn(obj); setnotworn(obj);
delobj(obj); delobj(obj);
return TRUE; return TRUE;
} else if (erode_obj(obj, NULL, ERODE_BURN, EF_DESTROY) == ER_DESTROYED) { } else if (erode_obj(obj, (char *) 0, ERODE_BURN, EF_DESTROY)
== ER_DESTROYED) {
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
@@ -3251,7 +3264,7 @@ struct obj *obj;
vismon = victim && (victim != &youmonst) && canseemon(victim); vismon = victim && (victim != &youmonst) && canseemon(victim);
if (obj->greased) { if (obj->greased) {
grease_protect(obj, NULL, victim); grease_protect(obj, (char *) 0, victim);
} else if (obj->oclass == SCROLL_CLASS && obj->otyp != SCR_BLANK_PAPER) { } else if (obj->oclass == SCROLL_CLASS && obj->otyp != SCR_BLANK_PAPER) {
if (obj->otyp != SCR_BLANK_PAPER if (obj->otyp != SCR_BLANK_PAPER
#ifdef MAIL #ifdef MAIL
@@ -3270,7 +3283,7 @@ struct obj *obj;
obj->spe = 0; obj->spe = 0;
obj->dknown = 0; obj->dknown = 0;
} else } else
erode_obj(obj, NULL, ERODE_CORRODE, EF_GREASE | EF_VERBOSE); erode_obj(obj, (char *) 0, ERODE_CORRODE, EF_GREASE | EF_VERBOSE);
} }
/* context for water_damage(), managed by water_damage_chain(); /* context for water_damage(), managed by water_damage_chain();
@@ -3437,7 +3450,7 @@ boolean here;
for (; obj; obj = otmp) { for (; obj; obj = otmp) {
otmp = here ? obj->nexthere : obj->nobj; otmp = here ? obj->nexthere : obj->nobj;
water_damage(obj, NULL, FALSE); water_damage(obj, (char *) 0, FALSE);
} }
/* reset acid context */ /* reset acid context */
@@ -3746,7 +3759,7 @@ boolean bury_it;
place_object(otmp, ttmp->tx, ttmp->ty); place_object(otmp, ttmp->tx, ttmp->ty);
if (bury_it) { if (bury_it) {
/* magical digging first disarms this trap, then will unearth it */ /* magical digging first disarms this trap, then will unearth it */
(void) bury_an_obj(otmp, NULL); (void) bury_an_obj(otmp, (boolean *) 0);
} else { } else {
/* Sell your own traps only... */ /* Sell your own traps only... */
if (ttmp->madeby_u) if (ttmp->madeby_u)
@@ -4064,9 +4077,9 @@ struct trap *ttmp;
You("grab the trapped %s using your bare %s.", mtmp->data->mname, You("grab the trapped %s using your bare %s.", mtmp->data->mname,
makeplural(body_part(HAND))); makeplural(body_part(HAND)));
if (poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM)) if (poly_when_stoned(youmonst.data) && polymon(PM_STONE_GOLEM)) {
display_nhwindow(WIN_MESSAGE, FALSE); display_nhwindow(WIN_MESSAGE, FALSE);
else { } else {
char kbuf[BUFSZ]; char kbuf[BUFSZ];
Sprintf(kbuf, "trying to help %s out of a pit", Sprintf(kbuf, "trying to help %s out of a pit",
@@ -5024,9 +5037,9 @@ lava_effects()
iflags.in_lava_effects--; iflags.in_lava_effects--;
/* s/he died... */ /* s/he died... */
boil_away = boil_away = (u.umonnum == PM_WATER_ELEMENTAL
(u.umonnum == PM_WATER_ELEMENTAL || u.umonnum == PM_STEAM_VORTEX || u.umonnum == PM_STEAM_VORTEX
|| u.umonnum == PM_FOG_CLOUD); || u.umonnum == PM_FOG_CLOUD);
for (;;) { for (;;) {
u.uhp = -1; u.uhp = -1;
/* killer format and name are reconstructed every iteration /* killer format and name are reconstructed every iteration