public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: Geert Uytterhoeven <geert@linux-m68k.org>,
	Qu Wenruo <quwenruo.btrfs@gmx.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 20:15:27 +1030	[thread overview]
Message-ID: <a13d0f90-58bd-45cb-bf4e-880880f54a32@suse.com> (raw)
In-Reply-To: <CAMuHMdVNZPm0jdqD0EdahiTc8PJYQ+OVvBxKagQx_je-GTmJ2w@mail.gmail.com>


[-- Attachment #1.1.1: Type: text/plain, Size: 3889 bytes --]



On 2024/1/3 19:57, Geert Uytterhoeven wrote:
> Hi Qu,
> 
> On Tue, Jan 2, 2024 at 9:56 PM Qu Wenruo <quwenruo.btrfs@gmx.com> wrote:
>> On 2024/1/2 23:53, Geert Uytterhoeven wrote:
>>> 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...
> 
> So did that fix the sparse warning? ;-)

Intel guys have already masked this particular warning.

But your newer suggestion is much better.

> 
>>>> +#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)
> 
> Ah yes, sparse can be annoying.
> I'm still looking for a clean and concise way to shut up [1].
> 
>> 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?
> 
> Masking off the extra bits, like lower_32_bits()[2] does?
> 
>      #define POINTER_PATTERN  ((void *)(uintptr_t)((ULL_PATTERN) & UINTPTR_MAX))

This sounds much better to me.

I would go this path instead, and finally no need to manually count how 
many bytes (which I already failed once).

Thanks,
Qu

> 
> [1] https://lore.kernel.org/oe-kbuild-all/202312181649.u6k6hLIm-lkp@intel.com/
> [2] https://elixir.bootlin.com/linux/latest/source/include/linux/kernel.h#L82
> 
> Gr{oetje,eeting}s,
> 
>                          Geert
> 

[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 7027 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 495 bytes --]

  reply	other threads:[~2024-01-03  9:45 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
2024-01-03  9:27       ` Geert Uytterhoeven
2024-01-03  9:45         ` Qu Wenruo [this message]
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=a13d0f90-58bd-45cb-bf4e-880880f54a32@suse.com \
    --to=wqu@suse.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=quwenruo.btrfs@gmx.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