All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Jan Beulich <JBeulich@suse.com>
Cc: Julien Grall <julien.grall@arm.com>,
	Stefano Stabellini <sstabellini@kernel.org>,
	Wei Liu <wei.liu2@citrix.com>,
	Xen-devel <xen-devel@lists.xen.org>,
	Roger Pau Monne <roger.pau@citrix.com>
Subject: Re: [PATCH] xen/cmdline: Fix buggy strncmp(s, LITERAL, ss - s) construct
Date: Fri, 4 Jan 2019 15:55:36 +0000	[thread overview]
Message-ID: <bff3c33d-a244-362a-529c-32f91b5f3965@citrix.com> (raw)
In-Reply-To: <5C2F7C8D020000780020A422@prv1-mh.provo.novell.com>

On 04/01/2019 15:32, Jan Beulich wrote:
>>>> On 31.12.18 at 18:35, <andrew.cooper3@citrix.com> wrote:
>> --- a/xen/arch/x86/cpu/vpmu.c
>> +++ b/xen/arch/x86/cpu/vpmu.c
>> @@ -61,42 +61,31 @@ static unsigned vpmu_count;
>>  
>>  static DEFINE_PER_CPU(struct vcpu *, last_vcpu);
>>  
>> -static int parse_vpmu_param(const char *s, unsigned int len)
>> -{
>> -    if ( !*s || !len )
>> -        return 0;
>> -    if ( !strncmp(s, "bts", len) )
>> -        vpmu_features |= XENPMU_FEATURE_INTEL_BTS;
>> -    else if ( !strncmp(s, "ipc", len) )
>> -        vpmu_features |= XENPMU_FEATURE_IPC_ONLY;
>> -    else if ( !strncmp(s, "arch", len) )
>> -        vpmu_features |= XENPMU_FEATURE_ARCH_ONLY;
>> -    else
>> -        return 1;
>> -    return 0;
>> -}
>> -
>>  static int __init parse_vpmu_params(const char *s)
>>  {
>> -    const char *sep, *p = s;
>> +    const char *ss;
>>  
>>      switch ( parse_bool(s, NULL) )
>>      {
>>      case 0:
>>          break;
>>      default:
>> -        for ( ; ; )
>> -        {
>> -            sep = strchr(p, ',');
>> -            if ( sep == NULL )
>> -                sep = strchr(p, 0);
>> -            if ( parse_vpmu_param(p, sep - p) )
>> -                goto error;
>> -            if ( !*sep )
>> -                /* reached end of flags */
>> -                break;
>> -            p = sep + 1;
>> -        }
>> +        do {
>> +            ss = strchr(s, ',');
>> +            if ( !ss )
>> +                ss = strchr(s, '\0');
>> +
>> +            if ( !cmdline_strcmp(s, "bts") )
>> +                vpmu_features |= XENPMU_FEATURE_INTEL_BTS;
>> +            else if ( !cmdline_strcmp(s, "ipc") )
>> +                vpmu_features |= XENPMU_FEATURE_IPC_ONLY;
>> +            else if ( !cmdline_strcmp(s, "arch") )
>> +                vpmu_features |= XENPMU_FEATURE_ARCH_ONLY;
>> +            else
>> +                return -EINVAL;
>> +
>> +            s = ss + 1;
>> +        } while ( *ss );
> While presumably also applicable elsewhere, the issue is more
> noticeable here because you introduce "ss" anew:

I don't introduce it.  It was previously named "sep", which I renamed
for consistency.  It is also subtly disguised with a 0 instead of '\0'
and a redundant local variable named p.

> It is now
> unhelpful (in terms of generated code) to calculate ss before
> the various cmdline_strcmp() calls, as the compiler can't know
> (despite the const) that what s points to won't change across
> those calls, and hence has to calculate ss early (and put it into
> a callee saved register or on the stack), as written. If the
> calculation was pulled down, only scratch registers would
> suffice for the compiler to carry out the calculation.
>
> That said - all of this is boot time only code, so not really
> performance critical. It's just that this general structure will
> then further proliferate, and the overall binary size is likely
> going to be (slightly) larger this way.

TBH, I'm not concerned far more with code consistency than efficiency,
given how often we manage to subtly break the command line parsing.

If we do want to come up with a more efficient way of doing this, we can
do a blanket change to our prevailing style.

>
>> --- a/xen/common/kernel.c
>> +++ b/xen/common/kernel.c
>> @@ -227,19 +227,49 @@ int parse_bool(const char *s, const char *e)
>>      if ( !len )
>>          return -1;
>>  
>> -    if ( !strncmp("no", s, len) ||
>> -         !strncmp("off", s, len) ||
>> -         !strncmp("false", s, len) ||
>> -         !strncmp("disable", s, len) ||
>> -         !strncmp("0", s, len) )
>> -        return 0;
>> +    switch ( len )
>> +    {
>> +    case 1:
>> +        if ( *s == '1' )
>> +            return 1;
>> +        else if ( *s == '0' )
> The "else" here is pointless (also further down).

So they are.  I'll drop.

>
>> @@ -271,6 +301,29 @@ int parse_boolean(const char *name, const char *s, const char *e)
>>      return -1;
>>  }
>>  
>> +int cmdline_strcmp(const char *frag, const char *name)
>> +{
>> +    while ( 1 )
> Could I talk you into using "for ( ; ; )" instead (and then perhaps
> moving the two increments up here)? I know gcc doesn't do this,
> but in the general case a compiler warning about such constant
> conditionals is not an entirely bad or wrong thing, so I prefer to
> see such constructs avoided where we reasonably can.

Ok.

>
>> +    {
>> +        int res = (*frag - *name);
> With the result of this being implementation defined (due to plain
> char's implementation defined - often command line controlled
> with an implementation defined default - signedness) I wonder if
> this function can really usefully return "int" rather than "bool".

My CPUID command line parsing needs this to work properly as int, for
bisecting across a sorted list.

I'll add an explicit cast to signed char.  I'll also fix out local libc
functions, which are similarly buggy.

>
>> +        if ( res || *name == '\0' )
>> +        {
>> +            /*
>> +             * NUL in 'name' matching a comma or colon in 'frag' implies
>> +             * success.
>> +             */
>> +            if ( *name == '\0' && (*frag == ',' || *frag == ':') )
> There's only a single (unrelated) use of ; as a separator right
> now (afaics), but adding it here would seem quite desirable to
> me.

Where is ; used, out of interest?  I'm happy to add it, but I didn't
spot it on my audit.

> Also, speaking of (the lack of) tokenization of the command line
> in the caller, wouldn't it make sense to accept white space as
> separators here too?

I'm not sure if that is wise or not.  I can't think of a situation where
you would want that behaviour, rather than finding yourself with a
parsing bug and having to fix a bug elsewhere.

~Andrew

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel

  reply	other threads:[~2019-01-04 15:55 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-31 17:35 [PATCH] xen/cmdline: Fix buggy strncmp(s, LITERAL, ss - s) construct Andrew Cooper
2019-01-02 10:13 ` Roger Pau Monné
2019-01-02 12:18   ` Andrew Cooper
2019-01-02 12:25     ` Roger Pau Monné
2019-01-02 12:28       ` Andrew Cooper
2019-01-04 15:32 ` Jan Beulich
2019-01-04 15:55   ` Andrew Cooper [this message]
2019-01-04 16:25     ` Andrew Cooper
2019-01-04 16:47     ` Jan Beulich

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=bff3c33d-a244-362a-529c-32f91b5f3965@citrix.com \
    --to=andrew.cooper3@citrix.com \
    --cc=JBeulich@suse.com \
    --cc=julien.grall@arm.com \
    --cc=roger.pau@citrix.com \
    --cc=sstabellini@kernel.org \
    --cc=wei.liu2@citrix.com \
    --cc=xen-devel@lists.xen.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.