-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On Sat, Feb 12, 2022 at 05:31:04PM +0100, HW42 wrote:
> Brendan Hoar:
> > On Sat, Feb 12, 2022 at 7:03 AM David Hobach <[email protected]> wrote:
> > 
> >> Dear all,
> >>
> >> just stumbled across it and was wondering what a reviewer would
> >> expect from this code to do:
> >>
> >> ```
> >> #!/bin/bash
> >>
> >> function badCode {
> >> echo "bad code executed"
> >> }
> >>
> >> function testCode {
> >> #pick some existing file, nonexisting works too though
> >> echo "/etc/passwd"
> >> }
> >>
> >> function tfunc {
> >>    local foo=
> >>    foo="$(testCode)" || {echo "foo";}
> >>    cat "$foo" || {
> >>          badCode
> >>          case $? in
> >>           *)
> >>             exit 1
> >>          esac
> >> }
> >> }
> >>
> >> echo "Finished."
> >> ```
> >>
> >> At least on my amchine it executes "badCode" in both domU and dom0. I
> >> guess it's a bash bug and reported it accordingly, but anyway...
> > 
> > 
> > I’m not exactly sure what’s going on (and can’t bash right now) but
> > output grouping requires white space around { and } so that’s at least
> > one problem on the foo= assignment line. Maybe it’s being interpreted
> > as closing the function definition early?
> 
> Yes you are on the right track here, I think. One problem is the missing
> space after the opening {
> 
>     $ false || {echo "foo"; 
>     bash: {echo: command not found
>     $ false || {echo "foo";}
>     bash: syntax error near unexpected token `}'
>     $ 
> 
> So the } after the "foo"; closes the function. But why don't we get an
> error for the later } that don't match? Because bash operates line by
> line (remember those self extracting archives, that are shell scripts
> followed by a tar). If you change the *) into, say, x) (or anything that
> doesn't match $?) you see it:
> 
>     $ ./t2
>     cat: '': No such file or directory
>     bad code executed
>     ./t2: line 22: syntax error near unexpected token `}'
>     ./t2: line 22: `}'
>     $ 
> 
> So this is probably not even a bug. Thanks for the nice example David
> (apropos shell: set -e semantics are also "fun").
> 
> Simon

I consider this a good reason to not use shell scripts for anything
non-trivial.  There are some scripts in the builder which definitely
violate this rule.  Also, `bash -n` will catch this.

- -- 
Sincerely,
Demi Marie Obenour (she/her/hers)
Invisible Things Lab
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEdodNnxM2uiJZBxxxsoi1X/+cIsEFAmIIIgoACgkQsoi1X/+c
IsGQbQ//VtA3XGI5Cqcpa9sBSsChICtoHKq7m/qz2Fu3D/CDyAs0sNVrtq+ujLD8
1Mk/di/nfUzv7aBM6DBJoaaXMsmNpgQcx41aDM/AA7W+2MGKSPwVlcYYbxO7VsST
nQWEv3me4JRevvWNrQQUcPdkzV/alFSehKWfR05oHEAb7QMSIpwQs19YEprfRWV4
i6Dv1PTVSARTYry42Do9NT7lUi7YF786JpqV9H3/vkJOhf4IWm7Hb8CBasYykF1A
h6B2gx66PoiwK+qnJJesxoM8ePGhpvCjCw272FNy2TGuxeDa/zUYzWgkCgQNeUI/
dyEqmbDT410n4wDZtsHVvQLvoNYPGYjCMH0KoX95QJt/12ufzewPqaOTh2GvHr0w
hcIETVL02D5fgC0Rgt23SpB0aGJVQ13NM9nTi7Vy3kyhYv2N8/j5pQQpYszlLC2I
QOLmFDbPnaCvRU8BrhA4oQ6jaAZJNrKUtysOuQz3RJnXKV5kwNla9ys7b2ykCdJ5
oFWhxYgwk0HKuaFwpAbyJ+pXsb2Nq8rHYZrnzH4JKDUYX95SCCHcv4tSs+zcUpnW
D1a+PvR6NUSQUbncbt0eJq9UCyCioA2KlwZzfVf7iLdmdJvICB/kIszeDwt4BBQO
Lpjx1vFj4f+Db4zHi3ATOx9JazVxS6Lm7sCKgSnJcxUtzx9DGUI=
=iCAe
-----END PGP SIGNATURE-----

-- 
You received this message because you are subscribed to the Google Groups 
"qubes-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/qubes-devel/YggiCuECbgco8%2BGl%40itl-email.

Reply via email to