From 1863a89ae9d52847142b4a205899bd53f97d0fef Mon Sep 17 00:00:00 2001 From: millert Date: Wed, 22 Jan 1997 06:43:51 +0000 Subject: [PATCH] Fix off-by-one differently--add an extra byte to buffer (ick). Don't dump core on empty file. --- usr.bin/sort/files.c | 6 +++--- usr.bin/sort/fsort.c | 8 ++++---- usr.bin/sort/msort.c | 37 ++++++++++++++++++++----------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/usr.bin/sort/files.c b/usr.bin/sort/files.c index f345877e48e..a11a882ccae 100644 --- a/usr.bin/sort/files.c +++ b/usr.bin/sort/files.c @@ -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); } } } diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c index 122608ae646..3537dfb9f5e 100644 --- a/usr.bin/sort/fsort.c +++ b/usr.bin/sort/fsort.c @@ -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; diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c index 09081480e67..5e2fc6556d6 100644 --- a/usr.bin/sort/msort.c +++ b/usr.bin/sort/msort.c @@ -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); - } + } } } -- 2.20.1