[sheepdog] [PATCH] sheep: better error handling of numbers specified in command line arguments

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Jan 28 03:02:22 CET 2013


Current sheep treats this command line option as a valid port number:
"-p 7000/store/sheep". But I believe this should be treated as an
error. This sort of option can be passed if users forget to type ' '
between port number and path for sheep directory.

This confusing behaviour can be found in handling port number, log
level, zone ID, and free space size. This patch fixes it.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 sheep/sheep.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/sheep/sheep.c b/sheep/sheep.c
index 2808e5f..f2c7bcd 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -405,7 +405,8 @@ int main(int argc, char **argv)
 		switch (ch) {
 		case 'p':
 			port = strtol(optarg, &p, 10);
-			if (optarg == p || port < 1 || port > UINT16_MAX) {
+			if (optarg == p || port < 1 || UINT16_MAX < port
+				|| *p != '\0') {
 				fprintf(stderr, "Invalid port number '%s'\n",
 					optarg);
 				exit(1);
@@ -420,7 +421,7 @@ int main(int argc, char **argv)
 		case 'l':
 			log_level = strtol(optarg, &p, 10);
 			if (optarg == p || log_level < SDOG_EMERG ||
-			    log_level > SDOG_DEBUG) {
+			    SDOG_DEBUG < log_level || *p != '\0') {
 				fprintf(stderr, "Invalid log level '%s'\n",
 					optarg);
 				sdlog_help();
@@ -451,7 +452,8 @@ int main(int argc, char **argv)
 			break;
 		case 'z':
 			zone = strtol(optarg, &p, 10);
-			if (optarg == p || zone < 0 || UINT32_MAX < zone) {
+			if (optarg == p || zone < 0 || UINT32_MAX < zone
+				|| *p != '\0') {
 				fprintf(stderr, "Invalid zone id '%s': "
 					"must be an integer between 0 and %u\n",
 					optarg, UINT32_MAX);
@@ -462,7 +464,7 @@ int main(int argc, char **argv)
 		case 's':
 			free_space = strtoll(optarg, &p, 10);
 			if (optarg == p || free_space <= 0 ||
-			    UINT64_MAX < free_space) {
+			    UINT64_MAX < free_space || *p != '\0') {
 				fprintf(stderr, "Invalid free space size '%s': "
 					"must be an integer between 0 and "
 					"%"PRIu64"\n", optarg, UINT64_MAX);
-- 
1.7.2.5




More information about the sheepdog mailing list