Skip to content

Commit

Permalink
'luaL_execresult' does not assume -1 status as error
Browse files Browse the repository at this point in the history
ISO C is silent about the return of 'system'. Windows sets 'errno' in
case of errors. Linux has several different error cases, with different
return values. ISO C allows 'system' to set 'errno' even if there are no
errors. Here we assume that a status==0 is success (which is the case
on several platforms), otherwise it is an error. If there is an error
number, gives the error based on it. (The worst a spurious 'errno'
can do is to generate a bad error message.) Otherwise uses the normal
results.
  • Loading branch information
roberto-ieru committed May 22, 2020
1 parent 17dbaa8 commit efcf24b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lauxlib.c
Expand Up @@ -284,7 +284,7 @@ LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {

LUALIB_API int luaL_execresult (lua_State *L, int stat) {
const char *what = "exit"; /* type of termination */
if (stat == -1) /* error? */
if (stat != 0 && errno != 0) /* error with an 'errno'? */
return luaL_fileresult(L, 0, NULL);
else {
l_inspectstat(stat, what); /* interpret result */
Expand Down
1 change: 1 addition & 0 deletions liolib.c
Expand Up @@ -270,6 +270,7 @@ static int io_open (lua_State *L) {
*/
static int io_pclose (lua_State *L) {
LStream *p = tolstream(L);
errno = 0;
return luaL_execresult(L, l_pclose(L, p->f));
}

Expand Down
6 changes: 4 additions & 2 deletions loslib.c
Expand Up @@ -10,6 +10,7 @@
#include "lprefix.h"


#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -138,10 +139,11 @@




static int os_execute (lua_State *L) {
const char *cmd = luaL_optstring(L, 1, NULL);
int stat = system(cmd);
int stat;
errno = 0;
stat = system(cmd);
if (cmd != NULL)
return luaL_execresult(L, stat);
else {
Expand Down

0 comments on commit efcf24b

Please sign in to comment.