Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/join Don't step off into space after a realloc(). (...



details:   https://anonhg.NetBSD.org/src/rev/ebc50a91e6c8
branches:  trunk
changeset: 487652:ebc50a91e6c8
user:      mycroft <mycroft%NetBSD.org@localhost>
date:      Sat Jun 10 19:03:22 2000 +0000

description:
Don't step off into space after a realloc().  (Get rid of `lastlp'.)
Also, allocate more data in powers of 2.

diffstat:

 usr.bin/join/join.c |  27 ++++++++++++++++-----------
 1 files changed, 16 insertions(+), 11 deletions(-)

diffs (86 lines):

diff -r a11896894147 -r ebc50a91e6c8 usr.bin/join/join.c
--- a/usr.bin/join/join.c       Sat Jun 10 18:56:47 2000 +0000
+++ b/usr.bin/join/join.c       Sat Jun 10 19:03:22 2000 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: join.c,v 1.16 1999/08/10 20:09:02 tron Exp $   */
+/*     $NetBSD: join.c,v 1.17 2000/06/10 19:03:22 mycroft Exp $        */
 
 /*-
  * Copyright (c) 1991 The Regents of the University of California.
@@ -48,7 +48,7 @@
 #if 0
 static char sccsid[] = "from: @(#)join.c       5.1 (Berkeley) 11/18/91";
 #else
-__RCSID("$NetBSD: join.c,v 1.16 1999/08/10 20:09:02 tron Exp $");
+__RCSID("$NetBSD: join.c,v 1.17 2000/06/10 19:03:22 mycroft Exp $");
 #endif
 #endif /* not lint */
 
@@ -294,7 +294,7 @@
 slurp(F)
        INPUT *F;
 {
-       LINE *lp, *lastlp;
+       LINE *lp;
        LINE tmp;
        size_t len;
        int cnt;
@@ -304,8 +304,7 @@
         * Read all of the lines from an input file that have the same
         * join field.
         */
-       F->setcnt = 0;
-       for (lastlp = NULL;; ++F->setcnt, lastlp = lp) {
+       for (F->setcnt = 0;; ++F->setcnt) {
                /*
                 * If we're out of space to hold line structures, allocate
                 * more.  Initialize the structure so that we know that this
@@ -313,11 +312,15 @@
                 */
                if (F->setcnt == F->setalloc) {
                        cnt = F->setalloc;
-                       F->setalloc += 100;
+                       if (F->setalloc == 0)
+                               F->setalloc = 64;
+                       else
+                               F->setalloc <<= 1;
                        if ((F->set = realloc(F->set,
                            F->setalloc * sizeof(LINE))) == NULL)
                                enomem();
-                       memset(F->set + cnt, 0, 100 * sizeof(LINE));
+                       memset(F->set + cnt, 0,
+                           (F->setalloc - cnt) * sizeof(LINE));
                }
                        
                /*
@@ -341,8 +344,7 @@
                        if (lp->linealloc == 0)
                                lp->linealloc = 128;
                        while (lp->linealloc <= len + 1)
-                               lp->linealloc *= 2;
-
+                               lp->linealloc <<= 1;
                        if ((lp->line = realloc(lp->line,
                            lp->linealloc * sizeof(char))) == NULL)
                                enomem();
@@ -362,7 +364,10 @@
                        if (spans && *fieldp == '\0')
                                continue;
                        if (lp->fieldcnt == lp->fieldalloc) {
-                               lp->fieldalloc += 100;
+                               if (lp->fieldalloc == 0)
+                                       lp->fieldalloc = 16;
+                               else
+                                       lp->fieldalloc <<= 1;
                                if ((lp->fields = realloc(lp->fields,
                                    lp->fieldalloc * sizeof(char *))) == NULL)
                                        enomem();
@@ -371,7 +376,7 @@
                }
 
                /* See if the join field value has changed. */
-               if (lastlp != NULL && cmp(lp, F->joinf, lastlp, F->joinf)) {
+               if (F->setcnt && cmp(lp, F->joinf, lp - 1, F->joinf)) {
                        F->pushback = F->setcnt;
                        break;
                }



Home | Main Index | Thread Index | Old Index