memory leak in case of "read -e"

2017-02-22 Thread ZhangXiao
Hi Chet and experts,

Build in command "read" with "-e" option causes memory leak. Can be
reproduced with below script and lots of input data.

$cat script.sh
#!/bin/bash/bash
while [ 1 ];
do
read -e data;
echo $data;
done
$./script.sh


The patch at the end of this mail can fix it.

Thanks
Xiao

diff --git a/builtins/read.def b/builtins/read.def
index 33821f3..60bdc87 100644
--- a/builtins/read.def
+++ b/builtins/read.def
@@ -690,6 +690,11 @@ add_char:
   input_string[i] = '\0';
   CHECK_ALRM;

+#if defined (READLINE)
+  if (edit)
+xfree (rlbuf);
+#endif
+
   if (retval < 0)
 {
   t_errno = errno;



[PATCH] fix for loop assignments to nameref control vars

2017-02-22 Thread Grisha Levit
The special-case handling of namerefs as for loop control variables is
missing checks for the readonly attribute on the nameref and allows
creation of forbidden nameref self-references.

$ declare -rn ref=a
$ for ref in b; { :; }
$ declare -p ref
declare -nr ref="b"

$ declare -n ref
$ for ref in ref; { declare -p ref; }
declare -n ref="ref"


---
 execute_cmd.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/execute_cmd.c b/execute_cmd.c
index feccf7c7..71bbfb4e 100644
--- a/execute_cmd.c
+++ b/execute_cmd.c
@@ -2800,8 +2800,10 @@ execute_for_command (for_command)
  sh_invalidid (list->word->word);
  v = 0;
}
+ else if (readonly_p (v))
+   err_readonly (name_cell (v));
  else
-   v = bind_variable_value (v, list->word->word, 0);
+   v = bind_variable_value (v, list->word->word, ASS_NAMEREF);
}
   else
v = bind_variable (identifier, list->word->word, 0);
--


[PATCH] Add nosort,noquote support to complete and compopt

2017-02-22 Thread Grisha Levit
These are currently missing from the compspec/compopt displays:

$ complete -o noquote -o nospace -E
$ complete -p -E
complete -o nospace -E
$ compopt -E | grep -ow 'no\w*'
nospace


---
 builtins/complete.def | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/builtins/complete.def b/builtins/complete.def
index 6bdf88b4..407fa348 100644
--- a/builtins/complete.def
+++ b/builtins/complete.def
@@ -515,6 +515,8 @@ print_one_completion (cmd, cs)
   PRINTCOMPOPT (COPT_DEFAULT, "default");
   PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
   PRINTCOMPOPT (COPT_FILENAMES, "filenames");
+  PRINTCOMPOPT (COPT_NOQUOTE, "noquote");
+  PRINTCOMPOPT (COPT_NOSORT, "nosort");
   PRINTCOMPOPT (COPT_NOSPACE, "nospace");
   PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");

@@ -589,6 +591,8 @@ print_compopts (cmd, cs, full)
   XPRINTCOMPOPT (COPT_DEFAULT, "default");
   XPRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
   XPRINTCOMPOPT (COPT_FILENAMES, "filenames");
+  XPRINTCOMPOPT (COPT_NOQUOTE, "noquote");
+  XPRINTCOMPOPT (COPT_NOSORT, "nosort");
   XPRINTCOMPOPT (COPT_NOSPACE, "nospace");
   XPRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
 }
@@ -598,6 +602,8 @@ print_compopts (cmd, cs, full)
   PRINTCOMPOPT (COPT_DEFAULT, "default");
   PRINTCOMPOPT (COPT_DIRNAMES, "dirnames");
   PRINTCOMPOPT (COPT_FILENAMES, "filenames");
+  PRINTCOMPOPT (COPT_NOQUOTE, "noquote");
+  PRINTCOMPOPT (COPT_NOSORT, "nosort");
   PRINTCOMPOPT (COPT_NOSPACE, "nospace");
   PRINTCOMPOPT (COPT_PLUSDIRS, "plusdirs");
 }
--


Re: [PATCH] Add nosort,noquote support to complete and compopt

2017-02-22 Thread Chet Ramey
On 2/22/17 8:27 AM, Grisha Levit wrote:
> These are currently missing from the compspec/compopt displays:

Good catch; thanks for the patch.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: memory leak in case of "read -e"

2017-02-22 Thread Chet Ramey
On 2/22/17 2:33 AM, ZhangXiao wrote:
> Hi Chet and experts,
> 
> Build in command "read" with "-e" option causes memory leak. Can be
> reproduced with below script and lots of input data.
> 
> $cat script.sh
> #!/bin/bash/bash
> while [ 1 ];
> do
> read -e data;
> echo $data;
> done
> $./script.sh
> 
> 
> The patch at the end of this mail can fix it.

Thanks for the report and fix.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/



Re: [PATCH] fix for loop assignments to nameref control vars

2017-02-22 Thread Chet Ramey
On 2/22/17 6:47 AM, Grisha Levit wrote:
> The special-case handling of namerefs as for loop control variables is
> missing checks for the readonly attribute on the nameref and allows
> creation of forbidden nameref self-references.

Thanks for the report and fix.

Chet
-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/