From 493fa9c39c40a86859970f3364db397f5e767a9f Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Mon, 12 Oct 2026 17:49:22 +0300
Subject: [PATCH 85/85] 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                  | 2 +-
 common/networking/conn_types.h | 4 ++--
 server/unithand.c              | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/common/city.c b/common/city.c
index a7b4473211..6f2546dae1 100644
--- a/common/city.c
+++ b/common/city.c
@@ -3770,7 +3770,7 @@ 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 %ld)",
               packet->length, MAX_LEN_ROUTE);
     return;
   }
diff --git a/common/networking/conn_types.h b/common/networking/conn_types.h
index 22788e4f0c..24b614a4ba 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 b57cd96a71..f1bd3e8ae7 100644
--- a/server/unithand.c
+++ b/server/unithand.c
@@ -6914,7 +6914,7 @@ 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 length %d (max %ld)", unit_rule_name(punit),
               packet->unit_id, length, MAX_LEN_ROUTE);
     return;
   }
-- 
2.53.0

