summaryrefslogtreecommitdiff
path: root/usr.bin/sort
diff options
context:
space:
mode:
authorenami <enami@NetBSD.org>2010-02-05 21:58:41 +0000
committerenami <enami@NetBSD.org>2010-02-05 21:58:41 +0000
commit47e571f2eabba4fa9b5dee5e518452bfcd8bf113 (patch)
tree98ed052b4e878201a99dfc27942a1dd204deb20b /usr.bin/sort
parentcbd4507895f8c25edfc5dda73808015586754440 (diff)
Don't touch past the end of allocated region. It results segmentation
violation.
Diffstat (limited to 'usr.bin/sort')
-rw-r--r--usr.bin/sort/fsort.c8
-rw-r--r--usr.bin/sort/msort.c12
-rw-r--r--usr.bin/sort/sort.c11
-rw-r--r--usr.bin/sort/sort.h3
4 files changed, 21 insertions, 13 deletions
diff --git a/usr.bin/sort/fsort.c b/usr.bin/sort/fsort.c
index fc6e4c0cff4..c229ae227a5 100644
--- a/usr.bin/sort/fsort.c
+++ b/usr.bin/sort/fsort.c
@@ -1,4 +1,4 @@
-/* $NetBSD: fsort.c,v 1.46 2009/11/06 18:34:22 joerg Exp $ */
+/* $NetBSD: fsort.c,v 1.47 2010/02/05 21:58:41 enami Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
#include "sort.h"
#include "fsort.h"
-__RCSID("$NetBSD: fsort.c,v 1.46 2009/11/06 18:34:22 joerg Exp $");
+__RCSID("$NetBSD: fsort.c,v 1.47 2010/02/05 21:58:41 enami Exp $");
#include <stdlib.h>
#include <string.h>
@@ -95,7 +95,7 @@ fsort(struct filelist *filelist, int nfiles, FILE *outfp, struct field *ftbl)
int file_no;
int max_recs = DEBUG('m') ? 16 : MAXNUM;
- buffer = malloc(bufsize);
+ buffer = allocrec(NULL, bufsize);
bufend = (u_char *)buffer + bufsize;
/* Allocate double length keymap for radix_sort */
keylist = malloc(2 * max_recs * sizeof(*keylist));
@@ -154,7 +154,7 @@ fsort(struct filelist *filelist, int nfiles, FILE *outfp, struct field *ftbl)
/* c == BUFFEND, and we can process more data */
/* Allocate a larger buffer for this lot of data */
bufsize *= 2;
- nbuffer = realloc(buffer, bufsize);
+ nbuffer = allocrec(buffer, bufsize);
if (!nbuffer) {
err(2, "failed to realloc buffer to %zu bytes",
bufsize);
diff --git a/usr.bin/sort/msort.c b/usr.bin/sort/msort.c
index 51d2d4f9c38..200b7224d95 100644
--- a/usr.bin/sort/msort.c
+++ b/usr.bin/sort/msort.c
@@ -1,4 +1,4 @@
-/* $NetBSD: msort.c,v 1.29 2009/11/06 18:34:22 joerg Exp $ */
+/* $NetBSD: msort.c,v 1.30 2010/02/05 21:58:42 enami Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
#include "sort.h"
#include "fsort.h"
-__RCSID("$NetBSD: msort.c,v 1.29 2009/11/06 18:34:22 joerg Exp $");
+__RCSID("$NetBSD: msort.c,v 1.30 2010/02/05 21:58:42 enami Exp $");
#include <stdlib.h>
#include <string.h>
@@ -206,7 +206,7 @@ merge_sort_fstack(FILE *outfp, put_func_t put, struct field *ftbl)
for (nfiles = i = 0; i < fstack_count; i++) {
cfile = &fstack[i];
if (cfile->rec == NULL) {
- cfile->rec = emalloc(DEFLLEN);
+ cfile->rec = allocrec(NULL, DEFLLEN);
cfile->end = (u_char *)cfile->rec + DEFLLEN;
}
rewind(cfile->fp);
@@ -219,7 +219,7 @@ merge_sort_fstack(FILE *outfp, put_func_t put, struct field *ftbl)
if (c == BUFFEND) {
/* Double buffer size */
sz = (cfile->end - (u_char *)cfile->rec) * 2;
- cfile->rec = erealloc(cfile->rec, sz);
+ cfile->rec = allocrec(cfile->rec, sz);
cfile->end = (u_char *)cfile->rec + sz;
continue;
}
@@ -245,7 +245,7 @@ merge_sort_fstack(FILE *outfp, put_func_t put, struct field *ftbl)
* output file - maintaining one record from each file in the sorted
* list.
*/
- new_rec = emalloc(DEFLLEN);
+ new_rec = allocrec(NULL, DEFLLEN);
new_end = (u_char *)new_rec + DEFLLEN;
for (;;) {
cfile = flist[0];
@@ -263,7 +263,7 @@ merge_sort_fstack(FILE *outfp, put_func_t put, struct field *ftbl)
if (c == BUFFEND) {
/* Buffer not large enough - double in size */
sz = (new_end - (u_char *)new_rec) * 2;
- new_rec = erealloc(new_rec, sz);
+ new_rec = allocrec(new_rec, sz);
new_end = (u_char *)new_rec +sz;
continue;
}
diff --git a/usr.bin/sort/sort.c b/usr.bin/sort/sort.c
index 80d47fe4a11..fc390844a28 100644
--- a/usr.bin/sort/sort.c
+++ b/usr.bin/sort/sort.c
@@ -1,4 +1,4 @@
-/* $NetBSD: sort.c,v 1.57 2009/11/06 18:34:22 joerg Exp $ */
+/* $NetBSD: sort.c,v 1.58 2010/02/05 21:58:42 enami Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -76,7 +76,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
-__RCSID("$NetBSD: sort.c,v 1.57 2009/11/06 18:34:22 joerg Exp $");
+__RCSID("$NetBSD: sort.c,v 1.58 2010/02/05 21:58:42 enami Exp $");
#include <sys/types.h>
#include <sys/time.h>
@@ -402,3 +402,10 @@ usage(const char *msg)
" [-t char] [file ...]\n");
exit(2);
}
+
+RECHEADER *
+allocrec(RECHEADER *rec, size_t size)
+{
+
+ return (erealloc(rec, size + sizeof(long) - 1));
+}
diff --git a/usr.bin/sort/sort.h b/usr.bin/sort/sort.h
index 4593ffd2251..39d59bc1796 100644
--- a/usr.bin/sort/sort.h
+++ b/usr.bin/sort/sort.h
@@ -1,4 +1,4 @@
-/* $NetBSD: sort.h,v 1.30 2009/09/28 20:30:01 dsl Exp $ */
+/* $NetBSD: sort.h,v 1.31 2010/02/05 21:58:42 enami Exp $ */
/*-
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
@@ -174,6 +174,7 @@ extern int ncols;
#define DEBUG(ch) (debug_flags & (1 << ((ch) & 31)))
extern unsigned int debug_flags;
+RECHEADER *allocrec(RECHEADER *, size_t);
void append(RECHEADER **, int, FILE *, void (*)(const RECHEADER *, FILE *));
void concat(FILE *, FILE *);
length_t enterkey(RECHEADER *, const u_char *, u_char *, size_t, struct field *);