Hello Stefan,

> On Wed, 19 Feb 2014 19:17:51 +0800, Wang Shilong wrote:
>> There are many places that need parse string to u64 for btrfs commands,
>> in fact, we do such things *too casually*, using atoi/atol/atoll..is not
>> right at all, and even we don't check whether it is a valid string.
>> 
>> Let's do everything more gracefully, we introduce a new helper
>> btrfs_strtoull() which will do all the necessary checks.If we fail to
>> parse string to u64, we will output message and exit directly, this is
>> something like what usage() is doing. It is ok to not return erro to
>> it's caller, because this function should be called when parsing arg
>> (just like usage!)
>> 
>> Signed-off-by: Wang Shilong <[email protected]>
>> ---
>> utils.c | 19 +++++++++++++++++++
>> utils.h |  1 +
>> 2 files changed, 20 insertions(+)
>> 
>> diff --git a/utils.c b/utils.c
>> index 97e23d5..0698d8d 100644
>> --- a/utils.c
>> +++ b/utils.c
>> @@ -1520,6 +1520,25 @@ scan_again:
>>      return 0;
>> }
>> 
>> +u64 btrfs_strtoull(char *str, int base)
>> +{
>> +    u64 value;
>> +    char *ptr_parse_end = NULL;
>> +    char *ptr_str_end = str + strlen(str);
>> +
>> +    value = strtoull(str, &ptr_parse_end, base);
>> +    if (ptr_parse_end != ptr_str_end) {
>> +            fprintf(stderr, "ERROR: %s is not an invalid unsigned long long 
>> integer.\n",
> 
> "not invalid" :)
> 
oops

>> +                            str);
>> +            exit(1);
>> +    }
>> +    if (value == ULONG_MAX) {
> 
> ULLONG_MAX or {errno = 0; value = strtoull(...); if (errno == ERANGE)…}

Yeah, i will use ULLONG_MAX instead ULONG  to check^_^..

> 
> 
>> +            fprintf(stderr, "ERROR: %s is out of range.\n", str);
>> +            exit(1);
>> +    }
> 
> base = 0 is best BTW since it is able to read hex and octal numbers as
> well. I'd remove the base parameter to btrfs_strtoull() and always use 0.

Right, all comments addressed, thanks for your review.^_^

Thanks,
Wang
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to [email protected]
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to