Fix off-by-one differently--add an extra byte to buffer (ick).
authormillert <millert@openbsd.org>
Wed, 22 Jan 1997 06:43:51 +0000 (06:43 +0000)
committermillert <millert@openbsd.org>
Wed, 22 Jan 1997 06:43:51 +0000 (06:43 +0000)
Don't dump core on empty file.

usr.bin/sort/files.c
usr.bin/sort/fsort.c
usr.bin/sort/msort.c

index f345877..a11a882 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: files.c,v 1.1 1997/01/20 19:39:50 millert Exp $       */
+/*     $OpenBSD: files.c,v 1.2 1997/01/22 06:43:51 millert Exp $       */
 
 /*-
  * Copyright (c) 1993
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)files.c    8.1 (Berkeley) 6/6/93";
 #else
-static char rcsid[] = "$OpenBSD: files.c,v 1.1 1997/01/20 19:39:50 millert Exp $";
+static char rcsid[] = "$OpenBSD: files.c,v 1.2 1997/01/22 06:43:51 millert Exp $";
 #endif
 #endif /* not lint */
 
@@ -184,7 +184,7 @@ makeline(flno, filelist, nfiles, buffer, bufend, dummy2)
                        if(flno >= 0) fstack[flno].fp = 0;
                } else {
                        buffer->data[100] = '\000';
-                       warnx("line too long:ignoring %s...", buffer->data);
+                       warnx("line too long: ignoring %s...", buffer->data);
                }
        }
 }
index 122608a..3537dfb 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: fsort.c,v 1.2 1997/01/22 05:49:19 millert Exp $       */
+/*     $OpenBSD: fsort.c,v 1.3 1997/01/22 06:43:52 millert Exp $       */
 
 /*-
  * Copyright (c) 1993
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)fsort.c    8.1 (Berkeley) 6/6/93";
 #else
-static char rcsid[] = "$OpenBSD: fsort.c,v 1.2 1997/01/22 05:49:19 millert Exp $";
+static char rcsid[] = "$OpenBSD: fsort.c,v 1.3 1997/01/22 06:43:52 millert Exp $";
 #endif
 #endif /* not lint */
 
@@ -92,12 +92,12 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
        tfield[0].icol.num = 1;
        weights = ftbl[0].weights;
        if (!buffer) {
-               buffer = malloc(BUFSIZE);
+               buffer = malloc(BUFSIZE + 1);
                keylist = malloc(MAXNUM * sizeof(u_char *));
                if (!SINGL_FLD)
                        linebuf = malloc(MAXLLEN);
        }
-       bufend = buffer + BUFSIZE - 1;
+       bufend = buffer + BUFSIZE;
        if (binno >= 0) {
                tfiles.top = infiles.top + nfiles;
                get = getnext;
index 0908148..5e2fc65 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: msort.c,v 1.1 1997/01/20 19:39:53 millert Exp $       */
+/*     $OpenBSD: msort.c,v 1.2 1997/01/22 06:43:53 millert Exp $       */
 
 /*-
  * Copyright (c) 1993
@@ -40,7 +40,7 @@
 #if 0
 static char sccsid[] = "@(#)msort.c    8.1 (Berkeley) 6/6/93";
 #else
-static char rcsid[] = "$OpenBSD: msort.c,v 1.1 1997/01/20 19:39:53 millert Exp $";
+static char rcsid[] = "$OpenBSD: msort.c,v 1.2 1997/01/22 06:43:53 millert Exp $";
 #endif
 #endif /* not lint */
 
@@ -151,6 +151,7 @@ merge(infl0, nfiles, get, outfp, put, ftbl)
        int c, i, j;
        union f_handle dummy = {0};
        struct mfile *flist[16], *cfile;
+
        for (i = j = 0; i < nfiles; i++) {
                cfile = (MFILE *) (buffer +
                    i * LALIGN(MAXLLEN + sizeof(TMFILE)));
@@ -170,22 +171,24 @@ merge(infl0, nfiles, get, outfp, put, ftbl)
                }
                j++;
        }
-       cfile = cfilebuf;
-       cfile->flno = flist[0]->flno;
-       cfile->end = cfile->rec->data + MAXLLEN;
-       while (nfiles) {
-               for (c = 1; c == 1;) {
-                       if (EOF == (c = get(cfile->flno, dummy, nfiles,
-                          cfile->rec, cfile->end, ftbl))) {
-                               put(flist[0]->rec, outfp);
-                               memmove(flist, flist + 1,
-                                   sizeof(MFILE *) * (--nfiles));
-                               cfile->flno = flist[0]->flno;
-                               break;
+       if (nfiles > 0) {
+               cfile = cfilebuf;
+               cfile->flno = flist[0]->flno;
+               cfile->end = cfile->rec->data + MAXLLEN;
+               while (nfiles) {
+                       for (c = 1; c == 1;) {
+                               if (EOF == (c = get(cfile->flno, dummy, nfiles,
+                                  cfile->rec, cfile->end, ftbl))) {
+                                       put(flist[0]->rec, outfp);
+                                       memmove(flist, flist + 1,
+                                           sizeof(MFILE *) * (--nfiles));
+                                       cfile->flno = flist[0]->flno;
+                                       break;
+                               }
+                               if (!(c = insert(flist, &cfile, nfiles, DELETE)))
+                                       put(cfile->rec, outfp);
                        }
-                       if (!(c = insert(flist, &cfile, nfiles, DELETE)))
-                               put(cfile->rec, outfp);
-               }
+               }       
        }       
 }