Commit 018ea2d7 authored by Vipin Sharma's avatar Vipin Sharma Committed by Sean Christopherson
Browse files

KVM: selftests: Add atoi_paranoid() to catch errors missed by atoi()



atoi() doesn't detect errors. There is no way to know that a 0 return
is correct conversion or due to an error.

Introduce atoi_paranoid() to detect errors and provide correct
conversion. Replace all atoi() calls with atoi_paranoid().

Signed-off-by: default avatarVipin Sharma <vipinsh@google.com>
Suggested-by: default avatarDavid Matlack <dmatlack@google.com>
Suggested-by: default avatarSean Christopherson <seanjc@google.com>
Reviewed-by: default avatarSean Christopherson <seanjc@google.com>
Link: https://lore.kernel.org/r/20221103191719.1559407-4-vipinsh@google.com


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent 0eb88a41
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -414,7 +414,7 @@ static bool parse_args(int argc, char *argv[])
	while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) {
		switch (opt) {
		case 'n':
			test_args.nr_vcpus = atoi(optarg);
			test_args.nr_vcpus = atoi_paranoid(optarg);
			if (test_args.nr_vcpus <= 0) {
				pr_info("Positive value needed for -n\n");
				goto err;
@@ -425,21 +425,21 @@ static bool parse_args(int argc, char *argv[])
			}
			break;
		case 'i':
			test_args.nr_iter = atoi(optarg);
			test_args.nr_iter = atoi_paranoid(optarg);
			if (test_args.nr_iter <= 0) {
				pr_info("Positive value needed for -i\n");
				goto err;
			}
			break;
		case 'p':
			test_args.timer_period_ms = atoi(optarg);
			test_args.timer_period_ms = atoi_paranoid(optarg);
			if (test_args.timer_period_ms <= 0) {
				pr_info("Positive value needed for -p\n");
				goto err;
			}
			break;
		case 'm':
			test_args.migration_freq_ms = atoi(optarg);
			test_args.migration_freq_ms = atoi_paranoid(optarg);
			if (test_args.migration_freq_ms < 0) {
				pr_info("0 or positive value needed for -m\n");
				goto err;
+1 −1
Original line number Diff line number Diff line
@@ -423,7 +423,7 @@ int main(int argc, char *argv[])
	while ((opt = getopt(argc, argv, "i:")) != -1) {
		switch (opt) {
		case 'i':
			ss_iteration = atoi(optarg);
			ss_iteration = atoi_paranoid(optarg);
			break;
		case 'h':
		default:
+3 −3
Original line number Diff line number Diff line
@@ -824,16 +824,16 @@ int main(int argc, char **argv)
	while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
		switch (opt) {
		case 'n':
			nr_irqs = atoi(optarg);
			nr_irqs = atoi_paranoid(optarg);
			if (nr_irqs > 1024 || nr_irqs % 32)
				help(argv[0]);
			break;
		case 'e':
			eoi_split = (bool)atoi(optarg);
			eoi_split = (bool)atoi_paranoid(optarg);
			default_args = false;
			break;
		case 'l':
			level_sensitive = (bool)atoi(optarg);
			level_sensitive = (bool)atoi_paranoid(optarg);
			default_args = false;
			break;
		case 'h':
+1 −1
Original line number Diff line number Diff line
@@ -368,7 +368,7 @@ int main(int argc, char *argv[])
			params.vcpu_memory_bytes = parse_size(optarg);
			break;
		case 'v':
			params.nr_vcpus = atoi(optarg);
			params.nr_vcpus = atoi_paranoid(optarg);
			break;
		case 'o':
			overlap_memory_access = true;
+1 −1
Original line number Diff line number Diff line
@@ -427,7 +427,7 @@ int main(int argc, char *argv[])
			p.src_type = parse_backing_src_type(optarg);
			break;
		case 'v':
			nr_vcpus = atoi(optarg);
			nr_vcpus = atoi_paranoid(optarg);
			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
				    "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
			break;
Loading