gawk-diffs
[Top][All Lists]
Advanced

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

[gawk-diffs] [SCM] gawk branch, porting, updated. gawk-4.1.0-3609-g57666


From: Arnold Robbins
Subject: [gawk-diffs] [SCM] gawk branch, porting, updated. gawk-4.1.0-3609-g5766636
Date: Sun, 20 Jan 2019 15:19:19 -0500 (EST)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gawk".

The branch, porting has been updated
       via  5766636f7bb7eb6d8fa9fd1b097ca74329062173 (commit)
       via  7560c312229e5967ff1b479b8ced6d3e8e99dc37 (commit)
       via  c73d262642ee3a3d6585dc44acc62c432910f920 (commit)
       via  fa9c6b921f784ad6f35f36b65635969ae4fade2d (commit)
       via  82a1b8457331ee7fa439d67676ceb68cab017966 (commit)
       via  5812db74483bce781281422953d359fde5be6a99 (commit)
       via  69b4c6e0b70f92e829848e21c4d8983fac904f30 (commit)
      from  f98d137c530a92756895bb30e143579ac95c7ce8 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.sv.gnu.org/cgit/gawk.git/commit/?id=5766636f7bb7eb6d8fa9fd1b097ca74329062173

commit 5766636f7bb7eb6d8fa9fd1b097ca74329062173
Author: Arnold D. Robbins <address@hidden>
Date:   Sun Jan 20 21:42:36 2019 +0200

    Cleanups for assoc_set.

diff --git a/ChangeLog b/ChangeLog
index d7d1471..5e3f9a2 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-01-20         Arnold D. Robbins     <address@hidden>
+
+       * debug.c (do_set_var): Add comments before calls to assoc_set.
+       * interpret.h (r_interpret): For Op_sub_array, deref the subscript
+       appropriately. Thanks to Andy Schorr for the catch.
+
 2019-01-15         Arnold D. Robbins     <address@hidden>
 
        * array.c (asort_actual): Use assoc_set in 2 places.
