Re: $(( )): binary/unary VAR/NUM inconsistency

2022-07-09 Thread Steffen Nurpmeso
Well i considered this one done upstream.

Martin D Kealey wrote in
 :
 |On Sat, 9 Jul 2022, 02:08 Chet Ramey,  wrote:
 |> On 7/8/22 11:03 AM, Steffen Nurpmeso wrote:
 |>
 |>> So you seem to use your own itoa, and here is (another) bash bug.
 ...
 |>> In this hindsight bash should bail with syntax error due to 0x:
 ...
 |I would urge extreme caution in removing functionality like this. I've had

Yeah!! ..but the thing is that all libraries and programs agreed
in changing (at least to the best of my knowledge).
So except for possibly missing backports the thing is in the world
since, when it was, i think 2017 or 2018.

 |the nightmare of finding that Bash removed functionality that I relied on,
 |because a group collectively lacked the imagination to see how it might be
 |used in ways that were neither bugs nor impediments to maintenance.
 |
 |Some might think that changing the expression parser so that a bare "0x" is
 |rejected would be harmless at worst, and helpful against writing bugs. They
 |would be wrong. Writing "0x$var" is an established if uncommon idiom that

Not for me. :)  Not for many standard developers (the called kre
even?), who push set -u.

 |currently exhibits the same "empty is zero" behaviour as we get when
 |writing "var" without a radix prefix.

You are really wrong here, and sit on non-portable code!  You
need "#!/usr/bin/env bash" for it.  (I do not know about zsh.)
Or condomize via ": ${var:=0}" maybe?

 |But my real point is that I shouldn't have to come here and make that
 |point; rather, avoiding breaking changes, even "in the name of making
 |scripts less buggy", should be the standing policy.

Oh the first part is what i must have thought by then, but "they
changed interpretation" of the ISO C standard from one day to
another, and many jumped on this train and agreed with it, so you
(i, that is) really could only follow suit.

Sigh.  Now i am looking for half an hour and cannot find the
discussion(s).  (I thought oss-security, but .. no.)  So i looked
around in the code repositories i track (mostly BSDs), and i see
for example on OpenBSD

  Commit: millert 
  CommitDate: 2017-07-06 16:23:11 +

  The 0x (or 0X) prefix in base 16 is optional so only skip over the
  prefix if the character following it is a valid hex char.  The C99
  standard is clear that given the string "0xy" zero should be returned
  and endptr set to point to the "x".  OK deraadt@ espie@

FreeBSD was fixed in 2005 already.  NetBSD also on 2017-07-06:

  CommitDate: 2017-07-06 21:08:44 +

  Fix ISO C compliance: strtol of "0xX" should give the largest valid
  numeric prefix, which is 0.

DragonFly is not yet fixed.  Hm.

It also had to be quick i think; i thought a bit about it and
later added a special flags for my "integer codec":

 /*! Relaxed \a{base} 0 convenience: if the input used \c{BASE#number} number
  * sign syntax, then the scan will be restarted anew with the base given.
  * Like this an UI can be permissive and support \c{s='  -008'; eval 10#\$s}
  * out of the box (it would require a lot of logic otherwise). */
 su_IDEC_MODE_BASE0_NUMBER_SIGN_RESCAN = 1u<<2,

Documented as "The number sign notation uses a permissive parse
mode and as such supports complicated conditions out of the box:",
followed by the practically same example.  But not 0x.

 |-Martin
 |
 |PS: For those who wonder what change I'm referring to…
 |
 |The change to "break" and "continue" so that they would refuse to act on a
 |loop outside the current function being a case in point. Apparently
 |"nobody" could see how the previous behaviour might be useful, other than
 |"to write bugs", and "everyone" thought that all possible uses must
 |necessarily be inadvertent bugs, and so the feature was removed.
 |
 |But that change broke all my my "break_when_xxx" functions, and with them
 |my tab completion function for "ip", which of necessity comprises a fairly
 |complex stateful parser. In particular, its core is a case statement with
 |hundreds of cases and they all need the same conditional break when
 |reaching the word being expanded, so it made sense to encapsulate that in a
 |well-named function.
 |
 |And I didn't find out until my OS updated its version of Bash several years
 |after that change to Bash itself, by which time it was way too late to
 |unwind the change to Bash and I had to rewrite large swathes of code to
 |cope with the installed base of Bash.

--steffen
|
|Der Kragenbaer,The moon bear,
|der holt sich munter   he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Re: $(( )): binary/unary VAR/NUM inconsistency

2022-07-09 Thread Chet Ramey

On 7/8/22 5:05 PM, Robert Elz wrote:

 Date:Fri, 8 Jul 2022 12:08:38 -0400
 From:Chet Ramey 
 Message-ID:  

   | This is where folks like kre are going to argue.

Am I?   I was keeping out of this one!


It was a different thread discussing the same subject where I saw your
comment about stricter token parsing.



About the only other thing I'd say (more than 30 years too late) is that
even when the ++ and -- operators weren't implemented, the tokenizer
really should always have recognised them


So they would always have been an error? What's the benefit of that? If
you're not going to implement pre/post inc/dec, why should ++10 be an
error instead of two unary plus operators?

--
``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: Revisiting Error handling (errexit)

2022-07-09 Thread Yair Lenga
Hi Martin,

Long answer - my own view: The old 'errexit' logic was spec'ed many years
ago. As far as I can tell, it existed in bash 2.0, from 1996. I think
requirements/expectations are different now. The 'exit on error' error
handling was good for 1996 - does not meet today's requirement - using bash
to glue complex systems together. For that reason, I do not think that
replicating the errexit small details is a good strategy. In fact, I think
that most developers are looking for something that is closer to the "try
... catch" that they have in other environments. The 'errfail' is my
proposal to get there, without introducing lsignificant changes, or risk.

Re: command prefaced by ! which is important:
* The '!' operator 'normal' behavior is to reverse the exit status of a
command ('if ! check-something ; then ...').
* I do not think it's a good idea to change the meaning of '!' when running
with 'error checking'.
* I think that the existing structures ('|| true', or '|| :') to force
success status are good enough and well understood by beginner and advanced
developers.

Question: What is the penalty for "|| true" ? true is bash builtin, and in
bash it's equivalent to ':'. (see:
https://unix.stackexchange.com/questions/34677/what-is-the-difference-between-and-true
)

Re: yet another global setting is perpetuating the "wrong direction".
I prefer not to get into "flame war" on the issue of dynamic scope vs.
lexical scope. Most other scripting solutions that I'm familiar with are
using dynamic (rather than lexical) scoping for 'try ... catch.'.
Considering that bash is stable, I do not think that it is realistic to try
to expect major changes to 'bash'. Also, Bash code was developed with
specific design/requirements in mind. It can not be easily stretched to
integrate new ideas - sometimes it is better to go for practical solutions
than for the best solution. There is a big range of applications where bash
can play an important role. For a more sophisticated environment, python,
groovy, javascript or (your favorite choice) might be a better solution.

Question: Out of curiosity, can you share your idea for a better solution ?

Thanks for taking the time !
Yair


Sent from my iPad

On Jul 8, 2022, at 3:31 PM, Martin D Kealey  wrote:


The old errexit explicitly exempts any command prefaced by ! which is
important so that one can write ! (( x++ )) without it blowing up when x is
0 and without paying the penalty for  "|| true".
Does this new proposal honour that?

Aside from that, I still think that yet another global setting is
perpetuating the "wrong direction"; "local -" has the same dynamic scope as
any other "local", which means magic action at a difference, and makes
brittle code that can abort unexpectedly in production.

-Martin