git: 4f055dad0314 - main - games/xnethack: fix behaviour with mismatched lua version

From: Li-Wen Hsu <lwhsu_at_FreeBSD.org>
Date: Sat, 25 Jun 2022 14:29:41 UTC
The branch main has been updated by lwhsu:

URL: https://cgit.FreeBSD.org/ports/commit/?id=4f055dad0314f34264ccc320846b49340bd27dd0

commit 4f055dad0314f34264ccc320846b49340bd27dd0
Author:     Robert Clausecker <fuz@fuz.su>
AuthorDate: 2022-06-25 04:58:18 +0000
Commit:     Li-Wen Hsu <lwhsu@FreeBSD.org>
CommitDate: 2022-06-25 14:28:19 +0000

    games/xnethack: fix behaviour with mismatched lua version
    
    The latest version of the package has introduced a "lua sandbox" that
    comes with a version check against the bundled lua version.
    
    As we unbundle lua, we cannot ensure that this version check succeeds.
    Removal of the version check allows us to build a working binary, but
    it is uncertain if there are any residual effects from the version
    mismatch.
    
    To rule out that possibility, the sandbox code is left disabled by
    default but can be enabled if desired through the SANDBOX option.
    
    PR:             264882
---
 games/xnethack/Makefile                     |  9 ++++++-
 games/xnethack/files/patch-include-config.h |  9 +++++++
 games/xnethack/files/patch-src_nhlua.c      | 40 +++++++++++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/games/xnethack/Makefile b/games/xnethack/Makefile
index b5665934f18e..7f621cab17ae 100644
--- a/games/xnethack/Makefile
+++ b/games/xnethack/Makefile
@@ -1,6 +1,7 @@
 PORTNAME=	xNetHack
 DISTVERSIONPREFIX=	xnh
 DISTVERSION=	7.1
+PORTREVISION=	1
 CATEGORIES=	games
 
 MAINTAINER=	fuz@fuz.su
@@ -49,7 +50,13 @@ GRAPHICS=	QT_GRAPHICS
 
 CFLAGS+=	-DNOMAIL -DCURSES_GRAPHICS
 
-OPTIONS_DEFINE=	DOCS
+# nhlua.c has a (patched out) version check for lua when the sandbox
+# is used.  As I cannot ascertain whether using the system's different
+# lua version could cause problems, I'll leave the sandbox disabled by
+# default.
+OPTIONS_DEFINE=	DOCS SANDBOX
+SANDBOX_DESC=	Run lua in a sandbox (untested)
+SANDBOX_CFLAGS=	-DNHL_SANDBOX
 
 .include <bsd.port.pre.mk>
 
diff --git a/games/xnethack/files/patch-include-config.h b/games/xnethack/files/patch-include-config.h
index 26f78d8821aa..8be26e04924d 100644
--- a/games/xnethack/files/patch-include-config.h
+++ b/games/xnethack/files/patch-include-config.h
@@ -34,3 +34,12 @@
  /* DUMPLOG_FILE allows following placeholders:
     %% literal '%'
     %v version (eg. "3.6.3-0")
+@@ -697,7 +697,7 @@ typedef unsigned char uchar;
+ 
+ /* TEMPORARY - MAKE UNCONDITIONAL BEFORE RELEASE */
+ /* undef this to check if sandbox breaks something */
+-#define NHL_SANDBOX
++/* #define NHL_SANDBOX */
+ 
+ /* End of Section 4 */
+ 
diff --git a/games/xnethack/files/patch-src_nhlua.c b/games/xnethack/files/patch-src_nhlua.c
new file mode 100644
index 000000000000..4df225a11834
--- /dev/null
+++ b/games/xnethack/files/patch-src_nhlua.c
@@ -0,0 +1,40 @@
+As we unbundle lua, we cannot ensure that this version check succeeds.
+Removal of the version check allows us to build a working binary, but
+it is uncertain if there are any residual effects from the version
+mismatch.
+
+To rule out that possibility, the sandbox code is left disabled by
+default but can be enabled if desired through the SANDBOX option.
+
+--- src/nhlua.c.orig	2022-06-25 04:31:43 UTC
++++ src/nhlua.c
+@@ -1652,20 +1652,6 @@ DISABLE_WARNING_CONDEXPR_IS_CONSTANT
+ lua_State *
+ nhl_init(nhl_sandbox_info *sbi)
+ {
+-	/* It would be nice to import EXPECTED from each build system. XXX */
+-	/* And it would be nice to do it only once, but it's cheap. */
+-#ifndef NHL_VERSION_EXPECTED
+-#define NHL_VERSION_EXPECTED 50404
+-#endif
+-
+-#ifdef NHL_SANDBOX
+-    if(NHL_VERSION_EXPECTED != LUA_VERSION_RELEASE_NUM){
+-	panic(
+-	    "sandbox doesn't know this Lua version: this=%d != expected=%d ",
+-	    LUA_VERSION_RELEASE_NUM, NHL_VERSION_EXPECTED);
+-    }
+-#endif
+-
+     lua_State *L = nhlL_newstate(sbi);
+ 
+     iflags.in_lua = TRUE;
+@@ -2348,7 +2334,7 @@ nhlL_newstate (nhl_sandbox_info *sbi) {
+     }
+ 
+     lua_State *L = lua_newstate(nhl_alloc, nud);
+-#if LUA_VERSION_NUM == 503
++#ifndef luai_likely
+ # define luai_likely(x) (x)
+ #endif
+     if (luai_likely(L)) {