From: Alexey Dobriyan <adobriyan@gmail.com>
To: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 01/10] Add parse_integer() (replacement for simple_strto*())
Date: Mon, 4 May 2015 17:32:50 +0300 [thread overview]
Message-ID: <CACVxJT_=GLvA0D9LGsXnOcj+r8VgFMP4pcL5oM6cQ2CcT2ZNVA@mail.gmail.com> (raw)
In-Reply-To: <87h9rsiigj.fsf@rasmusvillemoes.dk>
On Mon, May 4, 2015 at 4:24 PM, Rasmus Villemoes
<linux@rasmusvillemoes.dk> wrote:
> On Sat, May 02 2015, Alexey Dobriyan <adobriyan@gmail.com> wrote:
>> Enter parse_integer().
>>
>> int parse_integer(const char *s, unsigned int base, T *val);
>>
>
> I like the general idea. Few nits below (and in reply to other patches).
>
> First: Could you tell me what tree I can commit this on top of, to see
> what gcc makes of it.
Any recent kernel should be OK, code it quite self contained.
I've just applied first two patches on top 4.1-rc2.
BTW the correct order is
1) [PATCH 01/10] Add parse_integer() (replacement for simple_strto*())
*** 2) [PATCH CORRECT 03/10] parse_integer: convert sscanf()
3) [PATCH 03/10] parse_integer: convert sscanf()
4) [PATCH 04/10] sscanf: fix overflow
...
10) [PATCH 10/10] ext2, ext3, ext4: convert to parse_integer()/kstrto*()
I've copied patch #2 twice, so it won't apply and resent it
with subject from patch #3 to confuse everyone even more.
>> +#define parse_integer(s, base, val) \
>> +({ \
>> + const char *_s = (s); \
>> + unsigned int _base = (base); \
>> + typeof(val) _val = (val); \
>> + \
>> + __builtin_choose_expr( \
>> + __builtin_types_compatible_p(typeof(_val), signed char *), \
>> + _parse_integer_sc(_s, _base, (void *)_val),
>> \
>
> Why the (void*) cast? Isn't _val supposed to have precisely the type
> expected by _parse_integer_sc at this point?
>
>> + __builtin_choose_expr( \
>> + __builtin_types_compatible_p(typeof(_val), long *) && sizeof(long) == 4,\
>> + _parse_integer_i(_s, _base, (void *)_val), \
>> + __builtin_choose_expr( \
>> + __builtin_types_compatible_p(typeof(_val), long *) && sizeof(long) == 8,\
>> + _parse_integer_ll(_s, _base, (void *)_val), \
>> + __builtin_choose_expr( \
>> + __builtin_types_compatible_p(typeof(_val), unsigned long *) && sizeof(unsigned long) == 4,\
>> + _parse_integer_u(_s, _base, (void *)_val), \
>> + __builtin_choose_expr( \
>> + __builtin_types_compatible_p(typeof(_val), unsigned long *) && sizeof(unsigned long) == 8,\
>> + _parse_integer_ull(_s, _base, (void *)_val), \
>
> Ah, I see. In these cases, one probably has to do a cast to pass a
> (long*) as either (int*) or (long long*) - but why not cast to the type
> actually expected by _parse_integer_* instead of the launder-anything (void*)?
First macro was written without casts at all naively thinking
that gcc will only typecheck in chosen __builtin_choose_expr branch.
But it doesn't do that, remove casts and observe million of warnings.
So I shut it up with "void *". Branch is chosen base on __b_t_c_p
expression and I don't think it is possible to sneak in incorrect pointer.
> Another thing: It may be slightly confusing that this can't be used with
> an array passed as val. This won't work:
>
> long x[1];
> rv = parse_integer(s, 0, x);
> One could argue that one should pass &x[0] instead, but since this is a
> macro, gcc doesn't really give a very helpful error (I just get "error:
> invalid initializer"). I think it can be fixed simply by declaring _val
> using typeof(&val[0]).
I'd say &x[0] is way more clear that x in this case, but objection taken.
kstrto*() works in exactly same situation after all.
>> +int _parse_integer_ull(const char *s, unsigned int base, unsigned long long *val)
>> +{
>> + int rv;
>> +
>> + if (*s == '-') {
>> + return -EINVAL;
>> + } else if (*s == '+') {
>> + rv = __parse_integer(s + 1, base, val);
>> + if (rv < 0)
>> + return rv;
>> + return rv + 1;
>> + } else
>> + return __parse_integer(s, base, val);
>> +}
>> +EXPORT_SYMBOL(_parse_integer_ull);
>> +
>> +int _parse_integer_ll(const char *s, unsigned int base, long long *val)
>> +{
>> + unsigned long long tmp;
>> + int rv;
>> +
>> + if (*s == '-') {
>> + rv = __parse_integer(s + 1, base, &tmp);
>> + if (rv < 0)
>> + return rv;
>> + if ((long long)-tmp >= 0)
>> + return -ERANGE;
>
> Is there any reason to disallow "-0"?
No! -0 is not accepted because code is copied from kstrtoll()
which doesn't accept "-0". It is even in the testsuite:
static void __init test_kstrtoll_fail(void)
{
...
/* negative zero isn't an integer in Linux */
{"-0", 0},
{"-0", 8},
{"-0", 10},
{"-0", 16},
Frankly I don't even remember why it does that, and
no one noticed until now. libc functions accept "-0".
next prev parent reply other threads:[~2015-05-04 14:33 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-02 0:47 [PATCH 01/10] Add parse_integer() (replacement for simple_strto*()) Alexey Dobriyan
2015-05-02 0:48 ` [PATCH 02/10] parse_integer: rewrite kstrto*() Alexey Dobriyan
2015-05-02 0:50 ` [PATCH 03/10] parse_integer: convert sscanf() Alexey Dobriyan
2015-05-02 1:10 ` [PATCH CORRECT " Alexey Dobriyan
2015-05-02 0:51 ` [PATCH 04/10] sscanf: fix overflow Alexey Dobriyan
2015-05-05 9:51 ` Rasmus Villemoes
2015-05-05 11:10 ` Alexey Dobriyan
2015-05-06 7:49 ` Rasmus Villemoes
2015-05-02 0:53 ` [PATCH 05/10] parse_integer: convert lib/ Alexey Dobriyan
2015-05-04 14:10 ` Rasmus Villemoes
2015-05-04 14:57 ` Alexey Dobriyan
2015-05-02 0:55 ` [PATCH 06/10] parse_integer: convert mm/ Alexey Dobriyan
2015-05-04 14:33 ` Rasmus Villemoes
2015-05-04 15:09 ` Alexey Dobriyan
2015-05-02 0:56 ` [PATCH 07/10] parse_integer: convert misc fs/ code Alexey Dobriyan
2015-05-02 0:59 ` [PATCH 08/10] fs/cachefiles/: convert to parse_integer() Alexey Dobriyan
2015-05-02 1:01 ` [PATCH 09/10] ocfs2: convert to parse_integer()/kstrto*() Alexey Dobriyan
2015-05-02 1:03 ` [PATCH 10/10] ext2, ext3, ext4: " Alexey Dobriyan
2015-05-04 13:24 ` [PATCH 01/10] Add parse_integer() (replacement for simple_strto*()) Rasmus Villemoes
2015-05-04 14:32 ` Alexey Dobriyan [this message]
2015-05-04 16:44 ` Rasmus Villemoes
2015-05-04 19:54 ` Alexey Dobriyan
2015-05-04 21:48 ` Rasmus Villemoes
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='CACVxJT_=GLvA0D9LGsXnOcj+r8VgFMP4pcL5oM6cQ2CcT2ZNVA@mail.gmail.com' \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
/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;
as well as URLs for NNTP newsgroup(s).