OSDN Git Service

[Fix] 数の指定で受け付けない入力値がある
authorshimitei <shimitei@gmail.com>
Wed, 16 Jun 2021 10:49:47 +0000 (19:49 +0900)
committershimitei <shimitei@gmail.com>
Thu, 17 Jun 2021 11:22:43 +0000 (20:22 +0900)
符号付32ビット整数を符号付16ビット整数としてキャストするため、例えば101010の入力をマイナスの値として扱う不整合が出ていた。
入力値の制限を0から符号付16ビット整数の最大値までとすることで不整合を回避する。

src/core/asking-player.cpp

index 0b86ace..998481a 100644 (file)
@@ -14,6 +14,9 @@
 #include "util/string-processor.h"
 #include "view/display-messages.h"
 
+#include <climits>
+#include <algorithm>
+
 /*
  * Get some string input at the cursor location.
  * Assume the buffer is initialized to a default string.
@@ -340,6 +343,9 @@ bool get_com(concptr prompt, char *command, bool z_escape)
  */
 QUANTITY get_quantity(concptr prompt, QUANTITY max)
 {
+    // FIXME : QUANTITY、COMMAND_CODE、その他の型サイズがまちまちな変数とのやり取りが多数ある。この処理での数の入力を0からSHRT_MAXに制限することで不整合の発生を回避する。
+    max = std::clamp<QUANTITY>(max, 0, SHRT_MAX);
+
     bool res;
     char tmp[80];
     char buf[80];
@@ -386,14 +392,12 @@ QUANTITY get_quantity(concptr prompt, QUANTITY max)
     if (!res)
         return 0;
 
-    amt = (COMMAND_CODE)atoi(buf);
-    if (isalpha(buf[0]))
-        amt = max;
-    if (amt > max)
+   if (isalpha(buf[0]))
         amt = max;
-    if (amt < 0)
-        amt = 0;
-    if (amt)
+    else
+        amt = std::clamp<int>(atoi(buf), 0, max);
+
+   if (amt)
         repeat_push((COMMAND_CODE)amt);
 
     return (amt);