Browse Source

Bugfix: Check for error conditions in get_intrange to not have weird --device behaviour when bad values are provided

Also improves its accepted input
Luke Dashjr 12 years ago
parent
commit
7ab84e91c2
2 changed files with 22 additions and 6 deletions
  1. 21 5
      miner.c
  2. 1 1
      miner.h

+ 21 - 5
miner.c

@@ -748,10 +748,24 @@ static char *add_serial(char *arg)
 }
 #endif
 
-void get_intrange(char *arg, int *val1, int *val2)
+bool get_intrange(const char *arg, int *val1, int *val2)
 {
-	if (sscanf(arg, "%d-%d", val1, val2) == 1)
-		*val2 = *val1;
+	int pos, n;
+	// Is is unclear whether %n is counted in the returned value, so %n is doubled up to make 2 unambiguous
+	n = sscanf(arg, "%d%n%n -%d %n", val1, &pos, &pos, val2, &pos);
+	if (unlikely(arg[pos]))
+		return false;
+	switch (n)
+	{
+		case 1:  // %n not counted (only one number)
+		case 3:  // %n     counted
+			*val2 = *val1;
+		case 2:  // %n not counted (two numbers)
+		case 5:  // %n     counted
+			return true;
+		default:
+			return false;
+	}
 }
 
 static char *set_devices(char *arg)
@@ -770,7 +784,8 @@ static char *set_devices(char *arg)
 	nextptr = strtok(arg, ",");
 	if (nextptr == NULL)
 		return "Invalid parameters for set devices";
-	get_intrange(nextptr, &val1, &val2);
+	if (!get_intrange(nextptr, &val1, &val2))
+		return "Invalid device number";
 	if (val1 < 0 || val1 > MAX_DEVICES || val2 < 0 || val2 > MAX_DEVICES ||
 	    val1 > val2) {
 		return "Invalid value passed to set devices";
@@ -782,7 +797,8 @@ static char *set_devices(char *arg)
 	}
 
 	while ((nextptr = strtok(NULL, ",")) != NULL) {
-		get_intrange(nextptr, &val1, &val2);
+		if (!get_intrange(nextptr, &val1, &val2))
+			return "Invalid device number";
 		if (val1 < 0 || val1 > MAX_DEVICES || val2 < 0 || val2 > MAX_DEVICES ||
 		val1 > val2) {
 			return "Invalid value passed to set devices";

+ 1 - 1
miner.h

@@ -922,7 +922,7 @@ extern void api(int thr_id);
 
 extern struct pool *current_pool(void);
 extern int enabled_pools;
-extern void get_intrange(char *arg, int *val1, int *val2);
+extern bool get_intrange(const char *arg, int *val1, int *val2);
 extern bool detect_stratum(struct pool *pool, char *url);
 extern void print_summary(void);
 extern struct pool *add_pool(void);