[BUG] os.execute reports wrong exit code on Windows

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[BUG] os.execute reports wrong exit code on Windows

Thijs Schreijer

This exists since Lua 5.2. The culprit is the exit code on Windows having a different format than on unix/posix. The latter has 0-255 as common ground and mostly anything else is undefined. But on Windows the exit code is a 32bit signed integer.

Now this code: https://github.com/lua/lua/blob/f59e6a93c0ad38a27a420e51abf8f13d962446b5/lauxlib.c#L276-L277
Treats -1 as a special case, and goes looking for an error message here: https://github.com/lua/lua/blob/f59e6a93c0ad38a27a420e51abf8f13d962446b5/lauxlib.c#L241-L247

This results in “No error” in the windows case of -1, since it is a valid exit code.

Test output (the actual exit code used, followed by the 3 results of os.execute):
-6 nil exit -6
-5 nil exit -5
-4 nil exit -4
-3 nil exit -3
-2 nil exit -2
-1 nil No error 0
0 true exit 0
1 nil exit 1
2 nil exit 2
3 nil exit 3
4 nil exit 4
5 nil exit 5

On Windows the -1 case should have been “-1 nil exit -1”

regards
Thijs

Reply | Threaded
Open this post in threaded view
|

Re: [BUG] os.execute reports wrong exit code on Windows

Roberto Ierusalimschy
>
> This exists since Lua 5.2. The culprit is the exit code on Windows having a different format than on unix/posix. The latter has 0-255 as common ground and mostly anything else is undefined. But on Windows the exit code is a 32bit signed integer.
>
> Now this code: https://github.com/lua/lua/blob/f59e6a93c0ad38a27a420e51abf8f13d962446b5/lauxlib.c#L276-L277
> Treats -1 as a special case, and goes looking for an error message here: https://github.com/lua/lua/blob/f59e6a93c0ad38a27a420e51abf8f13d962446b5/lauxlib.c#L241-L247
>
> This results in “No error” in the windows case of -1, since it is a valid exit code.

(Sorry for such a late reply...)

I don't understand the problem here. The documentation for 'system' in
Windows says this:

  https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2017

  A return value of -1 indicates an error, and errno is set to one of the
  following values: [...]

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: [BUG] os.execute reports wrong exit code on Windows

Thijs Schreijer


On 30 Jan 2019, at 16:11, Roberto Ierusalimschy <[hidden email]> wrote:


This exists since Lua 5.2. The culprit is the exit code on Windows having a different format than on unix/posix. The latter has 0-255 as common ground and mostly anything else is undefined. But on Windows the exit code is a 32bit signed integer.

Now this code: https://github.com/lua/lua/blob/f59e6a93c0ad38a27a420e51abf8f13d962446b5/lauxlib.c#L276-L277
Treats -1 as a special case, and goes looking for an error message here: https://github.com/lua/lua/blob/f59e6a93c0ad38a27a420e51abf8f13d962446b5/lauxlib.c#L241-L247

This results in “No error” in the windows case of -1, since it is a valid exit code.

(Sorry for such a late reply...)

I don't understand the problem here. The documentation for 'system' in
Windows says this:

 https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/system-wsystem?view=vs-2017

 A return value of -1 indicates an error, and errno is set to one of the
 following values: […]

It’s in the preceding line: 
> It returns the value 0 only if the command interpreter returns the value 0. A return
> value of - 1 indicates an error, and errno is set to one of the following values:

And this makes the result of -1 ambiguous, because both an error and a -1 return value from the command result in a -1 result.

I think the proper approach would be to reset `errno` before the `system` call and then check for: `stat` being -1, AND `errno` being non-0.

Thijs


-- Roberto