Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/find Reallocate memory correctly while substituting ...



details:   https://anonhg.NetBSD.org/src/rev/2f4b6108a300
branches:  trunk
changeset: 515269:2f4b6108a300
user:      enami <enami%NetBSD.org@localhost>
date:      Fri Sep 21 07:11:33 2001 +0000

description:
Reallocate memory correctly while substituting the braces.

diffstat:

 usr.bin/find/extern.h   |   4 ++--
 usr.bin/find/function.c |   6 +++---
 usr.bin/find/misc.c     |  25 ++++++++++++++++++-------
 3 files changed, 23 insertions(+), 12 deletions(-)

diffs (97 lines):

diff -r b33333e85268 -r 2f4b6108a300 usr.bin/find/extern.h
--- a/usr.bin/find/extern.h     Fri Sep 21 06:12:52 2001 +0000
+++ b/usr.bin/find/extern.h     Fri Sep 21 07:11:33 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.13 2000/03/10 11:46:04 itohy Exp $        */
+/*     $NetBSD: extern.h,v 1.14 2001/09/21 07:11:33 enami Exp $        */
 
 /*-
  * Copyright (c) 1991, 1993, 1994
@@ -37,7 +37,7 @@
 
 #include <sys/cdefs.h>
 
-void    brace_subst __P((char *, char **, char *, int));
+void    brace_subst __P((char *, char **, char *, int *));
 void   *emalloc __P((unsigned int));
 PLAN   *find_create __P((char ***));
 int     find_execute __P((PLAN *, char **));
diff -r b33333e85268 -r 2f4b6108a300 usr.bin/find/function.c
--- a/usr.bin/find/function.c   Fri Sep 21 06:12:52 2001 +0000
+++ b/usr.bin/find/function.c   Fri Sep 21 07:11:33 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: function.c,v 1.37 2001/09/18 05:11:15 simonb Exp $     */
+/*     $NetBSD: function.c,v 1.38 2001/09/21 07:11:33 enami Exp $      */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "from: @(#)function.c   8.10 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: function.c,v 1.37 2001/09/18 05:11:15 simonb Exp $");
+__RCSID("$NetBSD: function.c,v 1.38 2001/09/21 07:11:33 enami Exp $");
 #endif
 #endif /* not lint */
 
@@ -345,7 +345,7 @@
        for (cnt = 0; plan->e_argv[cnt]; ++cnt)
                if (plan->e_len[cnt])
                        brace_subst(plan->e_orig[cnt], &plan->e_argv[cnt],
-                           entry->fts_path, plan->e_len[cnt]);
+                           entry->fts_path, &plan->e_len[cnt]);
 
        if (plan->flags == F_NEEDOK && !queryuser(plan->e_argv))
                return (0);
diff -r b33333e85268 -r 2f4b6108a300 usr.bin/find/misc.c
--- a/usr.bin/find/misc.c       Fri Sep 21 06:12:52 2001 +0000
+++ b/usr.bin/find/misc.c       Fri Sep 21 07:11:33 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: misc.c,v 1.7 1998/02/02 14:02:25 mrg Exp $     */
+/*     $NetBSD: misc.c,v 1.8 2001/09/21 07:11:33 enami Exp $   */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -41,7 +41,7 @@
 #if 0
 static char sccsid[] = "from: @(#)misc.c       8.2 (Berkeley) 4/1/94";
 #else
-__RCSID("$NetBSD: misc.c,v 1.7 1998/02/02 14:02:25 mrg Exp $");
+__RCSID("$NetBSD: misc.c,v 1.8 2001/09/21 07:11:33 enami Exp $");
 #endif
 #endif /* not lint */
 
@@ -65,17 +65,28 @@
 void
 brace_subst(orig, store, path, len)
        char *orig, **store, *path;
-       int len;
+       int *len;
 {
-       int plen;
-       char ch, *p;
+       int nlen, plen, rest;
+       char ch, *p, *ostore;
 
        plen = strlen(path);
        for (p = *store; (ch = *orig) != '\0'; ++orig)
                if (ch == '{' && orig[1] == '}') {
-                       while ((p - *store) + plen > len)
-                               if (!(*store = realloc(*store, len *= 2)))
+                       /* Length of string after the {}. */
+                       rest = strlen(&orig[2]);
+
+                       nlen = *len;
+                       while ((p - *store) + plen + rest + 1 > nlen)
+                               nlen *= 2;
+
+                       if (nlen > *len) {
+                               ostore = *store;
+                               if ((*store = realloc(ostore, nlen)) == NULL)
                                        err(1, "realloc");
+                               *len = nlen;
+                               p += *store - ostore;   /* Relocate. */
+                       }
                        memmove(p, path, plen);
                        p += plen;
                        ++orig;



Home | Main Index | Thread Index | Old Index