]> granicus.if.org Git - nethack/commitdiff
fix github issue #110 - sortloot segfault
authorPatR <rankin@nethack.org>
Thu, 21 Jun 2018 19:09:12 +0000 (12:09 -0700)
committerPatR <rankin@nethack.org>
Thu, 21 Jun 2018 19:09:12 +0000 (12:09 -0700)
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
src/invent.c

index 1f222f8497204ba96f321dfb6cb6fcb091aa65d9..fc7a7218eed714e8dada79e9275dba14882c01e0 100644 (file)
@@ -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
index c2d4861a8cc5d1883f6c471c1c34650a1bd54a8c..9ea9746989e40f12477d082ab3b2a155b6f87694 100644 (file)
@@ -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 */