fix bugs caused by implicit assumption that 'length' and

'offset' members of struct recheader/trecheader are shorts - they are size_t
now
this makes sort pass all tests in TEST/stests again after my last change

other misc cosmetic changes
This commit is contained in:
jdolecek 2000-10-17 15:22:57 +00:00
parent fe7f0860c0
commit c477768e0b
3 changed files with 29 additions and 23 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: append.c,v 1.5 2000/10/16 21:37:03 jdolecek Exp $ */
/* $NetBSD: append.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $ */
/*-
* Copyright (c) 1993
@ -39,7 +39,7 @@
#include "sort.h"
#ifndef lint
__RCSID("$NetBSD: append.c,v 1.5 2000/10/16 21:37:03 jdolecek Exp $");
__RCSID("$NetBSD: append.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $");
__SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -94,11 +94,15 @@ append(keylist, nelem, depth, fp, put, ftbl)
prec = (const RECHEADER *) (*ppos - depth);
if (UNIQUE)
put(prec, fp);
for (cpos = keylist+1; cpos < lastkey; cpos++) {
for (cpos = &keylist[1]; cpos < lastkey; cpos++) {
crec = (const RECHEADER *) (*cpos - depth);
if (crec->length == prec->length) {
pend = (const u_char *) &prec->offset + prec->length;
cend = (const u_char *) &crec->offset + crec->length;
/*
* Set pend & cend so that trailing '\0' and
* record separator is ignored.
*/
pend = (const u_char *) &prec->data + prec->length - 2;
cend = (const u_char *) &crec->data + crec->length - 2;
for (start = *cpos; cend >= start; cend--) {
if (wts[*cend] != wts[*pend])
break;
@ -129,8 +133,12 @@ append(keylist, nelem, depth, fp, put, ftbl)
for (cpos = keylist+1; cpos < lastkey; cpos++) {
crec = (const RECHEADER *) (*cpos - depth);
if (crec->offset == prec->offset) {
pend = (const u_char *) &prec->offset + prec->offset;
cend = (const u_char *) &crec->offset + crec->offset;
/*
* Set pend & cend so that trailing '\0' and
* record separator is ignored.
*/
pend = (const u_char *) &prec->data + prec->offset - 2;
cend = (const u_char *) &crec->data + crec->offset - 2;
for (start = *cpos; cend >= start; cend--) {
if (wts[*cend] != wts[*pend])
break;

View File

@ -1,4 +1,4 @@
/* $NetBSD: files.c,v 1.5 2000/10/16 21:53:19 jdolecek Exp $ */
/* $NetBSD: files.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $ */
/*-
* Copyright (c) 1993
@ -40,7 +40,7 @@
#include "fsort.h"
#ifndef lint
__RCSID("$NetBSD: files.c,v 1.5 2000/10/16 21:53:19 jdolecek Exp $");
__RCSID("$NetBSD: files.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $");
__SCCSID("@(#)files.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -62,7 +62,7 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
{
int i;
u_char *hp;
static long nleft = 0;
static size_t nleft = 0;
static int cnt = 0, flag = -1;
static u_char maxb = 0;
static FILE *fp;
@ -84,9 +84,7 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
return (EOF);
}
fp = fstack[infl0.top + cnt].fp;
hp = (u_char *) &nleft;
for (i = sizeof(TRECHEADER); i; --i)
*hp++ = getc(fp);
fread(&nleft, sizeof(nleft), 1, fp);
if (binno < maxb)
fstack[infl0.top+cnt].max_o
+= sizeof(nleft) + nleft;
@ -107,10 +105,9 @@ getnext(binno, infl0, nfiles, pos, end, dummy)
}
if ((u_char *) pos > end - sizeof(TRECHEADER))
return (BUFFEND);
hp = (u_char *) pos;
for (i = sizeof(TRECHEADER); i ; --i)
*hp++ = (u_char) getc(fp);
fread(pos, sizeof(TRECHEADER), 1, fp);
if (end - pos->data < pos->length) {
hp = ((u_char *) pos) + sizeof(TRECHEADER);
for (i = sizeof(TRECHEADER); i ; i--)
ungetc(*--hp, fp);
return (BUFFEND);

View File

@ -1,4 +1,4 @@
/* $NetBSD: fsort.c,v 1.5 2000/10/16 21:53:19 jdolecek Exp $ */
/* $NetBSD: fsort.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $ */
/*-
* Copyright (c) 1993
@ -47,7 +47,7 @@
#include "fsort.h"
#ifndef lint
__RCSID("$NetBSD: fsort.c,v 1.5 2000/10/16 21:53:19 jdolecek Exp $");
__RCSID("$NetBSD: fsort.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $");
__SCCSID("@(#)fsort.c 8.1 (Berkeley) 6/6/93");
#endif /* not lint */
@ -143,10 +143,11 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
* bigger buffer */
bufsize *= 2;
buffer = realloc(buffer, bufsize);
bufend = buffer + bufsize;
if (!buffer)
if (!buffer) {
err(2, "failed to realloc buffer to %ld bytes",
(unsigned long) bufsize);
}
bufend = buffer + bufsize;
continue;
}
if (c == BUFFEND || ntfiles || mfct) { /* push */
@ -264,7 +265,7 @@ onepass(a, depth, n, sizes, tr, fp)
u_char *tr;
FILE *fp;
{
long tsizes[NBINS+1];
size_t tsizes[NBINS+1];
const u_char **bin[257], ***bp, ***bpmax, **top[256], ***tp;
static int histo[256];
int *hp;
@ -274,7 +275,7 @@ onepass(a, depth, n, sizes, tr, fp)
memset(tsizes, 0, sizeof(tsizes));
depth += sizeof(TRECHEADER);
an = a + n;
an = &a[n];
for (ak = a; ak < an; ak++) {
histo[c = tr[**ak]]++;
tsizes[c] += ((const RECHEADER *) (*ak -= depth))->length;
@ -298,7 +299,7 @@ onepass(a, depth, n, sizes, tr, fp)
n = an - ak;
tsizes[c] += n * sizeof(TRECHEADER);
/* tell getnext how many elements in this bin, this segment. */
EWRITE(tsizes+c, sizeof(long), 1, fp);
EWRITE(&tsizes[c], sizeof(size_t), 1, fp);
sizes[c] += tsizes[c];
for (; ak < an; ++ak)
putrec((const RECHEADER *) *ak, fp);