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

From: Wang, Wei W
Date: Tue Oct 25 2022 - 22:17:00 EST


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;
> +}