diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2007-03-07 18:11:04 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2007-03-07 18:11:04 +0000 |
commit | a79c2c43999d2c5c5ca6f84d3f0a4f036d0e21b4 (patch) | |
tree | 983cc9fb59f184b8790f8b1ce709aaf1b2f5fc2a /usr.bin | |
parent | 577a9f29e7e7f9565e573a6e48ac3061cd90b62f (diff) |
Use separate buffers in fsort.c and msort.c. Fixes a problem with
buffer corruptions for some very large files. Also fixes some
TRECHEADER vs. RECHEADER mismatches. OK deraadt@ otto@ ray@
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/sort/fsort.c | 21 | ||||
-rw-r--r-- | usr.bin/sort/fsort.h | 6 | ||||
-rw-r--r-- | usr.bin/sort/msort.c | 44 | ||||
-rw-r--r-- | usr.bin/sort/sort.c | 9 |
4 files changed, 35 insertions, 45 deletions
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c index 5a0e98c4c40..7f471c1d438 100644 --- a/usr.bin/sort/fsort.c +++ b/usr.bin/sort/fsort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fsort.c,v 1.15 2006/10/29 18:40:34 millert Exp $ */ +/* $OpenBSD: fsort.c,v 1.16 2007/03/07 18:11:03 millert Exp $ */ /*- * Copyright (c) 1993 @@ -36,7 +36,7 @@ #if 0 static char sccsid[] = "@(#)fsort.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: fsort.c,v 1.15 2006/10/29 18:40:34 millert Exp $"; +static char rcsid[] = "$OpenBSD: fsort.c,v 1.16 2007/03/07 18:11:03 millert Exp $"; #endif #endif /* not lint */ @@ -53,10 +53,9 @@ static char rcsid[] = "$OpenBSD: fsort.c,v 1.15 2006/10/29 18:40:34 millert Exp #include <stdlib.h> #include <string.h> -u_char *buffer = NULL, *bufend = NULL, *linebuf = NULL; -size_t bufsize = BUFSIZE, linebuf_size = MAXLLEN; +u_char *linebuf; +size_t linebuf_size = MAXLLEN; struct tempfile fstack[MAXFCT]; -extern char toutpath[]; #define FSORTMAX 4 int PANIC = FSORTMAX; @@ -64,8 +63,9 @@ void fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp, struct field *ftbl) { - u_char *weights, **keypos, *tmpbuf; - static u_char **keylist; + u_char *weights, **keypos, *bufend, *tmpbuf; + static u_char *buffer, **keylist; + static size_t bufsize; int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0; int c, nelem; long sizes[NBINS+1]; @@ -85,10 +85,13 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp, tfield[0].weights = ascii; tfield[0].icol.num = 1; weights = ftbl[0].weights; - if (keylist == NULL) { - if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) + if (buffer == NULL) { + bufsize = BUFSIZE; + if ((buffer = malloc(bufsize)) == NULL || + (keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) err(2, NULL); } + bufend = buffer + bufsize - 1; if (binno >= 0) { tfiles.top = infiles.top + nfiles; get = getnext; diff --git a/usr.bin/sort/fsort.h b/usr.bin/sort/fsort.h index a2691563780..ddcb68a037d 100644 --- a/usr.bin/sort/fsort.h +++ b/usr.bin/sort/fsort.h @@ -1,4 +1,4 @@ -/* $OpenBSD: fsort.h,v 1.7 2006/10/29 18:40:34 millert Exp $ */ +/* $OpenBSD: fsort.h,v 1.8 2007/03/07 18:11:03 millert Exp $ */ /*- * Copyright (c) 1993 @@ -42,8 +42,8 @@ #define MAXFCT 1000 #define MAXLLEN ((1 << min(POW-4, 16)) - 14) -extern u_char *buffer, *bufend, *linebuf; -extern size_t bufsize, linebuf_size; +extern u_char *linebuf; +extern size_t linebuf_size; /* temp files in the stack have a file descriptor, a largest bin (maxb) * which becomes the last non-empty bin (lastb) when the actual largest diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c index 311b1e4e894..3c88cfbb1fb 100644 --- a/usr.bin/sort/msort.c +++ b/usr.bin/sort/msort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: msort.c,v 1.17 2006/10/29 18:40:34 millert Exp $ */ +/* $OpenBSD: msort.c,v 1.18 2007/03/07 18:11:03 millert Exp $ */ /*- * Copyright (c) 1993 @@ -36,7 +36,7 @@ #if 0 static char sccsid[] = "@(#)msort.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: msort.c,v 1.17 2006/10/29 18:40:34 millert Exp $"; +static char rcsid[] = "$OpenBSD: msort.c,v 1.18 2007/03/07 18:11:03 millert Exp $"; #endif #endif /* not lint */ @@ -56,13 +56,10 @@ typedef struct mfile { short flno; RECHEADER rec[1]; } MFILE; -typedef struct tmfile { - u_char *end; - short flno; - TRECHEADER rec[1]; -} TMFILE; -u_char *wts, *wts1 = 0; -struct mfile *cfilebuf; +static u_char *wts, *wts1; +static struct mfile *cfilebuf; +static void *buffer; +static size_t bufsize; static int cmp(RECHEADER *, RECHEADER *); static int insert(struct mfile **, struct mfile **, int, int); @@ -80,21 +77,17 @@ fmerge(int binno, union f_handle files, int nfiles, wts = ftbl->weights; if (!UNIQUE && SINGL_FLD && (ftbl->flags & F)) wts1 = (ftbl->flags & R) ? Rascii : ascii; - if (!cfilebuf) { - cfilebuf = malloc(MAXLLEN + sizeof(TMFILE)); + if (cfilebuf == NULL) { + cfilebuf = malloc(MAXLLEN + sizeof(MFILE)); if (cfilebuf == NULL) errx(2, "cannot allocate memory"); } - i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE)); + i = min(16, nfiles) * LALIGN(MAXLLEN + sizeof(MFILE)); if (i > bufsize) { - do { - bufsize *= 2; - } while (i > bufsize); - buffer = realloc(buffer, bufsize); - if (!buffer) - errx(2, "cannot allocate memory"); - bufend = buffer + bufsize - 1; + bufsize = i; + if ((buffer = realloc(buffer, bufsize)) == NULL) + err(2, NULL); } if (binno >= 0) @@ -148,7 +141,7 @@ merge(int infl0, int nfiles, for (i = j = 0; i < nfiles; i++) { cfile = (MFILE *) (buffer + - i * LALIGN(MAXLLEN + sizeof(TMFILE))); + i * LALIGN(MAXLLEN + sizeof(MFILE))); cfile->flno = j + infl0; cfile->end = cfile->rec->data + MAXLLEN; for (c = 1; c == 1;) { @@ -253,18 +246,15 @@ order(union f_handle infile, int c; RECHEADER *crec, *prec, *trec; - if (bufsize < 2 * ALIGN(MAXLLEN + sizeof(TRECHEADER))) { - do { - bufsize *= 2; - } while (bufsize < 2 * ALIGN(MAXLLEN + sizeof(TRECHEADER))); + if (bufsize < 2 * ALIGN(MAXLLEN + sizeof(RECHEADER))) { + bufsize = 2 * ALIGN(MAXLLEN + sizeof(RECHEADER)); if ((buffer = realloc(buffer, bufsize)) == NULL) err(2, NULL); - bufend = buffer + bufsize - 1; } crec = (RECHEADER *) buffer; - crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(TRECHEADER)); + crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(RECHEADER)); prec = (RECHEADER *) crec_end; - prec_end = ((char *)prec) + ALIGN(MAXLLEN + sizeof(TRECHEADER)); + prec_end = ((char *)prec) + ALIGN(MAXLLEN + sizeof(RECHEADER)); wts = ftbl->weights; if (SINGL_FLD && (ftbl->flags & F)) wts1 = ftbl->flags & R ? Rascii : ascii; diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c index 90ba230554a..cbe5e5f8579 100644 --- a/usr.bin/sort/sort.c +++ b/usr.bin/sort/sort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sort.c,v 1.31 2006/10/29 18:40:34 millert Exp $ */ +/* $OpenBSD: sort.c,v 1.32 2007/03/07 18:11:03 millert Exp $ */ /*- * Copyright (c) 1993 @@ -42,7 +42,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)sort.c 8.1 (Berkeley) 6/6/93"; #else -static char rcsid[] = "$OpenBSD: sort.c,v 1.31 2006/10/29 18:40:34 millert Exp $"; +static char rcsid[] = "$OpenBSD: sort.c,v 1.32 2007/03/07 18:11:03 millert Exp $"; #endif #endif /* not lint */ @@ -254,10 +254,7 @@ main(int argc, char *argv[]) else get = makekey; - if ((buffer = malloc(bufsize)) == NULL) - err(2, NULL); - bufend = buffer + bufsize - 1; - if (!SINGL_FLD) { + if (!SINGL_FLD) { if ((linebuf = malloc(linebuf_size)) == NULL) err(2, NULL); } |