RE: [PATCH v6 3/5] KVM: selftests: Add atoi_paranoid() to catch errors missed by atoi()

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

 



On Saturday, October 22, 2022 5:18 AM, Vipin Sharma wrote:
> +int atoi_paranoid(const char *num_str)
> +{
> +	char *end_ptr;
> +	long num;
> +
> +	errno = 0;
> +	num = strtol(num_str, &end_ptr, 10);

Why not use strtoull here?
Negative numbers will result in a huge "unsigned long long" number,
and this will be captured by your TEST_ASSERT(num >= INT_MIN) below.
Then we don't need patch 4, I think.


> +	TEST_ASSERT(!errno, "strtol(\"%s\") failed", num_str);
> +	TEST_ASSERT(num_str != end_ptr,
> +		    "strtol(\"%s\") didn't find a valid integer.\n", num_str);
> +	TEST_ASSERT(*end_ptr == '\0',
> +		    "strtol(\"%s\") failed to parse trailing characters \"%s\".\n",
> +		    num_str, end_ptr);
> +	TEST_ASSERT(num >= INT_MIN && num <= INT_MAX,
> +		    "%ld not in range of [%d, %d]", num, INT_MIN, INT_MAX);
> +
> +	return num;
> +}




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux