From 8c2bd75ce4c3be7428f761d271a3c191af965af8 Mon Sep 17 00:00:00 2001 From: PatR Date: Thu, 21 Jun 2018 12:09:12 -0700 Subject: [PATCH] fix github issue #110 - sortloot segfault Fixes #110 NetHack dumped core while qsort was executing for sortloot. Fix a logic error introduced by adding filtering capability to sortloot() which could result in a sparsely populated array instead of having the number of elements be less than the list size. I don't know why this didn't show up sooner. --- doc/fixes36.2 | 1 + src/invent.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/fixes36.2 b/doc/fixes36.2 index 1f222f849..fc7a7218e 100644 --- a/doc/fixes36.2 +++ b/doc/fixes36.2 @@ -53,6 +53,7 @@ Fixes to Post-3.6.1 Problems that Were Exposed Via git Repository fix access violation when --debug:xxxx has no other args after it Setting the inverse attribute for gold had the space before "$:" getting highlighted along with the gold field +sortloot segfaulted when filtering a subset of items (seen with 'A' command) Platform- and/or Interface-Specific Fixes diff --git a/src/invent.c b/src/invent.c index c2d4861a8..9ea974698 100644 --- a/src/invent.c +++ b/src/invent.c @@ -462,7 +462,7 @@ boolean FDECL((*filterfunc), (OBJ_P)); augment_filter = (mode & SORTLOOT_PETRIFY) ? TRUE : FALSE; mode &= ~SORTLOOT_PETRIFY; /* remove flag, leaving mode */ /* populate aliarray[0..n-1] */ - for (i = 0, o = *olist; o; ++i, o = by_nexthere ? o->nexthere : o->nobj) { + for (i = 0, o = *olist; o; o = by_nexthere ? o->nexthere : o->nobj) { if (filterfunc && !(*filterfunc)(o) /* caller may be asking us to override filterfunc (in order to do a cockatrice corpse touch check during pickup even @@ -473,6 +473,7 @@ boolean FDECL((*filterfunc), (OBJ_P)); sliarray[i].obj = o, sliarray[i].indx = (int) i; sliarray[i].str = (char *) 0; sliarray[i].orderclass = sliarray[i].subclass = sliarray[i].disco = 0; + ++i; } n = i; /* add a terminator so that we don't have to pass 'n' back to caller */ -- 2.40.0