Feature #8083

Exit status is limited to one-byte values which is invalid for Windows

Added by Vladimir Rutsky about 1 year ago. Updated about 1 year ago.

[ruby-core:53368]
Status:Assigned
Priority:Normal
Assignee:cruby-windows
Category:platform/windows
Target version:-

Description

=begin
Windows uses 32-bit process exit codes so Ruby incorrectly truncates them to one byte:

C:\Ruby193\bin>ruby -e "system('C:\windows\system32\cmd.exe /c exit 255'); puts $?.exitstatus"
255

C:\Ruby193\bin>ruby -e "system('C:\windows\system32\cmd.exe /c exit 256'); puts $?.exitstatus"
0

C:\Ruby193\bin>ruby -e "system('C:\windows\system32\cmd.exe /c exit 257'); puts $?.exitstatus"
1

Similar code works correctly in Python:

C:\Python27>python -c "import subprocess; print subprocess.call('C:\windows\system32\cmd.exe /c exit 255')"
255

C:\Python27>python -c "import subprocess; print subprocess.call('C:\windows\system32\cmd.exe /c exit 256')"
256

C:\Python27>python -c "import subprocess; print subprocess.call('C:\windows\system32\cmd.exe /c exit 257')"
257
=end

History

#1 Updated by Luis Lavena about 1 year ago

  • Status changed from Open to Feedback

Can you provide links to MSDN article that states this? (32-bits exit codes)

Thank you.

#2 Updated by Heesob Park about 1 year ago

According to the document http://en.wikipedia.org/wiki/Exit_status#Windows,
"Windows uses 32-bit signed integers as exit codes. If a process fails initialization, a Windows system error code may be returned. Windows system error codes are available online.
Exit codes are directly referenced, for example, by the command line interpreter CMD.exe in the errorlevel terminology inherited from DOS. .NET Framework processes and the Windows PowerShell refer to it as the ExitCode property of the Process object."

Refer to
http://stackoverflow.com/questions/179565/exitcodes-bigger-than-255-possible
http://www.hiteksoftware.com/mize/Knowledge/articles/049.htm

#3 Updated by Usaku NAKAMURA about 1 year ago

  • Tracker changed from Bug to Feature
  • Status changed from Feedback to Assigned

This may be not a bug but a feature.

#4 Updated by Vladimir Rutsky about 1 year ago

=begin
I cannot find exact definition of possible exit codes on Windows, but here is ExitProcess function declaration [1]:

VOID WINAPI ExitProcess(
In UINT uExitCode
);

UINT type definition [2]:

An unsigned INT. The range is 0 through 4294967295 decimal.

GetExitCodeProcess declaration 3:

BOOL WINAPI GetExitCodeProcess(
In HANDLE hProcess,
Out LPDWORD lpExitCode
);

LPDWORD type definition [4]:

typedef DWORD *LPDWORD;

DWORD type definition [5]:

A 32-bit unsigned integer. The range is 0 through 4294967295 decimal.

Not sure about all Windows platforms, but at least on Windows 7 error codes in full 32-bit range can be retrieved in practice:

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(30))"

C:\Python27>echo %errorlevel%
30

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(2**31 - 1))"

C:\Python27>echo %errorlevel%
2147483647

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(2**31))"

C:\Python27>echo %errorlevel%
-2147483648

C:\Python27>python.exe -c "import ctypes, ctypes.wintypes; ctypes.windll.kernel32.ExitProcess(ctypes.wintypes.UINT(2**32 - 1))"

C:\Python27>echo %errorlevel%
-1

[1] http://msdn.microsoft.com/en-us/library/windows/desktop/ms682658%28v=vs.85%29.aspx

[2] http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx#UINT

[3] http://msdn.microsoft.com/en-us/library/windows/desktop/ms683189%28v=vs.85%29.aspx

[4] http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx#LPDWORD

[5] http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx#DWORD
=end

Also available in: Atom PDF