diff --git a/debug.c b/debug.c
index f36a327..05083a4 100644
--- a/debug.c
+++ b/debug.c
@@ -1270,6 +1270,8 @@ do_set_var(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
                                        arg = arg->next;
                                        val = arg->a_node;
                                        newval = dupnode(val);
+                                       // subs should not be freed, so
+                                       // use dupnode in call to assoc_set.
                                        assoc_set(r, dupnode(subs), newval);
                                        fprintf(out_fp, "%s[\"%.*s\"] = ", 
name, (int) subs->stlen, subs->stptr);
                                        valinfo(newval, fprintf, out_fp);
@@ -1280,6 +1282,8 @@ do_set_var(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
                                        array = make_array();
                                        array->vname = estrdup(subs->stptr, 
subs->stlen);
                                        array->parent_array = r;
+                                       // subs should not be freed, so
+                                       // use dupnode in call to assoc_set.
                                        assoc_set(r, dupnode(subs), array);
                                        r = array;
                                } else if (value->type != Node_var_array) {
diff --git a/interpret.h b/interpret.h
index 53abadc..e989667 100644
--- a/interpret.h
+++ b/interpret.h
@@ -310,7 +310,8 @@ uninitialized_scalar:
                                t2 = force_string(t2);
                                fatal(_("attempt to use scalar `%s[\"%.*s\"]' 
as an array"),
                                                array_vname(t1), (int) 
t2->stlen, t2->stptr);
-                       }
+                       } else
+                               DEREF(t2);
 
                        PUSH(r);
                        break;

http://git.sv.gnu.org/cgit/gawk.git/commit/?id=7560c312229e5967ff1b479b8ced6d3e8e99dc37

commit 7560c312229e5967ff1b479b8ced6d3e8e99dc37
Merge: c73d262 f98d137
Author: Arnold D. Robbins <address@hidden>
Date:   Fri Jan 18 14:41:00 2019 +0200

    Merge branch 'master' into feature/assoc_set


http://git.sv.gnu.org/cgit/gawk.git/commit/?id=c73d262642ee3a3d6585dc44acc62c432910f920

commit c73d262642ee3a3d6585dc44acc62c432910f920
Author: Arnold D. Robbins <address@hidden>
Date:   Tue Jan 15 21:27:06 2019 +0200

    Finish transition to using assoc_set.

diff --git a/ChangeLog b/ChangeLog
index 0de3b7a..d7d1471 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,18 @@
+2019-01-15         Arnold D. Robbins     <address@hidden>
+
+       * array.c (asort_actual): Use assoc_set in 2 places.
+       * awk.h (assoc_set): Improve leading comment.
+       * debug.c (do_set_var): Use assoc_set in 2 places.
+       * field.c (set_element, update_PROCINFO_str, update_PROCINFO_num):
+       Use assoc_set in each.
+       * gawkapi.c (api_set_array_element): Use assoc_set.
+       * interpret.h (r_interpret): Use assoc_set.
+       * main.c (init_args, load_environ, load_procinfo_argv):
+       Use assoc_set in each.
+       * mpfr.c (do_mpfr_intdiv): Use assoc_set in 2 places.
+       * str_array.c (str_lookup): Fix leading comment.
+       * symbol.c (install): Use assoc_set.
+
 2019-01-15         Andrew J. Schorr      <address@hidden>
 
        * builtin.c (do_match, do_intdiv): Remove unused `sub' and `lhs'
diff --git a/array.c b/array.c
index 34b7076..2cc7674 100644
--- a/array.c
+++ b/array.c
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 1986, 1988, 1989, 1991-2014, 2016, 2018,
+ * Copyright (C) 1986, 1988, 1989, 1991-2014, 2016, 2018, 2019,
  * the Free Software Foundation, Inc.
  *
  * This file is part of GAWK, the GNU implementation of the
@@ -796,7 +796,7 @@ asort_actual(int nargs, sort_context_t ctxt)
 {
        NODE *array, *dest = NULL, *result;
        NODE *r, *subs, *s;
-       NODE **list = NULL, **ptr, **lhs;
+       NODE **list = NULL, **ptr;
        unsigned long num_elems, i;
        const char *sort_str;
        char save;
@@ -884,12 +884,7 @@ asort_actual(int nargs, sort_context_t ctxt)
 
                for (i = 1, ptr = list; i <= num_elems; i++, ptr += 2) {
                        subs = make_number(i);
-                       lhs = assoc_lookup(result, subs);
-                       unref(*lhs);
-                       *lhs = *ptr;
-                       if (result->astore != NULL)
-                               (*result->astore)(result, subs);
-                       unref(subs);
+                       assoc_set(result, subs, *ptr);
                }
        } else {
                /* We want the values of the source array. */
@@ -904,11 +899,11 @@ asort_actual(int nargs, sort_context_t ctxt)
                        /* value node */
                        r = *ptr++;
 
-                       if (r->type == Node_val) {
-                               lhs = assoc_lookup(result, subs);
-                               unref(*lhs);
-                               *lhs = dupnode(r);
-                       } else {
+                       NODE *value;
+
+                       if (r->type == Node_val)
+                               value = dupnode(r);
+                       else {
                                NODE *arr;
                                arr = make_array();
                                subs = force_string(subs);
@@ -917,13 +912,10 @@ asort_actual(int nargs, sort_context_t ctxt)
                                subs->stptr = NULL;
                                subs->flags &= ~STRCUR;
                                arr->parent_array = array; /* actual parent, 
not the temporary one. */
-                               lhs = assoc_lookup(result, subs);
-                               unref(*lhs);
-                               *lhs = assoc_copy(r, arr);
+
+                               value = assoc_copy(r, arr);
                        }
-                       if (result->astore != NULL)
-                               (*result->astore)(result, subs);
-                       unref(subs);
+                       assoc_set(result, subs, value);
                }
        }
 
diff --git a/awk.h b/awk.h
index d14eae2..378b935 100644
--- a/awk.h
+++ b/awk.h
@@ -2024,7 +2024,7 @@ make_number_node(unsigned int flags)
        return r;
 }
 
