As the PR states, the following command segfaults:

$ sort -t: -k1f,1 -k1 /usr/share/misc/zipcodes

The PR has a rather ugly "workaround".  I believe the change below is
the proper fix.  I think putting the check in the else-clause just
before the assignment just like in the if-clause makes the code easier
to read as well.

ok?


Index: fields.c
===================================================================
RCS file: /cvs/src/usr.bin/sort/fields.c,v
retrieving revision 1.14
diff -u -p -r1.14 fields.c
--- fields.c    27 Oct 2009 23:59:43 -0000      1.14
+++ fields.c    31 Dec 2010 22:24:10 -0000
@@ -164,14 +164,14 @@ enterfield(u_char *tablepos, u_char *end
        for (; start < end; start++)
                if (mask[*start]) {
                        if (*start <= 1) {
-                               if (tablepos+2 >= endkey)
+                               if (tablepos + 2 >= endkey)
                                        return (NULL);
                                *tablepos++ = lweight[1];
                                *tablepos++ = lweight[*start ? 2 : 1];
                        } else {
-                               *tablepos++ = lweight[*start];
-                               if (tablepos == endkey)
+                               if (tablepos + 1 >= endkey)
                                        return (NULL);
+                               *tablepos++ = lweight[*start];
                        }
                }
        *tablepos++ = lweight[0];

Reply via email to