Blob Blame History Raw
From 5d79a5c944d05f71425d3156597a2a61c5f2ab9a Mon Sep 17 00:00:00 2001
From: Ming-Hung Tsai <mtsai@redhat.com>
Date: Wed, 2 Jun 2021 13:08:47 +0800
Subject: [PATCH 08/10] [thin_dump] Fix warnings on potential NULL pointer

---
 base/application.cc            |  8 +++++++-
 base/application.h             |  2 +-
 thin-provisioning/thin_dump.cc | 16 ++--------------
 3 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/base/application.cc b/base/application.cc
index 61338ff..dbd55a5 100644
--- a/base/application.cc
+++ b/base/application.cc
@@ -25,8 +25,14 @@ command::die(string const &msg)
 }
 
 ::uint64_t
-command::parse_uint64(string const &str, string const &desc)
+command::parse_uint64(char const *str, char const *desc)
 {
+	if (!str) {
+		ostringstream out;
+		out << "Couldn't parse " << desc << ": NULL";
+		die(out.str());
+	}
+
 	try {
 		// FIXME: check trailing garbage is handled
 		return lexical_cast<::uint64_t>(str);
diff --git a/base/application.h b/base/application.h
index b799eaf..f071533 100644
--- a/base/application.h
+++ b/base/application.h
@@ -19,7 +19,7 @@ namespace base {
 		virtual ~command() {}
 
 		void die(std::string const &msg);
-		uint64_t parse_uint64(std::string const &str, std::string const &desc);
+		uint64_t parse_uint64(char const *str, char const *desc);
 
 
 		virtual void usage(std::ostream &out) const = 0;
diff --git a/thin-provisioning/thin_dump.cc b/thin-provisioning/thin_dump.cc
index 74cfa91..c0e73e6 100644
--- a/thin-provisioning/thin_dump.cc
+++ b/thin-provisioning/thin_dump.cc
@@ -143,7 +143,6 @@ thin_dump_cmd::run(int argc, char **argv)
 	int c;
 	char const *output = NULL;
 	const char shortopts[] = "hm::o:f:rV";
-	char *end_ptr;
 	block_address metadata_snap = 0;
 	::uint64_t dev_id;
 	struct flags flags;
@@ -181,13 +180,7 @@ thin_dump_cmd::run(int argc, char **argv)
 			flags.use_metadata_snap = true;
 			if (optarg) {
 				// FIXME: deprecate this option
-				metadata_snap = strtoull(optarg, &end_ptr, 10);
-				if (end_ptr == optarg) {
-					cerr << "couldn't parse <metadata-snap>" << endl;
-					usage(cerr);
-					return 1;
-				}
-
+				metadata_snap = parse_uint64(optarg, "metadata-snap");
 				flags.snap_location = metadata_snap;
 			}
 			break;
@@ -197,12 +190,7 @@ thin_dump_cmd::run(int argc, char **argv)
 			break;
 
 		case 1:
-			dev_id = strtoull(optarg, &end_ptr, 10);
-			if (end_ptr == optarg) {
-				cerr << "couldn't parse <dev-id>\n";
-				usage(cerr);
-				return 1;
-			}
+			dev_id = parse_uint64(optarg, "dev-id");
 			flags.opts.select_dev(dev_id);
 			break;
 
-- 
1.8.3.1