bug-bash
[Top][All Lists]
Advanced

[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."
```

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]