lkml.org 
[lkml]   [2022]   [Oct]   [25]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRE: [PATCH v6 3/5] KVM: selftests: Add atoi_paranoid() to catch errors missed by atoi()
Date
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;
> +}

\
 
 \ /
  Last update: 2022-10-26 04:16    [W:0.132 / U:1.304 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site