From c77aafac75007032987a9c6c1f309c213859b67b Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Fri, 23 Oct 2026 23:00:40 +0300
Subject: [PATCH 92/92] Apply patch 2 for lua-5.5.1

See RM #2402

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 dependencies/lua-5.5/Version.txt | 2 +-
 dependencies/lua-5.5/src/lvm.c   | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/dependencies/lua-5.5/Version.txt b/dependencies/lua-5.5/Version.txt
index 708b9cffab..760821b08c 100644
--- a/dependencies/lua-5.5/Version.txt
+++ b/dependencies/lua-5.5/Version.txt
@@ -2,7 +2,7 @@ Sources here are from lua-5.5.1
 (http://www.lua.org/ftp/lua-5.5.1.tar.gz)
 
 Upstream bug fixes from https://www.lua.org/bugs.html applied:
-1
+1, 2
 
 Not entire lua distribution directory hierarchy is included here, and
 some files needed for Freeciv usage have been added.
diff --git a/dependencies/lua-5.5/src/lvm.c b/dependencies/lua-5.5/src/lvm.c
index 986c7db894..58320756ba 100644
--- a/dependencies/lua-5.5/src/lvm.c
+++ b/dependencies/lua-5.5/src/lvm.c
@@ -190,10 +190,11 @@ static int forlimit (lua_State *L, lua_Integer init, const TValue *lim,
       if (step < 0) return 1;  /* initial value must be less than it */
       *p = LUA_MAXINTEGER;  /* truncate */
     }
-    else {  /* it is less than min integer */
+    else if (luai_numgt(0, flim)) {  /* if it is negative, it is too small */
       if (step > 0) return 1;  /* initial value must be greater than it */
       *p = LUA_MININTEGER;  /* truncate */
     }
+    else return 1;  /* limit is NaN (neither 0 nor positive nor negative) */
   }
   return (step > 0 ? init > *p : init < *p);  /* not to run? */
 }
@@ -251,8 +252,8 @@ static int forprep (lua_State *L, StkId ra) {
       luaG_forerror(L, pinit, "initial value");
     if (step == 0)
       luaG_runerror(L, "'for' step is zero");
-    if (luai_numlt(0, step) ? luai_numlt(limit, init)
-                            : luai_numlt(init, limit))
+    if (!(luai_numge(0, step) ? luai_numge(init, limit)
+                              : luai_numge(limit, init)))
       return 1;  /* skip the loop */
     else {
       /* make sure all values are floats */
-- 
2.53.0

