Don't touch past the end of allocated region. It results segmentation

violation.
This commit is contained in:
enami 2010-02-05 21:58:41 +00:00
parent cbd4507895
commit 47e571f2ea
4 changed files with 21 additions and 13 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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));
}

View File

@ -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 *);