From ba22b8d34ff1ac0e86e403ecc5b01596a95870ef Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Mon, 12 Oct 2026 17:49:22 +0300
Subject: [PATCH 26/28] Stop attempts to send too long goto routes to the
 server

Client was trying to send overlong goto routes over
the network, resulting in assert failures and lost connection.
This was because MAX_LEN_ROUTE had far too big a value (2000).
Change MAX_LEN_VALUE to be mostly dynamically determined,
for related packets to fit in MAX_LEN_PACKET.
Currently the resulting MAX_LEN_ROUTE is 165.

Reported by Eelis Vuokko

See RM #2366

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 common/city.c                  | 3 ++-
 common/networking/conn_types.h | 4 ++--
 server/unithand.c              | 5 +++--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/common/city.c b/common/city.c
index 818b92cfdd..6063e7063f 100644
--- a/common/city.c
+++ b/common/city.c
@@ -3667,7 +3667,8 @@ void city_rally_point_receive(const struct packet_city_rally_point *packet,
 
   if (0 > packet->length || MAX_LEN_ROUTE < packet->length) {
     /* Shouldn't happen */
-    log_error("city_rally_point_receive() invalid packet length %d (max %d)",
+    log_error("city_rally_point_receive() invalid packet length %d (max "
+              SIZE_T_PRINTF ")",
               packet->length, MAX_LEN_ROUTE);
     return;
   }
diff --git a/common/networking/conn_types.h b/common/networking/conn_types.h
index 22788e4f0c..93b69e6c03 100644
--- a/common/networking/conn_types.h
+++ b/common/networking/conn_types.h
@@ -34,8 +34,8 @@ extern "C" {
 #define MAX_LEN_BUFFER   (MAX_LEN_PACKET * 128)
 
 /* Used in network protocol. */
-#define MAX_LEN_MSG             1536
-#define MAX_LEN_ROUTE           2000      /* MAX_LEN_PACKET / 2 - header */
+#define MAX_LEN_MSG       1536
+#define MAX_LEN_ROUTE   ((MAX_LEN_CONTENT - 100) / sizeof(struct unit_order))
 
 /* Used in network protocol. */
 enum authentication_type {
diff --git a/server/unithand.c b/server/unithand.c
index e4ed94dae7..c60b23136f 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -6630,8 +6630,9 @@ void handle_unit_orders(struct player *pplayer,
   if (0 > length || MAX_LEN_ROUTE < length) {
     /* Shouldn't happen */
     log_error("handle_unit_orders() invalid %s (%d) "
-              "packet length %d (max %d)", unit_rule_name(punit),
-              packet->unit_id, length, MAX_LEN_ROUTE);
+              "packet length %d (max " SIZE_T_PRINTF ")",
+              unit_rule_name(punit), packet->unit_id, length,
+              MAX_LEN_ROUTE);
     return;
   }
 
-- 
2.53.0

