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

Add File::BadExecutableError #13491

Conversation

HertzDevil
Copy link
Contributor

@HertzDevil HertzDevil commented May 22, 2023

This PR maps Errno::ENOEXEC and WinError::ERROR_BAD_EXE_FORMAT to a new exception class, File::BadExecutableError, that is raised if Process.new receives a file that has execute permissions but isn't really an executable file. WinError::ERROR_BAD_EXE_FORMAT passes the file argument to LibC.FormatMessageW. Fixes #12985. (The new exception class isn't technically necessary, I just think this is a good chance to capture this error in a platform-independent way.)

Note that this doesn't happen naturally on POSIX systems, where Process.new is backed by LibC.execvp; if execvp failed because of ENOEXEC, execution of the same file is attempted again via sh as though it is a shell script. Therefore, trying to execute a Windows executable on a non-WSL Linux produces:

begin
  p Process.run("/home/quinton/crystal/crystal/crystal.exe", output: :inherit, error: :inherit)
rescue ex
  p ex
end
/home/quinton/crystal/crystal/crystal.exe: 1: MZ����@▒��: not found
/home/quinton/crystal/crystal/crystal.exe: 2: Syntax error: word unexpected (expecting ")")
Process::Status[2]

However, musl is known not to fall back to sh, in which case File::BadExecutableError could still be caught. If we want to use LibC.execv instead then we must replicate the $PATH lookup ourselves when the file name does not include a slash character.

This PR also maps WinError::ERROR_ACCESS_DENIED to File::AccessDeniedError. This happens when Process.new receives a directory on Windows.

@HertzDevil HertzDevil added kind:bug A bug in the code. Does not apply to documentation, specs, etc. kind:feature topic:stdlib:system labels May 22, 2023
@straight-shoota straight-shoota added this to the 1.9.0 milestone May 22, 2023
@straight-shoota straight-shoota merged commit 9b97e84 into crystal-lang:master May 23, 2023
@HertzDevil HertzDevil deleted the feature/file-bad-executable-error branch May 24, 2023 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. kind:feature topic:stdlib:system
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Empty error message when running non-executable file on Windows
2 participants