Re: [PATCH v4 04/10] KVM: selftests: Add memory size parameter to the demand paging test

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
> new file mode 100644
> index 0000000000000..706e0f963a44b
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/lib/test_util.c
> @@ -0,0 +1,61 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * tools/testing/selftests/kvm/lib/test_util.c
> + *
> + * Copyright (C) 2020, Google LLC.
> + */
> +
> +#include "test_util.h"
> +
> +#include <ctype.h>
> +
> +/*
> + * Parses "[0-9]+[kmgt]?".
> + */
> +size_t parse_size(const char *size)
> +{
> +	size_t len = strlen(size);
> +	size_t i;
> +	size_t scale_shift = 0;
> +	size_t base;
> +
> +	TEST_ASSERT(len > 0, "Need at least 1 digit in '%s'", size);
> +
> +	/* Find the first letter in the string, indicating scale. */
> +	for (i = 0; i < len; i++) {
> +		if (!isdigit(size[i])) {
> +			TEST_ASSERT(i > 0, "Need at least 1 digit in '%s'",
> +				    size);
> +			TEST_ASSERT(i == len - 1,
> +				    "Expected letter at the end in '%s'.",
> +				    size);
> +			switch (tolower(size[i])) {
> +			case 't':
> +				scale_shift = 40;
> +				break;
> +			case 'g':
> +				scale_shift = 30;
> +				break;
> +			case 'm':
> +				scale_shift = 20;
> +				break;
> +			case 'k':
> +				scale_shift = 10;
> +				break;
> +			default:
> +				TEST_ASSERT(false, "Unknown size letter %c",
> +					    size[i]);
> +			}
> +		}
> +	}
> +
> +	TEST_ASSERT(scale_shift < 8 * sizeof(size_t),
> +		    "Overflow parsing scale!");
> +
> +	base = atoi(size);

I'd use strtoull(size, NULL, 0), allowing the user to input full 0x...
sizes too. And, if the strtoull is done before the scale parsing, then
you could supply a non-null endptr and avoid the need for the for loop.

Thanks,
drew




[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux