From 8e76a5a0649bc20ae294e6c45ed34048169e79f1 Mon Sep 17 00:00:00 2001 From: Kp Date: Sun, 27 Dec 2020 22:03:09 +0000 Subject: [PATCH] Avoid gcc snprintf truncation warning in net_udp.cpp gcc computes a potential value range for game times as [0, 1092] instead of the [0, 50] that the game uses. This could be reasonable as the code was before, but even adding an explicit range check before the usage does not eliminate the warning. Avoid the warning by increasing the size of the buffer to avoid truncation even if the value were 1092. --- similar/main/net_udp.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/similar/main/net_udp.cpp b/similar/main/net_udp.cpp index 635ced2fc..5775a37d8 100644 --- a/similar/main/net_udp.cpp +++ b/similar/main/net_udp.cpp @@ -3500,8 +3500,18 @@ protected: const unsigned game_is_cooperative; char packstring[sizeof("99")]; std::array portstring; - char srinvul[sizeof("Reactor life: 50 min")]; - char PlayText[sizeof("Max time: 50 min")]; + /* Reactor life and Maximum time are stored in a uint32_t, and have + * a theoretical maximum of 1092 after converting from internal game + * time (seconds in fixed point) to minutes. User input limitations + * prevent setting a value higher than 50 minutes. Even if the code + * is modified to verify a maximum value of 50 immediately before + * formatting it, the gcc value range propagation pass fails to + * detect the reduced range and issues a warning as if the value + * could be 1092. Eliminate the bogus warning by using a buffer + * large enough for the theoretical maximum. + */ + char srinvul[sizeof("Reactor life: 1092 min")]; + char PlayText[sizeof("Max time: 1092 min")]; char SpawnInvulnerableText[sizeof("Invul. Time: 0.0 sec")]; char SecludedSpawnText[sizeof("Use 0 Furthest Sites")]; char KillText[sizeof("Kill goal: 000 kills")];