From 57ee59ceeabf4f12156bb2e6e7f501517a8c6efb Mon Sep 17 00:00:00 2001
From: Dominic Spill <dominicgs@gmail.com>
Date: Thu, 14 Jan 2016 17:33:00 +0000
Subject: [PATCH 65/68] Add error handling to hackrf_trasfer argument parsing

---
 host/hackrf-tools/src/hackrf_transfer.c | 53 +++++++++++++++++----------------
 1 file changed, 27 insertions(+), 26 deletions(-)

--- a/host/hackrf-tools/src/hackrf_transfer.c
+++ b/host/hackrf-tools/src/hackrf_transfer.c
@@ -483,6 +483,8 @@
 	char date_time[DATE_TIME_MAX_LEN];
 	const char* path = NULL;
 	const char* serial_number = NULL;
+	char* endptr;
+	double f_hz;
 	int result;
 	time_t rawtime;
 	struct tm * timeinfo;
@@ -516,33 +518,33 @@
 			break;
 
 		case 'f':
-			{
-			double f = atof(optarg);
-			if (f < 0)
+			f_hz = strtod(optarg, &endptr);
+			if (optarg == endptr) {
+				result = HACKRF_ERROR_INVALID_PARAM;
 				break;
-			freq_hz = f;
-			automatic_tuning = true;
 			}
+			freq_hz = f_hz;
+			automatic_tuning = true;
 			break;
 
 		case 'i':
-			{
-			double f = atof(optarg);
-			if (f < 0)
+			f_hz = strtod(optarg, &endptr);
+			if (optarg == endptr) {
+				result = HACKRF_ERROR_INVALID_PARAM;
 				break;
-			if_freq_hz = f;
-			if_freq = true;
 			}
+			if_freq_hz = f_hz;
+			if_freq = true;
 			break;
 
 		case 'o':
-			{
-			double f = atof(optarg);
-			if (f < 0)
+			f_hz = strtod(optarg, &endptr);
+			if (optarg == endptr) {
+				result = HACKRF_ERROR_INVALID_PARAM;
 				break;
-			lo_freq_hz = f;
-			lo_freq = true;
 			}
+			lo_freq_hz = f_hz;
+			lo_freq = true;
 			break;
 
 		case 'm':
@@ -573,13 +575,13 @@
 			break;
 
 		case 's':
-			{
-			double f = atof(optarg);
-			if (f < 0)
+			f_hz = strtod(optarg, &endptr);
+			if (optarg == endptr) {
+				result = HACKRF_ERROR_INVALID_PARAM;
 				break;
-			sample_rate_hz = f;
-			sample_rate = true;
 			}
+			sample_rate_hz = f_hz;
+			sample_rate = true;
 			break;
 
 		case 'n':
@@ -589,14 +591,13 @@
 			break;
 
 		case 'b':
-			{
-			double f = atof(optarg);
-			if (f < 0)
+			f_hz = strtod(optarg, &endptr);
+			if (optarg == endptr) {
+				result = HACKRF_ERROR_INVALID_PARAM;
 				break;
-
-			baseband_filter_bw_hz = f;
-			baseband_filter_bw = true;
 			}
+			baseband_filter_bw_hz = f_hz;
+			baseband_filter_bw = true;
 			break;
 
 		case 'c':
