Re: Memory Leak - Bash 4.3.x30

2024-03-27 Thread Chet Ramey

On 3/26/24 1:33 PM, Jordi Ferrer wrote:

Just in case for people running old servers:

This simple code cause a memory leak in bash:

#!/bin/bash
#
Array=("S" "E")
while [ 1 ]; do
A=${Array[0]}
done


This was fixed by patch 40 to bash-4.3; if you're running a fully-patched
version you won't encounter it.

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




Re: Docco

2024-03-27 Thread Chet Ramey

On 3/27/24 7:40 AM, Andreas Schwab wrote:


The check for whether the first argument is '!' is not performed,
because the "$2 is a binary primary" check comes first.  This is how
POSIX documents it.


FWIW, ksh parses it the other way round:

$ ksh93 -c '[ ! -a /tmp ]; echo $?; [ . -a /tmp ]; echo $?; [ - -a /tmp ]; echo 
$?'
1
0
ksh93: [: -: unknown operator
2


Bash uses the order POSIX specifies. -a and -o are binary primaries when
test gets three arguments.

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




Re: Docco

2024-03-27 Thread Chet Ramey

On 3/27/24 5:50 AM, Phi Debian wrote:


Ok may be my wording is not correct, but yet it require a good reading
compile to get it right, first read all about [[ that is at the top of the
man (at 5%), then get the 'CONDITIONAL EXPRESSIONS' distingo between [[ vs
[ (at 40%) and finally get to 'test expr' (at 92%) to discover the whole
thing about -a vs -e (same for other options), a little heads up when quick
reading directly at 'CONDITIONAL EXPRESSIONS' would not hurt and not
jeopardise the docco semantic I guess?


This is from the first paragraph of CONDITIONAL EXPRESSIONS:

"The test and [  commands  determine  their
 behavior  based  on  the  number  of arguments; see the descriptions of
 those commands for any other command-specific actions."

The texinfo manual has internal cross-references; the man page format is
not that flexible.

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




Re: Docco

2024-03-27 Thread Chet Ramey

On 3/27/24 5:00 AM, Phi Debian wrote:

$ man bash
...
CONDITIONAL EXPRESSIONS
...

-a file
   True if file exists.
-e file
   True if file exists.
...

'May be' would be nice for newbies to precise which options are [ specific
vs [[ specific for instance


All the unary and binary primaries described there are available with both
`[[' and `test'.



-a file
   True if file exists ([[ only, for [ see test builtin)


This would not be accurate. `-a' is deprecated but still accepted as a
unary primary.



This to avoid things like

$ [   -a /tmp ] && echo ok || echo nok
ok
$ [ ! -a /tmp ] && echo ok || echo nok
ok


`test' works on the number of arguments, so these can be expected to be
different.


I know it is obvious, unless this is intended to force a complete
multi-pass man read...


The [[ and test descriptions document the differences.

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




Re: Docco

2024-03-27 Thread Andreas Schwab
On Mär 27 2024, Greg Wooledge wrote:

>> $ [ ! -a /tmp ] && echo ok || echo nok
>> ok
>
> Here, you have three arguments, and argument 2 is a "binary primary"
> (POSIX wording again), so it's treated as if you had written this:
>
> [ ! ] && [ /tmp ] && echo ok || echo nok
>
> This is simply performing two string length tests.  Both strings are
> non-empty (the first is one character, and the second is four), so
> the result is true.
>
> The check for whether the first argument is '!' is not performed,
> because the "$2 is a binary primary" check comes first.  This is how
> POSIX documents it.

FWIW, ksh parses it the other way round:

$ ksh93 -c '[ ! -a /tmp ]; echo $?; [ . -a /tmp ]; echo $?; [ - -a /tmp ]; echo 
$?' 
1
0
ksh93: [: -: unknown operator
2

-- 
Andreas Schwab, SUSE Labs, sch...@suse.de
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."



Re: Docco

2024-03-27 Thread Greg Wooledge
On Wed, Mar 27, 2024 at 10:00:06AM +0100, Phi Debian wrote:
> $ man bash
> ...
> CONDITIONAL EXPRESSIONS
> ...
> 
>-a file
>   True if file exists.
>-e file
>   True if file exists.
> ...
> 
> 'May be' would be nice for newbies to precise which options are [ specific
> vs [[ specific for instance
> 
>-a file
>   True if file exists ([[ only, for [ see test builtin)
> 
> This to avoid things like
> 
> $ [   -a /tmp ] && echo ok || echo nok
> ok
> $ [ ! -a /tmp ] && echo ok || echo nok
> ok
> 
> I know it is obvious, unless this is intended to force a complete
> multi-pass man read...

I wouldn't say it's "obvious" what's happening here.  The problem is
that there are two different "-a" operators, one unary, and one binary.
"help test" documents both of them:

hobbit:~$ help test | grep -- -a
  -a FILETrue if file exists.
  EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.

In your first example, you are using the unary -a operator on a file:

> $ [   -a /tmp ] && echo ok || echo nok
> ok

This one returns true because there are two arguments, and the first one
is not '!', and is a "unary primary" (POSIX wording).  Therefore it uses
the unary  -a and tests for existence of the second argument as a file.

In your second example, you are using the binary -a operator:

> $ [ ! -a /tmp ] && echo ok || echo nok
> ok

Here, you have three arguments, and argument 2 is a "binary primary"
(POSIX wording again), so it's treated as if you had written this:

[ ! ] && [ /tmp ] && echo ok || echo nok

This is simply performing two string length tests.  Both strings are
non-empty (the first is one character, and the second is four), so
the result is true.

The check for whether the first argument is '!' is not performed,
because the "$2 is a binary primary" check comes first.  This is how
POSIX documents it.

So... how do you work around this?  Well, the easiest way would be
to stop using -a entirely.  Both the unary *and* binary forms.  The
unary form can be replaced by -e, and then everything works as you
expect.  The binary form should be discarded along with "-o", and
never used.  You are much better off stringing together multiple
test or [ commands instead:

if [ -e "$logdir" ] && [ -e "$outputdir" ]; then ...

This removes all ambiguity, and is in fact the only supported way
to write this under POSIX restrictions (">4 arguments: The results
are unspecified.")



Re: Docco

2024-03-27 Thread Phi Debian
Interestingly, the ksh docco say that 'Conditional Expressions' applies to
[[ only :-) and then say the -a is obsolete.

test expression
later says
  the -a and -o binary oper-
  ators can be used, but they are fraught  with  pitfalls  due
 to
  grammatical ambiguities and therefore deprecated in favor of
in-
  voking separate test commands

Ok let's forget about this...


Re: Docco

2024-03-27 Thread Phi Debian
On Wed, Mar 27, 2024 at 10:28 AM Andreas Kähäri 
wrote:

> On Wed, Mar 27, 2024 at 10:00:06AM +0100, Phi Debian wrote:
> > $ man bash
>
> Would it not be untrue to say that "-a" is specific to "[[", as it is
> clearly not the case?  The fact that it is easy to confuse the two is
> a different matter, but the documentation is correct for the current
> implementation (which mimics the ksh shell with regards to the unary
> "-a" operator).
>
> --
> Andreas (Kusalananda) Kähäri
> Uppsala, Sweden
>
> .

Ok may be my wording is not correct, but yet it require a good reading
compile to get it right, first read all about [[ that is at the top of the
man (at 5%), then get the 'CONDITIONAL EXPRESSIONS' distingo between [[ vs
[ (at 40%) and finally get to 'test expr' (at 92%) to discover the whole
thing about -a vs -e (same for other options), a little heads up when quick
reading directly at 'CONDITIONAL EXPRESSIONS' would not hurt and not
jeopardise the docco semantic I guess?


Re: Docco

2024-03-27 Thread Andreas Kähäri
On Wed, Mar 27, 2024 at 10:00:06AM +0100, Phi Debian wrote:
> $ man bash
> ...
> CONDITIONAL EXPRESSIONS
> ...
> 
>-a file
>   True if file exists.
>-e file
>   True if file exists.
> ...
> 
> 'May be' would be nice for newbies to precise which options are [ specific
> vs [[ specific for instance
> 
>-a file
>   True if file exists ([[ only, for [ see test builtin)
> 
> This to avoid things like
> 
> $ [   -a /tmp ] && echo ok || echo nok
> ok
> $ [ ! -a /tmp ] && echo ok || echo nok
> ok
> 
> I know it is obvious, unless this is intended to force a complete
> multi-pass man read...

Would it not be untrue to say that "-a" is specific to "[[", as it is
clearly not the case?  The fact that it is easy to confuse the two is
a different matter, but the documentation is correct for the current
implementation (which mimics the ksh shell with regards to the unary
"-a" operator).

-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.



Docco

2024-03-27 Thread Phi Debian
$ man bash
...
CONDITIONAL EXPRESSIONS
...

   -a file
  True if file exists.
   -e file
  True if file exists.
...

'May be' would be nice for newbies to precise which options are [ specific
vs [[ specific for instance

   -a file
  True if file exists ([[ only, for [ see test builtin)

This to avoid things like

$ [   -a /tmp ] && echo ok || echo nok
ok
$ [ ! -a /tmp ] && echo ok || echo nok
ok

I know it is obvious, unless this is intended to force a complete
multi-pass man read...