From: Qu Wenruo <quwenruo.btrfs@gmx.com>
To: Geert Uytterhoeven <geert@linux-m68k.org>, Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org,
akpm@linux-foundation.org, christophe.jaillet@wanadoo.fr,
andriy.shevchenko@linux.intel.com, David.Laight@aculab.com,
ddiss@suse.de
Subject: Re: [PATCH v2 3/4] kstrtox: add unit tests for memparse_safe()
Date: Wed, 3 Jan 2024 07:25:33 +1030 [thread overview]
Message-ID: <756ac3e8-3d68-40fe-a7d4-1cf6ac77185e@gmx.com> (raw)
In-Reply-To: <CAMuHMdWXRid2hyvMLcQ6f+M1fxBZUPdeSN=3e=-xXNSce4gsJg@mail.gmail.com>
On 2024/1/2 23:53, Geert Uytterhoeven wrote:
> Hi Qu,
>
> On Tue, Jan 2, 2024 at 5:13 AM Qu Wenruo <wqu@suse.com> wrote:
>> The new tests cases for memparse_safe() include:
>>
>> - The existing test cases for kstrtoull()
>> Including all the 3 bases (8, 10, 16), and all the ok and failure
>> cases.
>> Although there are something we need to verify specific for
>> memparse_safe():
>>
>> * @retptr and @value are not modified for failure cases
>>
>> * return value are correct for failure cases
>>
>> * @retptr is correct for the good cases
>>
>> - New test cases
>> Not only testing the result value, but also the @retptr, including:
>>
>> * good cases with extra tailing chars, but without valid prefix
>> The @retptr should point to the first char after a valid string.
>> 3 cases for all the 3 bases.
>>
>> * good cases with extra tailing chars, with valid prefix
>> 5 cases for all the suffixes.
>>
>> * bad cases without any number but stray suffix
>> Should be rejected with -EINVAL
>>
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
>
> Thanks for your patch!
>
>> --- a/lib/test-kstrtox.c
>> +++ b/lib/test-kstrtox.c
>> @@ -268,6 +268,237 @@ static void __init test_kstrtoll_ok(void)
>> TEST_OK(kstrtoll, long long, "%lld", test_ll_ok);
>> }
>>
>> +/*
>> + * The special pattern to make sure the result is not modified for error cases.
>> + */
>> +#define ULL_PATTERN (0xefefefef7a7a7a7aULL)
>> +#if BITS_PER_LONG == 32
>> +#define POINTER_PATTERN (0xefef7a7a7aUL)
>
> This pattern needs 40 bits to fit, so it doesn't fit in a 32-bit
> unsigned long or pointer. Probably you wanted to use 0xef7a7a7aUL
> instead?
My bad, one extra byte...
>
>> +#else
>> +#define POINTER_PATTERN (ULL_PATTERN)
>> +#endif
>
> Shouldn't a simple cast to uintptr_t work fine for both 32-bit and
> 64-bit systems:
>
> #define POINTER_PATTERN ((uintptr_t)ULL_PATTERN)
>
> Or even better, incorporate the cast to a pointer:
>
> #define POINTER_PATTERN ((void *)(uintptr_t)ULL_PATTERN)
The problem is reported by sparse, which warns about that ULL_PATTERN
converted to a pointer would lose its width:
lib/test-kstrtox.c:339:40: sparse: sparse: cast truncates bits from
constant value (efefefef7a7a7a7a becomes 7a7a7a7a)
I'm not sure if using uiintptr_t would solve it, thus I go the macro to
switch the value to avoid the static checker's warning.
I tried to check how other locations handles patterned pointer value,
like CONFIG_INIT_STACK_ALL_PATTERN, but they're either relying on the
compiler or just memset().
Any better idea to solve the problem in a better way?
Thanks,
Qu
>
> so you can drop the extra cast when assigning/comparing retptr below.
>
>> +
>> +/* Want to include "E" suffix for full coverage. */
>> +#define MEMPARSE_TEST_SUFFIX (MEMPARSE_SUFFIX_K | MEMPARSE_SUFFIX_M |\
>> + MEMPARSE_SUFFIX_G | MEMPARSE_SUFFIX_T |\
>> + MEMPARSE_SUFFIX_P | MEMPARSE_SUFFIX_E)
>> +
>> +static void __init test_memparse_safe_fail(void)
>> +{
>
> [...]
>
>> + for_each_test(i, tests) {
>> + const struct memparse_test_fail *t = &tests[i];
>> + unsigned long long tmp = ULL_PATTERN;
>> + char *retptr = (char *)POINTER_PATTERN;
>> + int ret;
>
> [...]
>
> + if (retptr != (char *)POINTER_PATTERN)
>
> Gr{oetje,eeting}s,
>
> Geert
>
next prev parent reply other threads:[~2024-01-02 20:56 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-02 4:12 [PATCH v2 0/4] kstrtox: introduce memparse_safe() Qu Wenruo
2024-01-02 4:12 ` [PATCH v2 1/4] kstrtox: always skip the leading "0x" even if no more valid chars Qu Wenruo
2024-01-02 14:16 ` David Disseldorp
2024-01-02 4:12 ` [PATCH v2 2/4] kstrtox: introduce a safer version of memparse() Qu Wenruo
2024-01-02 14:19 ` David Disseldorp
2024-01-02 4:12 ` [PATCH v2 3/4] kstrtox: add unit tests for memparse_safe() Qu Wenruo
2024-01-02 13:23 ` Geert Uytterhoeven
2024-01-02 20:55 ` Qu Wenruo [this message]
2024-01-03 9:27 ` Geert Uytterhoeven
2024-01-03 9:45 ` Qu Wenruo
2024-01-02 14:17 ` David Disseldorp
2024-01-03 22:45 ` Qu Wenruo
2024-01-02 4:12 ` [PATCH v2 4/4] btrfs: migrate to the newer memparse_safe() helper Qu Wenruo
2024-01-02 14:24 ` David Disseldorp
2024-01-02 17:10 ` [PATCH v2 0/4] kstrtox: introduce memparse_safe() David Sterba
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=756ac3e8-3d68-40fe-a7d4-1cf6ac77185e@gmx.com \
--to=quwenruo.btrfs@gmx.com \
--cc=David.Laight@aculab.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=christophe.jaillet@wanadoo.fr \
--cc=ddiss@suse.de \
--cc=geert@linux-m68k.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wqu@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox