Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SC2317 false positive warning #2542

Open
3 of 4 tasks
orbea opened this issue Jul 23, 2022 · 10 comments
Open
3 of 4 tasks

SC2317 false positive warning #2542

orbea opened this issue Jul 23, 2022 · 10 comments

Comments

@orbea
Copy link

orbea commented Jul 23, 2022

For bugs

  • Rule Id (if any, e.g. SC1000): SC2317, SC2317
  • My shellcheck version (shellcheck --version or 'online'): online
  • I tried on shellcheck.net and verified that this is still a problem on the latest commit
  • It's not reproducible on shellcheck.net, but I think that's because it's an OS, configuration or encoding issue

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/sh

set -e

quit () { err=$?; bar; }

trap 'quit; trap - EXIT; exit $err' EXIT INT

foo

exit 0

Here's what shellcheck currently says:

Line 3	SC2317: Command appears to be unreachable. Check usage (or ignore if invoked indirectly).
Line 3	SC2317: Command appears to be unreachable. Check usage (or ignore if invoked indirectly).

Here's what I wanted or expected to see:

This should not be a warning, the exit 0 at the end of a script is very common and the quit function is indeed reachable due to the trap. In this case the exit 0 may never be reached if the foo command fails for whatever reason and even if the exit 0 is reached the quit function which will have any cleanup is still activated due to the trap.

@dimo414
Copy link
Contributor

dimo414 commented Oct 24, 2022

It's the exit 0 at the end of the script that appears to be triggering this finding. Removing that line clears the "unreachable" finding. Here's a smaller example:

#!/bin/sh
foo() { bar; }
exit 0  # comment this out to do away with the finding

I disagree with your assertion that it's very common" to end a script with exit 0 - it's basically a no-op, and should rarely be needed, especially when using set -e. So a simple fix would be to remove that line from your script. The wiki mentions this:

Defined functions are assumed to be reachable when the script ends (not exits) since another file may source and invoke them.

That said, I agree this finding is confusing and at a minimum it should point to the exit line and explain how it's coming to this reachability conclusion.

@orbea
Copy link
Author

orbea commented Oct 24, 2022

So a simple fix would be to remove that line from your script.

You'll need to remove it from thousands of scripts.In other words that is not a reasonable solution.

@jjakob
Copy link

jjakob commented Mar 5, 2023

@dimo414 a trap on EXIT will be invoked even with exit 0, so there's no reason to say that function will not be reached.

@dimo414
Copy link
Contributor

dimo414 commented Mar 5, 2023

I agree that the finding is wrong, I'm just saying exit 0 isn't needed and removing that line appears to make the finding go away.

@orbea
Copy link
Author

orbea commented Mar 5, 2023

I'm just saying exit 0 isn't needed and removing that line appears to make the finding go away.

As already explained this is clearly not the solution.

@oliv3r
Copy link

oliv3r commented Mar 26, 2023

I disagree with your assertion that it's very common" to end a script with exit 0 - it's basically a no-op, and should rarely be needed, especially when using set -e.

I diagree with your assertion too :)

The final exit 0 at the end, is your canary. E.g. if your script walks its normal life, and exists normally via that exit, it will prevent any attacks such as:

echo "rm -rf /" >> script.sh

so its simply a best practice to always do it.

Having said that, you are correct that technically it is not needed.

@qneill-sifive
Copy link

qneill-sifive commented Mar 27, 2023

I agree "removing you exit 0" might be useful to avoid this particular shellcheck behavior, but it's not a good argument for asserting that shellcheck shouldn't handle it differently.

Explicitly using exit VALUE is a useful programming idiom. My script hit this problem with this stanza:

if has_some_error_condition; then
    print_error_report
    exit 1
else
    print_success_report
    exit 0
fi

@foresto
Copy link

foresto commented Jul 10, 2023

Aliases confuse it, too.

#!/bin/sh
foo() { echo "message"; }
alias bar=foo
true  # or some other command
RESULT=$?
if [ "$RESULT" -eq 0 ]; then bar; fi
exit "$RESULT"

My actual code is more complex, of course, with conditional aliases for multiple functions. Shellcheck 0.9.0 bombards me with "unreachable" messages for every line of every function. I don't remember this happening when I used 0.7.1 (though it's possible I wasn't using shellcheck on these scripts back then).

Also please note that the exit at the end is not a no-op.

@wileyhy
Copy link

wileyhy commented Jul 11, 2023 via email

@ringerc
Copy link

ringerc commented Jul 18, 2023

It can't seem to cope with traps like

function premature_exit() {
    echo 1>&2 "Unexpected premature exit from entrypoint script at $0:$line"
}

trap 'line=${BASH_LINENO:-};premature_exit' EXIT

# ... do work ...
errcode=$?

# clear trap
trap EXIT

# exit with errcode propagated from inner workload
exit $errcode

robn added a commit to robn/zfs that referenced this issue Jul 21, 2023
This new check in 0.9.0 appears to have some issues with various forms
of "early return", like trap, exit and return. This is tripping up (at
least):

  cmd/zed/zed.d/history_event-zfs-list-cacher.sh
  /etc/zfs/zfs-functions

Its not obvious what its complaining about or what the remedy is, so it
seems sensible to disable this check for now.

See also:

  https://www.shellcheck.net/wiki/SC2317
  koalaman/shellcheck#2542
  koalaman/shellcheck#2613

Signed-off-by: Rob Norris <robn@despairlabs.com>
behlendorf pushed a commit to openzfs/zfs that referenced this issue Jul 21, 2023
This new check in 0.9.0 appears to have some issues with various forms
of "early return", like trap, exit and return. This is tripping up (at
least):

  cmd/zed/zed.d/history_event-zfs-list-cacher.sh
  /etc/zfs/zfs-functions

Its not obvious what its complaining about or what the remedy is, so it
seems sensible to disable this check for now.

See also:

  https://www.shellcheck.net/wiki/SC2317
  koalaman/shellcheck#2542
  koalaman/shellcheck#2613

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes #15089
behlendorf pushed a commit to behlendorf/zfs that referenced this issue Jul 21, 2023
This new check in 0.9.0 appears to have some issues with various forms
of "early return", like trap, exit and return. This is tripping up (at
least):

  cmd/zed/zed.d/history_event-zfs-list-cacher.sh
  /etc/zfs/zfs-functions

Its not obvious what its complaining about or what the remedy is, so it
seems sensible to disable this check for now.

See also:

  https://www.shellcheck.net/wiki/SC2317
  koalaman/shellcheck#2542
  koalaman/shellcheck#2613

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes openzfs#15089
behlendorf pushed a commit to openzfs/zfs that referenced this issue Jul 21, 2023
This new check in 0.9.0 appears to have some issues with various forms
of "early return", like trap, exit and return. This is tripping up (at
least):

  cmd/zed/zed.d/history_event-zfs-list-cacher.sh
  /etc/zfs/zfs-functions

Its not obvious what its complaining about or what the remedy is, so it
seems sensible to disable this check for now.

See also:

  https://www.shellcheck.net/wiki/SC2317
  koalaman/shellcheck#2542
  koalaman/shellcheck#2613

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes #15089
lundman pushed a commit to openzfsonwindows/openzfs that referenced this issue Dec 12, 2023
This new check in 0.9.0 appears to have some issues with various forms
of "early return", like trap, exit and return. This is tripping up (at
least):

  cmd/zed/zed.d/history_event-zfs-list-cacher.sh
  /etc/zfs/zfs-functions

Its not obvious what its complaining about or what the remedy is, so it
seems sensible to disable this check for now.

See also:

  https://www.shellcheck.net/wiki/SC2317
  koalaman/shellcheck#2542
  koalaman/shellcheck#2613

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Rob Norris <robn@despairlabs.com>
Closes openzfs#15089
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants