lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This was first mentioned on the mailing list in 2004, but the bug still exists today :
print( os.execute("exit 1") )
256

The reason this is bad, is running os.exit(os.execute(cmd)) will always return 0, as only the lowest 8 bits of the exit code are used. 

The cause is the os_execute function is returning the full return value of the call to system(), without processing it for the exit code. The fix is :

static int os_execute (lua_State *L) {
  int rv = system(luaL_optstring(L, 1, NULL));
  int exitCode = 0;
  if (WIFEXITED(rv)) {
 exitCode = WEXITSTATUS(rv);
  }
  lua_pushinteger(L, exitCode);
  return 1;
}
(based on Lua 5.1.4, adapt slightly for Lua 5.2.1 which checks for a shell)