]> granicus.if.org Git - nethack/commitdiff
food prayer tweak
authorPatR <rankin@nethack.org>
Sun, 16 May 2021 23:42:20 +0000 (16:42 -0700)
committerPatR <rankin@nethack.org>
Sun, 16 May 2021 23:42:20 +0000 (16:42 -0700)
Guard against 16-bit int overflow when augmenting prayer timeout
for long running games.

src/pray.c

index f21e101ca22ff4845f910d3bfaf7f13584782fc9..6642e9678b9a6f38eec58599818ce7bc125d5166 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 pray.c  $NHDT-Date: 1596498198 2020/08/03 23:43:18 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.144 $ */
+/* NetHack 3.7 pray.c  $NHDT-Date: 1621208529 2021/05/16 23:42:09 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.147 $ */
 /* Copyright (c) Benson I. Margulies, Mike Stephenson, Steve Linhart, 1989. */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -1210,13 +1210,22 @@ pleased(aligntyp g_align)
     if (kick_on_butt)
         u.ublesscnt += kick_on_butt * rnz(1000);
 
-    /* Avoid games that go into infinite loops of copy-pasted commands with no
-       human interaction; this is a DoS vector against the computer running
-       NetHack. Once the turn counter is over 100000, every additional 100 turns
-       increases the prayer timeout by 1, thus eventually nutrition prayers will
-       fail and some other source of nutrition will be required. */
-    if (g.moves > 100000L)
-        u.ublesscnt += (g.moves - 100000L) / 100;
+    /* Avoid games that go into infinite loops of copy-pasted commands
+       with no human interaction; this is a DoS vector against the
+       computer running NetHack.  Once the turn counter is over 100000,
+       every additional 100 turns increases the prayer timeout by 1,
+       thus eventually hunger prayers will fail and some other source
+       of nutrition will be required.  The increase gets throttled if
+       it ever reaches 32K so that configurations using 16-bit ints are
+       still viable. */
+    if (g.moves > 100000L) {
+        long incr = (g.moves - 100000L) / 100L,
+             largest_ublesscnt_incr = (long) (LARGEST_INT - u.ublesscnt);
+
+        if (incr > largest_ublesscnt_incr)
+            incr = largest_ublesscnt_incr;
+        u.ublesscnt += (int) incr;
+    }
 
     return;
 }