shithub: moonfish

Download patch

ref: 01a9a23796629848e5049fe78143f32e890f06f7
parent: 6fc07cae22dc42b10c497a7bfc84b73815735713
author: zamfofex <zamfofex@twdb.moe>
date: Wed Feb 7 15:14:11 EST 2024

avoid using sscanf

--- a/main.c
+++ b/main.c
@@ -20,6 +20,10 @@
 	int score;
 	int depth;
 	struct moonfish_chess chess;
+	char *end;
+#ifndef moonfish_mini
+	long int long_depth;
+#endif
 	
 	if (argc > 1)
 	{
@@ -64,12 +68,24 @@
 					else xtime = &btime;
 					
 					arg = strtok(NULL, "\r\n\t ");
-					
-					if (arg == NULL || sscanf(arg, "%ld", xtime) != 1 || *xtime < 0)
+					if (arg == NULL)
 					{
 						fprintf(stderr, "%s: malformed 'go' command\n", argv[0]);
 						return 1;
 					}
+					
+					errno = 0;
+					*xtime = strtol(arg, &end, 10);
+					if (errno != 0)
+					{
+						perror(argv[0]);
+						return 1;
+					}
+					if (*end == 0 || *xtime < 0)
+					{
+						fprintf(stderr, "%s: malformed time in 'go' command\n", argv[0]);
+						return 1;
+					}
 				}
 #ifndef moonfish_mini
 				else if (!strcmp(arg, "depth"))
@@ -81,6 +97,21 @@
 						fprintf(stderr, "%s: malformed 'go depth' command\n", argv[0]);
 						return 1;
 					}
+					
+					errno = 0;
+					long_depth = strtol(arg, &end, 10);
+					if (errno != 0)
+					{
+						perror(argv[0]);
+						return 1;
+					}
+					if (*end == 0 || long_depth < 0 || long_depth > 100)
+					{
+						fprintf(stderr, "%s: malformed time in 'go' command\n", argv[0]);
+						return 1;
+					}
+					
+					depth = long_depth;
 				}
 #endif
 			}
@@ -90,7 +121,7 @@
 			
 			if (depth >= 0)
 #ifdef moonfish_mini
-				exit(1);
+				return 1;
 #else
 				score = moonfish_best_move_depth(analysis, &move, depth);
 #endif
--