Commit 0001725d authored by Vipin Sharma's avatar Vipin Sharma Committed by Sean Christopherson
Browse files

KVM: selftests: Add atoi_positive() and atoi_non_negative() for input validation



Many KVM selftests take command line arguments which are supposed to be
positive (>0) or non-negative (>=0). Some tests do these validation and
some missed adding the check.

Add atoi_positive() and atoi_non_negative() to validate inputs in
selftests before proceeding to use those values.

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


Signed-off-by: default avatarSean Christopherson <seanjc@google.com>
parent c15bdebb
Loading
Loading
Loading
Loading
+5 −20
Original line number Diff line number Diff line
@@ -414,36 +414,21 @@ 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_paranoid(optarg);
			if (test_args.nr_vcpus <= 0) {
				pr_info("Positive value needed for -n\n");
				goto err;
			} else if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
			test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
			if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
				pr_info("Max allowed vCPUs: %u\n",
					KVM_MAX_VCPUS);
				goto err;
			}
			break;
		case 'i':
			test_args.nr_iter = atoi_paranoid(optarg);
			if (test_args.nr_iter <= 0) {
				pr_info("Positive value needed for -i\n");
				goto err;
			}
			test_args.nr_iter = atoi_positive("Number of iterations", optarg);
			break;
		case 'p':
			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;
			}
			test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
			break;
		case 'm':
			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;
			}
			test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
			break;
		case 'h':
		default:
+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_paranoid(optarg);
			ss_iteration = atoi_positive("Number of iterations", optarg);
			break;
		case 'h':
		default:
+1 −1
Original line number Diff line number Diff line
@@ -824,7 +824,7 @@ int main(int argc, char **argv)
	while ((opt = getopt(argc, argv, "hn:e:l:")) != -1) {
		switch (opt) {
		case 'n':
			nr_irqs = atoi_paranoid(optarg);
			nr_irqs = atoi_non_negative("Number of IRQs", optarg);
			if (nr_irqs > 1024 || nr_irqs % 32)
				help(argv[0]);
			break;
+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_paranoid(optarg);
			params.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
			break;
		case 'o':
			overlap_memory_access = true;
+2 −2
Original line number Diff line number Diff line
@@ -427,8 +427,8 @@ int main(int argc, char *argv[])
			p.src_type = parse_backing_src_type(optarg);
			break;
		case 'v':
			nr_vcpus = atoi_paranoid(optarg);
			TEST_ASSERT(nr_vcpus > 0 && nr_vcpus <= max_vcpus,
			nr_vcpus = atoi_positive("Number of vCPUs", optarg);
			TEST_ASSERT(nr_vcpus <= max_vcpus,
				    "Invalid number of vcpus, must be between 1 and %d", max_vcpus);
			break;
		case 'o':
Loading