[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Interesting bug
From: |
David Hobach |
Subject: |
Re: Interesting bug |
Date: |
Sat, 12 Feb 2022 19:24:05 +0100 |
I guess 99% of programmers would either expect "Finished" to be printed or some
syntax error.
Well, 99% of shell programmers will (hopefully ;-) ) put a blank between "{" and
"echo" in the line
foo="$(testCode)" || {echo "foo";}
Yes, the interesting part is that depending on which space you accidentally forget, you'll either
get the expected "Finished" or "bad code executed".
foo="$(testCode)" || {echo "foo"; } # --> bad code
foo="$(testCode)" || { echo "foo";} # --> Finished
I guess it closes the function and {echo is interpreted as string or so, but
that is probably not all (testCode is e.g. never executed).
A syntax error would be nice instead.
Shellcheck at least gets this unless you do something weird and more obvious
such as
```
#!/bin/bash
function badCode {
echo "bad code executed"
}
function testCode {
#pick some existing file
echo "/etc/passwd"
}
function tfunc {
local foo=
foo="$(testCode)" || "{echo" "foo";}
cat "$foo" || {
badCode
case $? in
*)
exit 1
esac
}
echo "Finished."
```
It's also interesting that this - in contrast to the original example -
triggers a syntax error:
```
#!/bin/bash
function badCode {
echo "bad code executed"
}
function testCode {
#pick some existing file
echo "/etc/passwd"
}
function tfunc {
local foo=
foo="$(testCode)" || {echo "foo";}
cat "$foo" || {
badCode
case $? in
*)
exit 1
esac
} } #<-- only difference to the original example
echo "Finished."
```
smime.p7s
Description: S/MIME Cryptographic Signature
- Interesting bug, David Hobach, 2022/02/12
- Re: Interesting bug, Martin Schulte, 2022/02/12
- Re: Interesting bug,
David Hobach <=
- Re: Interesting bug, David Hobach, 2022/02/12
- Re: Interesting bug, Andreas Schwab, 2022/02/12
- Re: Interesting bug, Martin Schulte, 2022/02/12
- Re: Interesting bug, Ilkka Virta, 2022/02/12
- Re: Interesting bug, David Hobach, 2022/02/12
- Re: Interesting bug, Chet Ramey, 2022/02/13
- Re: Interesting bug, Chet Ramey, 2022/02/13
Re: Interesting bug, Léa Gris, 2022/02/12