summaryrefslogtreecommitdiff
path: root/usr.bin/sort
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>1999-05-24 17:57:20 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>1999-05-24 17:57:20 +0000
commit307a36ae6cc0219b9923f7169597b82827342378 (patch)
treefcb2bda494cf73120571f418c0286c3f65ec9557 /usr.bin/sort
parent15692f0db44772091e8421fdfdbd95fd3a2fe04a (diff)
Dynamically allocate memory for structures whose sizes are based on the
number of keys given. Closes PR #825.
Diffstat (limited to 'usr.bin/sort')
-rw-r--r--usr.bin/sort/append.c6
-rw-r--r--usr.bin/sort/fields.c6
-rw-r--r--usr.bin/sort/fsort.c21
-rw-r--r--usr.bin/sort/init.c15
-rw-r--r--usr.bin/sort/msort.c27
-rw-r--r--usr.bin/sort/sort.c30
-rw-r--r--usr.bin/sort/sort.h7
7 files changed, 68 insertions, 44 deletions
diff --git a/usr.bin/sort/append.c b/usr.bin/sort/append.c
index 9542fb5e86b..64e5a06b2b8 100644
--- a/usr.bin/sort/append.c
+++ b/usr.bin/sort/append.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: append.c,v 1.4 1998/05/02 02:41:55 mickey Exp $ */
+/* $OpenBSD: append.c,v 1.5 1999/05/24 17:57:17 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)append.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: append.c,v 1.4 1998/05/02 02:41:55 mickey Exp $";
+static char rcsid[] = "$OpenBSD: append.c,v 1.5 1999/05/24 17:57:17 millert Exp $";
#endif
#endif /* not lint */
@@ -76,7 +76,7 @@ append(keylist, nelem, depth, fp, put, ftbl)
struct field *ftbl;
{
register u_char *wts, *wts1;
- register n, odepth;
+ register int n, odepth;
register u_char **cpos, **ppos, **lastkey;
register u_char *cend, *pend, *start;
register RECHEADER *crec, *prec;
diff --git a/usr.bin/sort/fields.c b/usr.bin/sort/fields.c
index e7dbf7a9c3d..46e955a59bc 100644
--- a/usr.bin/sort/fields.c
+++ b/usr.bin/sort/fields.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fields.c,v 1.3 1997/06/30 05:36:16 millert Exp $ */
+/* $OpenBSD: fields.c,v 1.4 1999/05/24 17:57:17 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)fields.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: fields.c,v 1.3 1997/06/30 05:36:16 millert Exp $";
+static char rcsid[] = "$OpenBSD: fields.c,v 1.4 1999/05/24 17:57:17 millert Exp $";
#endif
#endif /* not lint */
@@ -63,7 +63,7 @@ extern u_char *enterfield __P((u_char *, u_char *, struct field *, int));
extern u_char *number __P((u_char *, u_char *, u_char *, u_char *, int));
-extern struct coldesc clist[(ND+1)*2];
+extern struct coldesc *clist;
extern int ncols;
#define DECIMAL '.'
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c
index 6abd6e0b74a..7b605d424d8 100644
--- a/usr.bin/sort/fsort.c
+++ b/usr.bin/sort/fsort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fsort.c,v 1.6 1998/05/02 02:41:56 mickey Exp $ */
+/* $OpenBSD: fsort.c,v 1.7 1999/05/24 17:57:18 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.6 1998/05/02 02:41:56 mickey Exp $";
+static char rcsid[] = "$OpenBSD: fsort.c,v 1.7 1999/05/24 17:57:18 millert Exp $";
#endif
#endif /* not lint */
@@ -93,11 +93,13 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
tfield[0].icol.num = 1;
weights = ftbl[0].weights;
if (!buffer) {
- buffer = malloc(BUFSIZE + 1);
- keylist = malloc(MAXNUM * sizeof(u_char *));
- memset(keylist, 0, MAXNUM * sizeof(u_char *));
- if (!SINGL_FLD)
- linebuf = malloc(MAXLLEN);
+ if ((buffer = malloc(BUFSIZE + 1)) == NULL ||
+ (keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
+ errx(2, "cannot allocate memory");
+ if (!SINGL_FLD) {
+ if ((linebuf = malloc(MAXLLEN)) == NULL)
+ errx(2, "cannot allocate memory");
+ }
}
bufend = buffer + BUFSIZE;
if (binno >= 0) {
@@ -150,6 +152,9 @@ fsort(binno, depth, infiles, nfiles, outfp, ftbl)
if (mfct == 16 ||(c == EOF && ntfiles)) {
tmpbuf = malloc(bufend -
crec->data);
+ if (tmpbuf == NULL)
+ errx(2, "cannot "
+ "allocate memory");
memmove(tmpbuf, crec->data,
bufend - crec->data);
fstack[tfiles.top + ntfiles].fp
@@ -255,7 +260,7 @@ onepass(a, depth, n, sizes, tr, fp)
{
int tsizes[NBINS+1];
u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp;
- static histo[256];
+ static int histo[256];
int *hp;
register int c;
u_char **an, *t, **aj;
diff --git a/usr.bin/sort/init.c b/usr.bin/sort/init.c
index 9c7ac643810..0e4ad3e9cc3 100644
--- a/usr.bin/sort/init.c
+++ b/usr.bin/sort/init.c
@@ -1,4 +1,4 @@
-/* OpenBSD$ */
+/* $OpenBSD: init.c,v 1.3 1999/05/24 17:57:18 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)init.c 8.1 (Berkeley) 6/6/93";
#else
-static char rcsid[] = "$OpenBSD: init.c,v 1.2 1997/06/16 02:21:56 millert Exp $";
+static char rcsid[] = "$OpenBSD: init.c,v 1.3 1999/05/24 17:57:18 millert Exp $";
#endif
#endif /* not lint */
@@ -49,7 +49,7 @@ static char rcsid[] = "$OpenBSD: init.c,v 1.2 1997/06/16 02:21:56 millert Exp $"
#include <ctype.h>
#include <string.h>
-extern struct coldesc clist[(ND+1)*2];
+extern struct coldesc *clist;
extern int ncols;
u_char gweights[NBINS];
@@ -153,11 +153,8 @@ setfield(pos, cur_fld, gflag)
struct field *cur_fld;
int gflag;
{
- static int nfields = 0;
int tmp;
char *setcolumn();
- if (++nfields == ND)
- errx(2, "too many sort keys. (Limit is %d)", ND-1);
cur_fld->weights = ascii;
cur_fld->mask = alltable;
pos = setcolumn(pos, cur_fld, gflag);
@@ -223,8 +220,10 @@ fixit(argc, argv)
char **argv;
{
int i, j, v, w, x;
- static char vbuf[ND*20], *vpos, *tpos;
- vpos = vbuf;
+ static char *vbuf, *vpos, *tpos;
+
+ if ((vpos = vbuf = calloc(ND*20, sizeof(char))) == NULL)
+ errx(2, "cannot allocate memory");
for (i = 1; i < *argc; i++) {
if (argv[i][0] == '+') {
diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c
index 5b2e69f3621..6da0e01ef04 100644
--- a/usr.bin/sort/msort.c
+++ b/usr.bin/sort/msort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: msort.c,v 1.6 1997/06/30 05:36:17 millert Exp $ */
+/* $OpenBSD: msort.c,v 1.7 1999/05/24 17:57:18 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.6 1997/06/30 05:36:17 millert Exp $";
+static char rcsid[] = "$OpenBSD: msort.c,v 1.7 1999/05/24 17:57:18 millert Exp $";
#endif
#endif /* not lint */
@@ -89,16 +89,21 @@ fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
wts = ftbl->weights;
if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
wts1 = (ftbl->flags & R) ? Rascii : ascii;
- if (!cfilebuf)
+ if (!cfilebuf) {
cfilebuf = malloc(MAXLLEN + sizeof(TMFILE));
+ if (cfilebuf == NULL)
+ errx(2, "cannot allocate memory");
+ }
i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
if (!buffer || i > BUFSIZE) {
buffer = buffer ? realloc(buffer, i) : malloc(i);
if (!buffer)
- err(2, NULL);
- if (!SINGL_FLD)
- linebuf = malloc(MAXLLEN);
+ errx(2, "cannot allocate memory");
+ if (!SINGL_FLD) {
+ if ((linebuf = malloc(MAXLLEN)) == NULL)
+ errx(2, "cannot allocate memory");
+ }
}
if (binno >= 0)
@@ -262,9 +267,13 @@ order(infile, get, ftbl)
int c;
RECHEADER *crec, *prec, *trec;
- if (!SINGL_FLD)
- linebuf = malloc(MAXLLEN);
+ if (!SINGL_FLD) {
+ if ((linebuf = malloc(MAXLLEN)) == NULL)
+ errx(2, "cannot allocate memory");
+ }
buffer = malloc(2 * (MAXLLEN + sizeof(TRECHEADER)));
+ if (buffer == NULL)
+ errx(2, "cannot allocate memory");
end = buffer + 2 * (MAXLLEN + sizeof(TRECHEADER));
crec = (RECHEADER *) buffer;
prec = (RECHEADER *) (buffer + MAXLLEN + sizeof(TRECHEADER));
@@ -296,7 +305,7 @@ static int
cmp(rec1, rec2)
RECHEADER *rec1, *rec2;
{
- register r;
+ register int r;
register u_char *pos1, *pos2, *end;
register u_char *cwts;
for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 80d9b17f9b8..8447b47df82 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sort.c,v 1.11 1998/07/24 00:32:24 deraadt Exp $ */
+/* $OpenBSD: sort.c,v 1.12 1999/05/24 17:57:19 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -46,7 +46,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.11 1998/07/24 00:32:24 deraadt Exp $";
+static char rcsid[] = "$OpenBSD: sort.c,v 1.12 1999/05/24 17:57:19 millert Exp $";
#endif
#endif /* not lint */
@@ -82,10 +82,9 @@ u_char ascii[NBINS], Rascii[NBINS], RFtable[NBINS], Ftable[NBINS];
*/
u_char dtable[NBINS], itable[NBINS], alltable[NBINS];
int SINGL_FLD = 0, SEP_FLAG = 0, UNIQUE = 0;
-struct coldesc clist[(ND+1)*2];
+struct coldesc *clist;
int ncols = 0;
-extern struct coldesc clist[(ND+1)*2];
-extern int ncols;
+int ND = 10; /* limit on number of -k options. */
char devstdin[] = _PATH_STDIN;
char toutpath[_POSIX_PATH_MAX];
@@ -95,6 +94,15 @@ static void cleanup __P((void));
static void onsig __P((int));
static void usage __P((char *));
+#define CHECK_NFIELDS \
+ if (++nfields == ND) { \
+ ND += 10; \
+ if ((p = realloc(fldtab, ND)) == NULL) \
+ errx(2, "cannot allocate memory"); \
+ ftpos = p + (ftpos - fldtab); \
+ fldtab = p; \
+ }
+
int
main(argc, argv)
int argc;
@@ -102,18 +110,20 @@ main(argc, argv)
{
int (*get)();
int ch, i, stdinflag = 0, tmp = 0;
- char cflag = 0, mflag = 0;
+ char nfields = 0, cflag = 0, mflag = 0;
char *outfile, *outpath = 0;
- struct field fldtab[ND+2], *ftpos;
+ struct field *fldtab, *ftpos;
union f_handle filelist;
FILE *outfp = NULL;
+ void *p;
- memset(fldtab, 0, (ND+2)*sizeof(struct field));
+ if ((clist = calloc((ND+1)*2, sizeof(struct coldesc))) == NULL ||
+ (ftpos = fldtab = calloc(ND+2, sizeof(struct field))) == NULL)
+ errx(2, "cannot allocate memory");
memset(d_mask, 0, NBINS);
d_mask[REC_D = '\n'] = REC_D_F;
SINGL_FLD = SEP_FLAG = 0;
d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
- ftpos = fldtab;
fixit(&argc, argv);
if (!issetugid() && (outfile = getenv("TMPDIR")))
tmpdir = outfile;
@@ -138,6 +148,7 @@ main(argc, argv)
outpath = optarg;
break;
case 'k':
+ CHECK_NFIELDS;
setfield(optarg, ++ftpos, fldtab->flags);
break;
case 't':
@@ -212,6 +223,7 @@ main(argc, argv)
fldtab[0].icol.num = 1;
} else {
if (!fldtab[1].icol.num) {
+ CHECK_NFIELDS;
fldtab[0].flags &= ~(BI|BT);
setfield("1", ++ftpos, fldtab->flags);
}
diff --git a/usr.bin/sort/sort.h b/usr.bin/sort/sort.h
index 048bfbd9d8f..6146674bff0 100644
--- a/usr.bin/sort/sort.h
+++ b/usr.bin/sort/sort.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sort.h,v 1.3 1997/06/30 05:36:18 millert Exp $ */
+/* $OpenBSD: sort.h,v 1.4 1999/05/24 17:57:19 millert Exp $ */
/*-
* Copyright (c) 1993
@@ -66,8 +66,6 @@
#define FLD_D 2 /* ' ', '\t' default; from -t otherwise */
#define REC_D_F 4 /* '\n' default; from -T otherwise */
-#define ND 10 /* limit on number of -k options. */
-
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))
@@ -78,7 +76,7 @@
#define EWRITE(ptr, size, n, f) { \
if (!fwrite(ptr, size, n, f)) \
- err(2, NULL); \
+ err(2, "fwrite"); \
}
/* length of record is currently limited to 2^16 - 1 */
@@ -142,5 +140,6 @@ extern u_char d_mask[NBINS];
extern int SINGL_FLD, SEP_FLAG, UNIQUE;
extern int REC_D;
extern char *tmpdir;
+extern int ND; /* limit on number of -k options. */
#include "extern.h"