diff options
author | Christian Weisgerber <naddy@cvs.openbsd.org> | 2006-10-28 21:14:30 +0000 |
---|---|---|
committer | Christian Weisgerber <naddy@cvs.openbsd.org> | 2006-10-28 21:14:30 +0000 |
commit | 0f9bae4aff082f19e21ef9f53f414a8fddc7796f (patch) | |
tree | 1902024353ed580eba791df90176cc54423f77eb /usr.bin/sort | |
parent | 15046173c609145fe01171cd421fc7a14f588a62 (diff) |
revert previous change; causes segfault when called from
pkg_mklocatedb/locate.mklocatedb
Diffstat (limited to 'usr.bin/sort')
-rw-r--r-- | usr.bin/sort/fsort.c | 61 | ||||
-rw-r--r-- | usr.bin/sort/fsort.h | 4 | ||||
-rw-r--r-- | usr.bin/sort/msort.c | 28 | ||||
-rw-r--r-- | usr.bin/sort/sort.c | 11 |
4 files changed, 65 insertions, 39 deletions
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c index 5f9485c20cf..6502a44a70b 100644 --- a/usr.bin/sort/fsort.c +++ b/usr.bin/sort/fsort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fsort.c,v 1.13 2006/10/18 23:30:43 millert Exp $ */ +/* $OpenBSD: fsort.c,v 1.14 2006/10/28 21:14:29 naddy 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.13 2006/10/18 23:30:43 millert Exp $"; +static char rcsid[] = "$OpenBSD: fsort.c,v 1.14 2006/10/28 21:14:29 naddy Exp $"; #endif #endif /* not lint */ @@ -53,8 +53,8 @@ static char rcsid[] = "$OpenBSD: fsort.c,v 1.13 2006/10/18 23:30:43 millert Exp #include <stdlib.h> #include <string.h> -u_char *buffer = NULL, *linebuf = NULL; -size_t bufsize = BUFSIZE, linebuf_size = MAXLLEN; +u_char **keylist = 0, *buffer = 0, *linebuf = 0; +size_t bufsize, linebuf_size; struct tempfile fstack[MAXFCT]; extern char toutpath[]; #define FSORTMAX 4 @@ -64,8 +64,8 @@ void fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp, struct field *ftbl) { - u_char *bufend, *weights, **keypos, *tmpbuf; - static u_char **keylist; + u_char *bufend, **keypos, *tmpbuf; + u_char *weights; int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0; int c, nelem; long sizes[NBINS+1]; @@ -85,11 +85,18 @@ 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; - bufend = buffer + bufsize; - if (keylist == NULL) { - if ((keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) - err(2, NULL); + if (!buffer) { + bufsize = BUFSIZE; + if ((buffer = malloc(bufsize + 1)) == NULL || + (keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) + errx(2, "cannot allocate memory"); + if (!SINGL_FLD) { + linebuf_size = MAXLLEN; + if ((linebuf = malloc(linebuf_size)) == NULL) + errx(2, "cannot allocate memory"); + } } + bufend = buffer + bufsize; if (binno >= 0) { tfiles.top = infiles.top + nfiles; get = getnext; @@ -133,12 +140,9 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp, */ if (c == BUFFEND && nelem == 0) { bufsize *= 2; - tmpbuf = realloc(buffer, bufsize); - if (!tmpbuf) + buffer = realloc(buffer, bufsize); + if (!buffer) err(2, "failed to realloc buffer"); - crec = (RECHEADER *) - (tmpbuf + ((u_char *)crec - buffer)); - buffer = tmpbuf; bufend = buffer + bufsize; continue; } @@ -153,6 +157,27 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp, mfct++; /* reduce number of open files */ if (mfct == 16 ||(c == EOF && ntfiles)) { + /* + * Only copy extra incomplete + * crec data if there is any. + */ + int nodata = (bufend + >= (u_char *)crec + && bufend <= crec->data); + size_t sz = 0; + + if (!nodata) { + sz = bufend + - crec->data; + tmpbuf = malloc(sz); + if (tmpbuf == NULL) + errx(2, "cannot" + " allocate" + " memory"); + memmove(tmpbuf, + crec->data, sz); + } + fstack[tfiles.top + ntfiles].fp = ftmp(); fmerge(0, mstart, mfct, geteasy, @@ -160,6 +185,12 @@ fsort(int binno, int depth, union f_handle infiles, int nfiles, FILE *outfp, putrec, ftbl); ntfiles++; mfct = 0; + + if (!nodata) { + memmove(crec->data, + tmpbuf, sz); + free(tmpbuf); + } } } else { fstack[tfiles.top + ntfiles].fp= ftmp(); diff --git a/usr.bin/sort/fsort.h b/usr.bin/sort/fsort.h index d4801952f3b..d07e4010ae6 100644 --- a/usr.bin/sort/fsort.h +++ b/usr.bin/sort/fsort.h @@ -1,4 +1,4 @@ -/* $OpenBSD: fsort.h,v 1.5 2006/10/18 23:30:43 millert Exp $ */ +/* $OpenBSD: fsort.h,v 1.6 2006/10/28 21:14:29 naddy Exp $ */ /*- * Copyright (c) 1993 @@ -42,7 +42,7 @@ #define MAXFCT 1000 #define MAXLLEN ((1 << min(POW-4, 16)) - 14) -extern u_char *buffer, *linebuf; +extern u_char **keylist, *buffer, *linebuf; extern size_t bufsize, linebuf_size; /* temp files in the stack have a file descriptor, a largest bin (maxb) diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c index 9f8bd1d1276..e33298c0a21 100644 --- a/usr.bin/sort/msort.c +++ b/usr.bin/sort/msort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: msort.c,v 1.15 2006/10/18 23:30:43 millert Exp $ */ +/* $OpenBSD: msort.c,v 1.16 2006/10/28 21:14:29 naddy 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.15 2006/10/18 23:30:43 millert Exp $"; +static char rcsid[] = "$OpenBSD: msort.c,v 1.16 2006/10/28 21:14:29 naddy Exp $"; #endif #endif /* not lint */ @@ -87,13 +87,14 @@ fmerge(int binno, union f_handle files, int nfiles, } i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE)); - if (i > bufsize) { - do { - bufsize *= 2; - } while (i > bufsize); - buffer = realloc(buffer, bufsize); + if (!buffer || i > BUFSIZE) { + buffer = buffer ? realloc(buffer, i) : malloc(i); if (!buffer) errx(2, "cannot allocate memory"); + if (!SINGL_FLD) { + if ((linebuf = malloc(MAXLLEN)) == NULL) + errx(2, "cannot allocate memory"); + } } if (binno >= 0) @@ -252,13 +253,14 @@ 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 ((buffer = realloc(buffer, bufsize)) == NULL) - err(2, NULL); + if (!SINGL_FLD) { + if ((linebuf = malloc(MAXLLEN)) == NULL) + errx(2, "cannot allocate memory"); } + buffer = malloc(2 * ALIGN((MAXLLEN + sizeof(TRECHEADER)))); + if (buffer == NULL) + errx(2, "cannot allocate memory"); + crec = (RECHEADER *) buffer; crec_end = ((char *)crec) + ALIGN(MAXLLEN + sizeof(TRECHEADER)); prec = (RECHEADER *) crec_end; diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c index 430f3854d2e..f5da9ad174e 100644 --- a/usr.bin/sort/sort.c +++ b/usr.bin/sort/sort.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sort.c,v 1.29 2006/10/18 23:30:43 millert Exp $ */ +/* $OpenBSD: sort.c,v 1.30 2006/10/28 21:14:29 naddy 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.29 2006/10/18 23:30:43 millert Exp $"; +static char rcsid[] = "$OpenBSD: sort.c,v 1.30 2006/10/28 21:14:29 naddy Exp $"; #endif #endif /* not lint */ @@ -254,13 +254,6 @@ main(int argc, char *argv[]) else get = makekey; - if ((buffer = malloc(bufsize)) == NULL) - err(2, NULL); - if (!SINGL_FLD) { - if ((linebuf = malloc(linebuf_size)) == NULL) - err(2, NULL); - } - if (cflag) { order(filelist, get, fldtab); /* NOT REACHED */ |