From 27a4d7fd1a4e5374bf6533ad782798fa7b03eed8 Mon Sep 17 00:00:00 2001 From: k21971 Date: Wed, 12 Mar 2025 05:49:51 +0000 Subject: [PATCH] Fix: prevent segfault for non-existent trap. Caught this while fuzzing. Fix comes from NetHack 3.7 commit 5615974, slightly modified plus some other improvements in this block of code. --- doc/evilhack-changelog.md | 1 + src/do.c | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/doc/evilhack-changelog.md b/doc/evilhack-changelog.md index c1d384aab..711941a57 100644 --- a/doc/evilhack-changelog.md +++ b/doc/evilhack-changelog.md @@ -4383,4 +4383,5 @@ The following changes to date are: 'output' used (windows build, crafting traps) - Debug hints files tweak - Fix: get rid of unneeded dispose_of_orig_obj() call +- Fix: prevent segfault for non-existent trap diff --git a/src/do.c b/src/do.c index 0cbb19fb6..63f9b6156 100644 --- a/src/do.c +++ b/src/do.c @@ -2306,12 +2306,16 @@ struct obj *corpse; struct trap *ttmp; ttmp = t_at(mtmp->mx, mtmp->my); - ttmp->tseen = TRUE; + if (ttmp) + ttmp->tseen = TRUE; pline("%s claws its way out of the ground!", - Amonnam(mtmp)); + canspotmon(mtmp) ? Amonnam(mtmp) : Something); newsym(mtmp->mx, mtmp->my); - } else if (distu(mtmp->mx, mtmp->my) < 5 * 5) - You_hear("scratching noises."); + } else if (distu(mtmp->mx, mtmp->my) < 5 * 5) { + if (!Deaf) + You_hear("scratching noises."); + } + fill_pit(mtmp->mx, mtmp->my); break; } /*FALLTHRU*/