[Sheepdog] [PATCH v2] use strtol() instead of atoi() for error check

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Mon Oct 17 16:14:37 CEST 2011


Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---

v2:
 - fix compile warnings


 collie/cluster.c |   11 ++++++++++-
 collie/collie.c  |    7 ++++++-
 collie/vdi.c     |   14 +++++++++++---
 sheep/sheep.c    |   13 +++++++++----
 sheep/store.c    |    6 +++++-
 5 files changed, 41 insertions(+), 10 deletions(-)

diff --git a/collie/cluster.c b/collie/cluster.c
index 0d5dfbe..e0ab37f 100644
--- a/collie/cluster.c
+++ b/collie/cluster.c
@@ -172,9 +172,18 @@ static struct subcommand cluster_cmd[] = {
 
 static int cluster_parser(int ch, char *opt)
 {
+	int copies;
+	char *p;
+
 	switch (ch) {
 	case 'c':
-		cluster_cmd_data.copies = atoi(opt);
+		copies = strtol(opt, &p, 10);
+		if (opt == p || copies < 1 || copies > SD_MAX_REDUNDANCY) {
+			fprintf(stderr, "copies must be from 1 through %d\n",
+				SD_MAX_REDUNDANCY);
+			exit(EXIT_FAILURE);
+		}
+		cluster_cmd_data.copies = copies;
 		break;
 	}
 
diff --git a/collie/collie.c b/collie/collie.c
index e064a0a..418c644 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -278,6 +278,7 @@ int main(int argc, char **argv)
 	unsigned long flags;
 	struct option *long_options;
 	const char *short_options;
+	char *p;
 	struct command commands[] = {
 		vdi_command,
 		node_command,
@@ -304,7 +305,11 @@ int main(int argc, char **argv)
 			sdhost = optarg;
 			break;
 		case 'p':
-			sdport = atoi(optarg);
+			sdport = strtol(optarg, &p, 10);
+			if (optarg == p || sdport < 1 || sdport > UINT16_MAX) {
+				fprintf(stderr, "invalid port number: %s\n", optarg);
+				exit(EXIT_USAGE);
+			}
 			break;
 		case 'r':
 			raw_output = 1;
diff --git a/collie/vdi.c b/collie/vdi.c
index 27b0595..63c174f 100644
--- a/collie/vdi.c
+++ b/collie/vdi.c
@@ -1191,18 +1191,26 @@ static struct subcommand vdi_cmd[] = {
 
 static int vdi_parser(int ch, char *opt)
 {
+	char *p;
+
 	switch (ch) {
 	case 'P':
 		vdi_cmd_data.prealloc = 1;
 		break;
 	case 'i':
-		vdi_cmd_data.index = atoi(opt);
+		vdi_cmd_data.index = strtol(opt, &p, 10);
+		if (opt == p) {
+			fprintf(stderr, "the index must be an integer\n");
+			exit(EXIT_FAILURE);
+		}
 		break;
 	case 's':
-		vdi_cmd_data.snapshot_id = atoi(opt);
-		if (vdi_cmd_data.snapshot_id == 0)
+		vdi_cmd_data.snapshot_id = strtol(opt, &p, 10);
+		if (opt == p) {
+			vdi_cmd_data.snapshot_id = 0;
 			strncpy(vdi_cmd_data.snapshot_tag, opt,
 				sizeof(vdi_cmd_data.snapshot_tag));
+		}
 		break;
 	case 'x':
 		vdi_cmd_data.exclusive = 1;
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 27859ca..3fbc81a 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -101,15 +101,20 @@ int main(int argc, char **argv)
 				 &longindex)) >= 0) {
 		switch (ch) {
 		case 'p':
-			port = atoi(optarg);
+			port = strtol(optarg, &p, 10);
+			if (optarg == p || port < 1 || port > UINT16_MAX) {
+				eprintf("invalid port number: %s\n", optarg);
+				exit(1);
+			}
 			break;
 		case 'f':
 			is_daemon = 0;
 			break;
 		case 'l':
-			log_level = atoi(optarg);
-			if ((log_level < SDOG_EMERG) || (log_level > SDOG_DEBUG)) {
-				printf("Invalid log level: %d\n", log_level);
+			log_level = strtol(optarg, &p, 10);
+			if (optarg == p || log_level < SDOG_EMERG ||
+			    log_level > SDOG_DEBUG) {
+				printf("Invalid log level: %s\n", optarg);
 				sdlog_help();
 				exit(1);
 			}
diff --git a/sheep/store.c b/sheep/store.c
index abaab9f..5df637d 100644
--- a/sheep/store.c
+++ b/sheep/store.c
@@ -941,6 +941,7 @@ int get_latest_epoch(void)
 	DIR *dir;
 	struct dirent *d;
 	uint32_t e, epoch = 0;
+	char *p;
 
 	dir = opendir(epoch_path);
 	if (!dir) {
@@ -949,7 +950,10 @@ int get_latest_epoch(void)
 	}
 
 	while ((d = readdir(dir))) {
-		e = atoi(d->d_name);
+		e = strtol(d->d_name, &p, 0);
+		if (d->d_name == p)
+			continue;
+
 		if (e > epoch)
 			epoch = e;
 	}
-- 
1.7.2.5




More information about the sheepdog mailing list