bug-bash
[Top][All Lists]
Advanced

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

[PATCH] asort: some bug fixes


From: Emanuele Torre
Subject: [PATCH] asort: some bug fixes
Date: Sun, 26 Mar 2023 08:19:25 +0200

- add some missing free()

- avoid calling xmalloc(0)

- fix  f () { local b; asort -i b a ;}  not updating b if b is declared
  but doesn't have a value using builtin_find_indexed_array()
  instead of find_or_make_array_variable():

    bash-5.2$ typeset a=(z y x) b c=1; asort -i b a; asort -i c a
    bash-5.2$ typeset -p a b c
    declare -a a=([0]="z" [1]="y" [2]="x")
    declare -a b
    declare -a c=([0]="2" [1]="1" [2]="0")

    bash-5.2$ typeset a=(z y x) b c=1; asort -i b a; asort -i c a
    bash-5.2$ typeset -p a b c
    declare -a a=([0]="z" [1]="y" [2]="x")
    declare -a b=([0]="2" [1]="1" [2]="0")
    declare -a c=([0]="2" [1]="1" [2]="0")

- make  asort -i a a  set a to an empty array instead of saving garbage
  in a. This is achieve by flushing the destination passing the 1 flag
  to builtin_find_indexed_array() array, before sorting the source
  array; because of that, the find_variable() call for the source array,
  has been moved before the builtin_find_indexed_array() call for the
  destination array to avoid flushing the destination array
  unnecessarily if the source array is not valid:

    bash-5.2$ a=( z y x ); asort -i a a; typeset -p a
    declare -a a=([0]="-3472328296227680305" [1]="4049355305727046445" [2]="1")

    bash-5.2$ a=( z y x ); asort -i a a; typeset -p a
    declare -a a=()
---
 examples/loadables/asort.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/examples/loadables/asort.c b/examples/loadables/asort.c
index 6570c9af..c9ece326 100644
--- a/examples/loadables/asort.c
+++ b/examples/loadables/asort.c
@@ -72,7 +72,8 @@ sort_index(SHELL_VAR *dest, SHELL_VAR *source)
     if (assoc_p(source)) {
         hash = assoc_cell(source);
         n = hash->nentries;
-        sa = xmalloc(n * sizeof(sort_element));
+        if (n)
+          sa = xmalloc (n * sizeof(sort_element));
         i = 0;
         for ( j = 0; j < hash->nbuckets; ++j ) {
             bucket = hash->bucket_array[j];
@@ -91,7 +92,8 @@ sort_index(SHELL_VAR *dest, SHELL_VAR *source)
     else {
         array = array_cell(source);
         n = array_num_elements(array);
-        sa = xmalloc(n * sizeof(sort_element));
+        if (n)
+          sa = xmalloc (n * sizeof(sort_element));
         i = 0;
 
         for (ae = element_forw(array->head); ae != array->head; ae = 
element_forw(ae)) {
@@ -107,13 +109,12 @@ sort_index(SHELL_VAR *dest, SHELL_VAR *source)
     // sanity check
     if ( i != n ) {
         builtin_error("%s: corrupt array", source->name);
+        xfree (sa);
         return EXECUTION_FAILURE;
     }
 
     qsort(sa, n, sizeof(sort_element), compare);
 
-    array_flush(dest_array);
-
     for ( i = 0; i < n; ++i ) {
         if ( assoc_p(source) )
             key = sa[i].key;
@@ -123,6 +124,7 @@ sort_index(SHELL_VAR *dest, SHELL_VAR *source)
         array_insert(dest_array, i, key);
     }
 
+    xfree (sa);
     return EXECUTION_SUCCESS;
 }
 
@@ -155,6 +157,7 @@ sort_inplace(SHELL_VAR *var)
     // sanity check
     if ( i != n ) {
         builtin_error("%s: corrupt array", var->name);
+        xfree (sa);
         return EXECUTION_FAILURE;
     }
 
@@ -220,14 +223,14 @@ asort_builtin(WORD_LIST *list)
             sh_invalidid (list->next->word->word);
             return EXECUTION_FAILURE;
         }
-        var = find_or_make_array_variable(list->word->word, 1);
-        if (var == 0)
-            return EXECUTION_FAILURE;
         var2 = find_variable(list->next->word->word);
         if ( !var2 || ( !array_p(var2) && !assoc_p(var2) ) ) {
             builtin_error("%s: Not an array", list->next->word->word);
             return EXECUTION_FAILURE;
         }
+        var = builtin_find_indexed_array (list->word->word, 1);
+        if (var == 0)
+            return EXECUTION_FAILURE;
         return sort_index(var, var2);
     }
 
-- 
2.40.0




reply via email to

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