-/* assoc_set -- set an element in an array */
+/* assoc_set -- set an element in an array. Does unref(sub)! */
 
 static inline void
 assoc_set(NODE *array, NODE *sub, NODE *value)
diff --git a/debug.c b/debug.c
index f8aa6ad..f36a327 100644
--- a/debug.c
+++ b/debug.c
@@ -1250,6 +1250,7 @@ do_set_var(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
        {
                NODE *subs, *value;
                int count = arg->a_count;
+               NODE *newval;
 
                assert(count > 0);
                name = arg->a_string;
@@ -1268,11 +1269,10 @@ do_set_var(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
                                else {
                                        arg = arg->next;
                                        val = arg->a_node;
-                                       lhs = assoc_lookup(r, subs);
-                                       unref(*lhs);
-                                       *lhs = dupnode(val);
+                                       newval = dupnode(val);
+                                       assoc_set(r, dupnode(subs), newval);
                                        fprintf(out_fp, "%s[\"%.*s\"] = ", 
name, (int) subs->stlen, subs->stptr);
-                                       valinfo(*lhs, fprintf, out_fp);
+                                       valinfo(newval, fprintf, out_fp);
                                }
                        } else {
                                if (value == NULL) {
@@ -1280,9 +1280,7 @@ do_set_var(CMDARG *arg, int cmd ATTRIBUTE_UNUSED)
                                        array = make_array();
                                        array->vname = estrdup(subs->stptr, 
subs->stlen);
                                        array->parent_array = r;
-                                       lhs = assoc_lookup(r, subs);
-                                       unref(*lhs);
-                                       *lhs = array;
+                                       assoc_set(r, dupnode(subs), array);
                                        r = array;
                                } else if (value->type != Node_var_array) {
                                        d_error(_("attempt to use scalar 
`%s[\"%.*s\"]' as array"),
diff --git a/field.c b/field.c
index ccf47df..d8bb52b 100644
--- a/field.c
+++ b/field.c
@@ -949,18 +949,12 @@ static void
 set_element(long num, char *s, long len, NODE *n)
 {
        NODE *it;
-       NODE **lhs;
        NODE *sub;
 
        it = make_string(s, len);
        it->flags |= USER_INPUT;
        sub = make_number((AWKNUM) (num));
-       lhs = assoc_lookup(n, sub);
-       unref(*lhs);
-       *lhs = it;
-        if (n->astore != NULL)
-                (*n->astore)(n, sub);
-       unref(sub);
+       assoc_set(n, sub, it);
 }
 
 /* do_split --- implement split(), semantics are same as for field splitting */
@@ -1412,16 +1406,12 @@ current_field_sep_str()
 void
 update_PROCINFO_str(const char *subscript, const char *str)
 {
-       NODE **aptr;
        NODE *tmp;
 
        if (PROCINFO_node == NULL)
                return;
        tmp = make_string(subscript, strlen(subscript));
-       aptr = assoc_lookup(PROCINFO_node, tmp);
-       unref(tmp);
-       unref(*aptr);
-       *aptr = make_string(str, strlen(str));
+       assoc_set(PROCINFO_node, tmp, make_string(str, strlen(str)));
 }
 
 /* update_PROCINFO_num --- update PROCINFO[sub] with numeric value */
@@ -1429,16 +1419,12 @@ update_PROCINFO_str(const char *subscript, const char 
*str)
 void
 update_PROCINFO_num(const char *subscript, AWKNUM val)
 {
-       NODE **aptr;
        NODE *tmp;
 
        if (PROCINFO_node == NULL)
                return;
        tmp = make_string(subscript, strlen(subscript));
-       aptr = assoc_lookup(PROCINFO_node, tmp);
-       unref(tmp);
-       unref(*aptr);
-       *aptr = make_number(val);
+       assoc_set(PROCINFO_node, tmp, make_number(val));
 }
 
 /* set_FPAT --- handle an assignment to FPAT */
diff --git a/gawkapi.c b/gawkapi.c
index 3227d55..c76c7b5 100644
--- a/gawkapi.c
+++ b/gawkapi.c
@@ -1046,7 +1046,6 @@ api_set_array_element(awk_ext_id_t id, awk_array_t 
a_cookie,
        NODE *array = (NODE *)a_cookie;
        NODE *tmp;
        NODE *elem;
-       NODE **aptr;
 
        /* don't check for index len zero, null str is ok as index */
        if (   array == NULL
@@ -1058,17 +1057,13 @@ api_set_array_element(awk_ext_id_t id, awk_array_t 
a_cookie,
                return awk_false;
 
        tmp = awk_value_to_node(index);
-       aptr = assoc_lookup(array, tmp);
-       unref(*aptr);
-       elem = *aptr = awk_value_to_node(value);
+       elem = awk_value_to_node(value);
        if (elem->type == Node_var_array) {
                elem->parent_array = array;
                elem->vname = estrdup(index->str_value.str,
                                        index->str_value.len);
        }
-       if (array->astore != NULL)
-               (*array->astore)(array, tmp);
-       unref(tmp);
+       assoc_set(array, tmp, elem);
 
        return awk_true;
 }
diff --git a/interpret.h b/interpret.h
index 82329e8..53abadc 100644
--- a/interpret.h
+++ b/interpret.h
@@ -303,22 +303,15 @@ uninitialized_scalar:
                        if (r == NULL) {
                                r = make_array();
                                r->parent_array = t1;
-                               lhs = assoc_lookup(t1, t2);
-                               unref(*lhs);
-                               *lhs = r;
                                t2 = force_string(t2);
                                r->vname = estrdup(t2->stptr, t2->stlen);       
/* the subscript in parent array */
-
-                               /* execute post-assignment routine if any */
-                               if (t1->astore != NULL)
-                                       (*t1->astore)(t1, t2);
+                               assoc_set(t1, t2, r);
                        } else if (r->type != Node_var_array) {
                                t2 = force_string(t2);
                                fatal(_("attempt to use scalar `%s[\"%.*s\"]' 
as an array"),
                                                array_vname(t1), (int) 
t2->stlen, t2->stptr);
                        }
 
-                       DEREF(t2);
                        PUSH(r);
                        break;
 
diff --git a/main.c b/main.c
index 03a96f3..878e808 100644
--- a/main.c
+++ b/main.c
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 1986, 1988, 1989, 1991-2018 the Free Software Foundation, Inc.
+ * Copyright (C) 1986, 1988, 1989, 1991-2019 the Free Software Foundation, Inc.
  *
  * This file is part of GAWK, the GNU implementation of the
  * AWK Programming Language.
@@ -745,23 +745,19 @@ static void
 init_args(int argc0, int argc, const char *argv0, char **argv)
 {
        int i, j;
-       NODE **aptr;
-       NODE *tmp;
+       NODE *sub, *val;
 
        ARGV_node = install_symbol(estrdup("ARGV", 4), Node_var_array);
-       tmp =  make_number(0.0);
-       aptr = assoc_lookup(ARGV_node, tmp);
-       unref(tmp);
-       unref(*aptr);
-       *aptr = make_string(argv0, strlen(argv0));
-       (*aptr)->flags |= USER_INPUT;
+       sub = make_number(0.0);
+       val = make_string(argv0, strlen(argv0));
+       val->flags |= USER_INPUT;
+       assoc_set(ARGV_node, sub, val);
+
        for (i = argc0, j = 1; i < argc; i++, j++) {
-               tmp = make_number((AWKNUM) j);
-               aptr = assoc_lookup(ARGV_node, tmp);
-               unref(tmp);
-               unref(*aptr);
-               *aptr = make_string(argv[i], strlen(argv[i]));
-               (*aptr)->flags |= USER_INPUT;
+               sub = make_number((AWKNUM) j);
+               val = make_string(argv[i], strlen(argv[i]));
+               val->flags |= USER_INPUT;
+               assoc_set(ARGV_node, sub, val);
        }
 
        ARGC_node = install_symbol(estrdup("ARGC", 4), Node_var);
@@ -890,9 +886,8 @@ load_environ()
        extern char **environ;
 #endif
        char *var, *val;
-       NODE **aptr;
        int i;
-       NODE *tmp;
+       NODE *sub, *newval;
        static bool been_here = false;
 
        if (been_here)
@@ -910,12 +905,10 @@ load_environ()
                        *val++ = '\0';
                else
                        val = nullstr;
-               tmp = make_string(var, strlen(var));
-               aptr = assoc_lookup(ENVIRON_node, tmp);
-               unref(tmp);
-               unref(*aptr);
-               *aptr = make_string(val, strlen(val));
-               (*aptr)->flags |= USER_INPUT;
+               sub = make_string(var, strlen(var));
+               newval = make_string(val, strlen(val));
+               newval->flags |= USER_INPUT;
+               assoc_set(ENVIRON_node, sub, newval);
 
                /* restore '=' so that system() gets a valid environment */
                if (val != nullstr)
@@ -939,31 +932,32 @@ load_environ()
        return ENVIRON_node;
 }
 
+/* load_procinfo_argv --- populate PROCINFO["argv"] */
+
 static void
 load_procinfo_argv()
 {
-       NODE *tmp;
-       NODE **aptr;
+       NODE *sub;
+       NODE *val;
        NODE *argv_array;
        int i;
 
-       tmp = make_string("argv", 4);
-       aptr = assoc_lookup(PROCINFO_node, tmp);
-       unref(tmp);
-       unref(*aptr);
+       // build the sub-array first
        getnode(argv_array);
        memset(argv_array, '\0', sizeof(NODE));  /* valgrind wants this */
        null_array(argv_array);
-       *aptr = argv_array;
        argv_array->parent_array = PROCINFO_node;
        argv_array->vname = estrdup("argv", 4);
        for (i = 0; d_argv[i] != NULL; i++) {
-               tmp = make_number(i);
-               aptr = assoc_lookup(argv_array, tmp);
-               unref(tmp);
-               unref(*aptr);
-               *aptr = make_string(d_argv[i], strlen(d_argv[i]));
+               sub = make_number(i);
+               val = make_string(d_argv[i], strlen(d_argv[i]));
+               assoc_set(argv_array, sub, val);
        }
+
+       // hook it into PROCINFO
+       sub = make_string("argv", 4);
+       assoc_set(PROCINFO_node, sub, argv_array);
+
 }
 
 /* load_procinfo --- populate the PROCINFO array */
diff --git a/mpfr.c b/mpfr.c
index c3ba2a6..e171286 100644
--- a/mpfr.c
+++ b/mpfr.c
@@ -1262,16 +1262,10 @@ do_mpfr_intdiv(int nargs)
        unref(denominator);
 
        sub = make_string("quotient", 8);
-       lhs = assoc_lookup(result, sub);
-       unref(*lhs);
-       *lhs = quotient;
-       unref(sub);
+       assoc_set(result, sub, quotient);
 
        sub = make_string("remainder", 9);
-       lhs = assoc_lookup(result, sub);
-       unref(*lhs);
-       *lhs = remainder;
-       unref(sub);
+       assoc_set(result, sub, remainder);
 
        return make_number((AWKNUM) 0.0);
 }
diff --git a/str_array.c b/str_array.c
index 2fdd1bf..85e554b 100644
--- a/str_array.c
+++ b/str_array.c
@@ -121,7 +121,7 @@ str_array_init(NODE *symbol ATTRIBUTE_UNUSED, NODE *subs 
ATTRIBUTE_UNUSED)
 
 
 /*
- * assoc_lookup:
+ * str_lookup:
  * Find SYMBOL[SUBS] in the assoc array.  Install it with value "" if it
  * isn't there. Returns a pointer ala get_lhs to where its value is stored.
  *
diff --git a/symbol.c b/symbol.c
index fc598da..94d66fe 100644
--- a/symbol.c
+++ b/symbol.c
@@ -307,7 +307,6 @@ static NODE *
 install(const char *name, NODE *parm, NODETYPE type)
 {
        NODE *r;
-       NODE **aptr;
        NODE *table;
        NODE *n_name;
        NODE *prev;
@@ -353,14 +352,12 @@ install(const char *name, NODE *parm, NODETYPE type)
                        goto simple;
                r->dup_ent = prev->dup_ent;
                prev->dup_ent = r;
+               unref(n_name);
        } else {
 simple:
                /* the simple case */
-               aptr = assoc_lookup(table, n_name);
-               unref(*aptr);
-               *aptr = r;
+               assoc_set(table, n_name, r);
        }
-       unref(n_name);
 
        if (install_func)
                (*install_func)(r);

http://git.sv.gnu.org/cgit/gawk.git/commit/?id=fa9c6b921f784ad6f35f36b65635969ae4fade2d

commit fa9c6b921f784ad6f35f36b65635969ae4fade2d
Merge: 82a1b84 6f16c4f
Author: Arnold D. Robbins <address@hidden>
Date:   Tue Jan 15 19:54:42 2019 +0200

    Merge branch 'master' into feature/assoc_set


http://git.sv.gnu.org/cgit/gawk.git/commit/?id=82a1b8457331ee7fa439d67676ceb68cab017966

commit 82a1b8457331ee7fa439d67676ceb68cab017966
Author: Andrew J. Schorr <address@hidden>
Date:   Tue Jan 15 10:59:01 2019 -0500

    Remove some unused local variables in builtin.c.

diff --git a/ChangeLog b/ChangeLog
index ea1badb..fb96f1f 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2019-01-15         Andrew J. Schorr      <address@hidden>
 
+       * builtin.c (do_match, do_intdiv): Remove unused `sub' and `lhs'
+       variables, since assoc_set is now doing all of the work.
+
+2019-01-15         Andrew J. Schorr      <address@hidden>
+
        * awk.h (assoc_set): Move the definition lower down because it
        needs to be after unref is declared.
        * builtin.c (do_match): Use assoc_set in 3 places.
diff --git a/builtin.c b/builtin.c
index de8e3f7..e936b5a 100644
--- a/builtin.c
+++ b/builtin.c
@@ -2706,8 +2706,6 @@ do_match(int nargs)
                                if ((s = SUBPATSTART(rp, t1->stptr, ii)) != -1) 
{
                                        size_t subpat_start;
                                        size_t subpat_len;
-                                       NODE **lhs;
-                                       NODE *sub;
 
                                        start = t1->stptr + s;
                                        subpat_start = s;
@@ -3984,7 +3982,6 @@ do_intdiv(int nargs)
 {
        NODE *numerator, *denominator, *result;
        double num, denom, quotient, remainder;
-       NODE *sub, **lhs;
 
        result = POP_PARAM();
        if (result->type != Node_var_array)

http://git.sv.gnu.org/cgit/gawk.git/commit/?id=5812db74483bce781281422953d359fde5be6a99

commit 5812db74483bce781281422953d359fde5be6a99
Author: Andrew J. Schorr <address@hidden>
Date:   Tue Jan 15 10:51:40 2019 -0500

    Fix location of assoc_set function in awk.h and start to use it.

diff --git a/ChangeLog b/ChangeLog
index f90a00f..ea1badb 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2019-01-15         Andrew J. Schorr      <address@hidden>
+
+       * awk.h (assoc_set): Move the definition lower down because it
+       needs to be after unref is declared.
+       * builtin.c (do_match): Use assoc_set in 3 places.
+       (do_intdiv): Use assoc_set in 2 places.
+       (do_typeof): Use assoc_set in 2 places.
+
 2019-01-14         Andrew J. Schorr      <address@hidden>
 
        * awk.h (assoc_set): Add new inline function to set an array element
diff --git a/awk.h b/awk.h
index 9b5d359..d14eae2 100644
--- a/awk.h
+++ b/awk.h
@@ -1362,20 +1362,6 @@ extern int fatal_tag_valid;
 /* assoc_remove --- remove an index from symbol[] */
 #define assoc_remove(a, s) ((a)->aremove(a, s) != NULL)
 
-/* assoc_set -- set an element in an array */
-
-static inline void
-assoc_set(NODE *array, NODE *sub, NODE *value)
-{
-
-       NODE **lhs = assoc_lookup(array, sub);
-       unref(*lhs);
-       *lhs = value;
-       if (array->astore != NULL)
-               (*array->astore)(array, sub);
-       unref(sub);
-}
-
 
 /* ------------- Function prototypes or defs (as appropriate) ------------- */
 /* array.c */
@@ -2038,6 +2024,20 @@ make_number_node(unsigned int flags)
        return r;
 }
 
+/* assoc_set -- set an element in an array */
+
+static inline void
+assoc_set(NODE *array, NODE *sub, NODE *value)
+{
+
+       NODE **lhs = assoc_lookup(array, sub);
+       unref(*lhs);
+       *lhs = value;
+       if (array->astore != NULL)
+               (*array->astore)(array, sub);
+       unref(sub);
+}
+
 /*
  * str_terminate_f, str_terminate, str_restore: function and macros to
  * reduce chances of typos when terminating and restoring strings.
diff --git a/builtin.c b/builtin.c
index 9cf817a..de8e3f7 100644
--- a/builtin.c
+++ b/builtin.c
@@ -2719,15 +2719,7 @@ do_match(int nargs)
 
                                        it = make_string(start, len);
                                        it->flags |= USER_INPUT;
-
-                                       sub = make_number((AWKNUM) (ii));
-                                       lhs = assoc_lookup(dest, sub);
-                                       unref(*lhs);
-                                       *lhs = it;
-                                       /* execute post-assignment routine if 
any */
-                                       if (dest->astore != NULL)
-                                               (*dest->astore)(dest, sub);
-                                       unref(sub);
+                                       assoc_set(dest, make_number((AWKNUM) 
(ii)), it);;
 
                                        sprintf(buff, "%d", ii);
                                        ilen = strlen(buff);
@@ -2745,14 +2737,7 @@ do_match(int nargs)
 
                                        slen = ilen + subseplen + 5;
 
-                                       it = make_number((AWKNUM) subpat_start 
+ 1);
-                                       sub = make_string(buf, slen);
-                                       lhs = assoc_lookup(dest, sub);
-                                       unref(*lhs);
-                                       *lhs = it;
-                                       if (dest->astore != NULL)
-                                               (*dest->astore)(dest, sub);
-                                       unref(sub);
+                                       assoc_set(dest, make_string(buf, slen), 
make_number((AWKNUM) subpat_start + 1));
 
                                        memcpy(buf, buff, ilen);
                                        memcpy(buf + ilen, subsepstr, 
subseplen);
@@ -2760,14 +2745,7 @@ do_match(int nargs)
 
                                        slen = ilen + subseplen + 6;
 
-                                       it = make_number((AWKNUM) subpat_len);
-                                       sub = make_string(buf, slen);
-                                       lhs = assoc_lookup(dest, sub);
-                                       unref(*lhs);
-                                       *lhs = it;
-                                       if (dest->astore != NULL)
-                                               (*dest->astore)(dest, sub);
-                                       unref(sub);
+                                       assoc_set(dest, make_string(buf, slen), 
make_number((AWKNUM) subpat_len));
                                }
                        }
 
@@ -4044,15 +4022,9 @@ do_intdiv(int nargs)
 #endif /* ! HAVE_FMOD */
        remainder = double_to_int(remainder);
 
-       sub = make_string("quotient", 8);
-       lhs = assoc_lookup(result, sub);
-       unref(*lhs);
-       *lhs = make_number((AWKNUM) quotient);
+       assoc_set(result, make_string("quotient", 8), make_number((AWKNUM) 
quotient));
 
-       sub = make_string("remainder", 9);
-       lhs = assoc_lookup(result, sub);
-       unref(*lhs);
-       *lhs = make_number((AWKNUM) remainder);
+       assoc_set(result, make_string("remainder", 9), make_number((AWKNUM) 
remainder));
 
        DEREF(denominator);
        DEREF(numerator);
@@ -4085,15 +4057,8 @@ do_typeof(int nargs)
                /* Node_var_array is never UPREF'ed */
                res = "array";
                deref = false;
-               if (dbg) {
-                       NODE *sub = make_string("array_type", 10);
-                       NODE **lhs = assoc_lookup(dbg, sub);
-                       unref(*lhs);
-                       *lhs = make_string(arg->array_funcs->name, 
strlen(arg->array_funcs->name));
-                       if (dbg->astore != NULL)
-                               (*dbg->astore)(dbg, sub);
-                       unref(sub);
-               }
+               if (dbg)
+                       assoc_set(dbg, make_string("array_type", 10), 
make_string(arg->array_funcs->name, strlen(arg->array_funcs->name)));
                break;
        case Node_val:
                switch (fixtype(arg)->flags & (STRING|NUMBER|USER_INPUT|REGEX)) 
{
@@ -4124,13 +4089,7 @@ do_typeof(int nargs)
                }
                if (dbg) {
                        const char *s = flags2str(arg->flags);
-                       NODE *sub = make_string("flags", 5);
-                       NODE **lhs = assoc_lookup(dbg, sub);
-                       unref(*lhs);
-                       *lhs = make_string(s, strlen(s));
-                       if (dbg->astore != NULL)
-                               (*dbg->astore)(dbg, sub);
-                       unref(sub);
+                       assoc_set(dbg, make_string("flags", 5), make_string(s, 
strlen(s)));
                }
                break;
        case Node_var_new:

http://git.sv.gnu.org/cgit/gawk.git/commit/?id=69b4c6e0b70f92e829848e21c4d8983fac904f30

commit 69b4c6e0b70f92e829848e21c4d8983fac904f30
Author: Andrew J. Schorr <address@hidden>
Date:   Mon Jan 14 22:39:00 2019 -0500

    Add assoc_set inline function for setting array elements.

diff --git a/ChangeLog b/ChangeLog
index da93370..f90a00f 100755
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2019-01-14         Andrew J. Schorr      <address@hidden>
 
+       * awk.h (assoc_set): Add new inline function to set an array element
+       to eliminate code duplication and reduce the chance of memory leaks.
+
+2019-01-14         Andrew J. Schorr      <address@hidden>
+
        * builtin.c (do_typeof): Fix memory leak when populating the
        optional array, and make sure to call the astore method.
 
diff --git a/awk.h b/awk.h
index bac788b..9b5d359 100644
--- a/awk.h
+++ b/awk.h
@@ -1362,6 +1362,21 @@ extern int fatal_tag_valid;
 /* assoc_remove --- remove an index from symbol[] */
 #define assoc_remove(a, s) ((a)->aremove(a, s) != NULL)
 
+/* assoc_set -- set an element in an array */
+
+static inline void
+assoc_set(NODE *array, NODE *sub, NODE *value)
+{
+
+       NODE **lhs = assoc_lookup(array, sub);
+       unref(*lhs);
+       *lhs = value;
+       if (array->astore != NULL)
+               (*array->astore)(array, sub);
+       unref(sub);
+}
+
+
 /* ------------- Function prototypes or defs (as appropriate) ------------- */
 /* array.c */
 typedef enum { SORTED_IN = 1, ASORT, ASORTI } sort_context_t;

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog   | 39 +++++++++++++++++++++++++++++++++++++
 array.c     | 30 +++++++++++------------------
 awk.h       | 15 +++++++++++++++
 builtin.c   | 62 ++++++++---------------------------------------------------
 debug.c     | 16 +++++++++-------
 field.c     | 20 +++----------------
 gawkapi.c   |  9 ++-------
 interpret.h | 12 +++---------
 main.c      | 64 ++++++++++++++++++++++++++++---------------------------------
 mpfr.c      | 10 ++--------
 str_array.c |  2 +-
 symbol.c    |  7 ++-----
 12 files changed, 124 insertions(+), 162 deletions(-)


hooks/post-receive
-- 
gawk



reply via email to

[Prev in Thread] Current Thread [Next in Thread]