Commit 5ef9c139240f17270d5124101b9972b955d070c9

Luke Dashjr 2012-07-27T20:53:59

Bugfix: API: Report errors from poolpriority command

diff --git a/api.c b/api.c
index 86de3c8..4e1da9a 100644
--- a/api.c
+++ b/api.c
@@ -2139,7 +2139,7 @@ static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, 
 	SETUP_STRTOK_TS;
 	int total_pools_ = total_pools;  // Keep a local copy, to be more threadsafe
 	char *a;
-	int i, prio = 0;
+	int i, prio = 0, e = -1;
 
 	if (total_pools_ == 0) {
 		strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
@@ -2152,7 +2152,11 @@ static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, 
 
 	a = strtok_ts(param, ",");
 	do {
-		i = atoi(a);
+		i = strtol(a, &a, 10);
+		if (unlikely(*a > 0x20 || i < 0 || i >= total_pools)) {
+			e = (*a > 0x20) ? -2 : i;
+			continue;
+		}
 		pools[i]->prio = prio++;
 		pools_changed[i] = true;
 	} while ( (a = strtok_ts(NULL, ",")) );
@@ -2164,6 +2168,14 @@ static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, 
 	if (current_pool()->prio)
 		switch_pools(NULL);
 
+	if (e != -1) {
+		if (e == -2)
+			strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
+		else
+			strcpy(io_buffer, message(MSG_INVPID, e, NULL, isjson));
+		return;
+	}
+
 	strcpy(io_buffer, message(MSG_POOLPRIO, 0, NULL, isjson));
 }