All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: Sergey Organov <sorganov@gmail.com>
Cc: Walter Harms <wharms@bfs.de>,
	David Laight <David.Laight@ACULAB.COM>,
	Joel Stanley <joel@jms.id.au>, Andrew Jeffery <andrew@aj.id.au>,
	"Chia-Wei, Wang" <chiawei_wang@aspeedtech.com>,
	Jae Hyun Yoo <jae.hyun.yoo@intel.com>,
	John Wang <wangzhiqiang.bj@bytedance.com>,
	Brad Bishop <bradleyb@fuzziesquirrel.com>,
	Patrick Venture <venture@google.com>,
	Benjamin Fair <benjaminfair@google.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Robert Lippert <rlippert@google.com>,
	"linux-aspeed@lists.ozlabs.org" <linux-aspeed@lists.ozlabs.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"kernel-janitors@vger.kernel.org"
	<kernel-janitors@vger.kernel.org>
Subject: Re: AW: [PATCH] soc: aspeed: fix a ternary sign expansion bug
Date: Fri, 23 Apr 2021 18:24:59 +0300	[thread overview]
Message-ID: <20210423152459.GU1959@kadam> (raw)
In-Reply-To: <878s59rrn0.fsf@osv.gnss.ru>

On Fri, Apr 23, 2021 at 05:40:19PM +0300, Sergey Organov wrote:
> Walter Harms <wharms@bfs.de> writes:
> 
> > as indepentent observer,
> > i would go for Dans solution:
> >
> > ret = kfifo_to_user();
> > /* if an error occurs just return */
> > if (ret)
> >    return ret;
> >
> > /* otherwise return the copied number of bytes */
> >
> > return copied;
> >
> > there is no need for any deeper language knowledge,
> 
> Yep, but this is not idiomatic C, so one looking at this code would
> tend to convert it back to ternary, and the actual problem here is that
> the type of 'copied' does not match the return type of the function.
>

I help maintain drivers/staging.  I would hope that no one would send us
a patch like this because it's not a checkpatch or CodingStyle violation.
But people have sent us these before and Greg NAKs them because he
doesn't like ternaries.  I NAK them because I like my success path kept
separate from the failure path.  I want the success path indented one
tab and the failure path indented two tabs.  I like when code is written
ploddingly, without fanciness, or combining multiple things on one line.

Using a ternary in this context seems to me like it falls under the
anti-pattern of "making the last call in a function weird".  A lot of
times people change from failure handling to success handling for the
last function call.

	err = one();
	if (err)
		goto fail;
	err = two();
	if (err)
		goto fail;
	err = three();
	if (!err)
		return 0;
goto fail:
	print("failed!\n");

It seems crazy, but people do this all the time!  It's fine to do:

	return three();

There are some maintainers who insist that it should be:

	err = three();
	if (err)
		return err;
	return 0;

I don't go as far as that.  But I also do like when I can glance at the
function and there is a giant "return 0;" at the bottom.

Anyway, if people change it back to ternary then the kbuild bot will
send them a warning message and they'll learn about an odd quirk in C's
type promotion rules.

regards,
dan carpenter

WARNING: multiple messages have this Message-ID (diff)
From: Dan Carpenter <dan.carpenter@oracle.com>
To: linux-aspeed@lists.ozlabs.org
Subject: AW: [PATCH] soc: aspeed: fix a ternary sign expansion bug
Date: Fri, 23 Apr 2021 18:24:59 +0300	[thread overview]
Message-ID: <20210423152459.GU1959@kadam> (raw)
In-Reply-To: <878s59rrn0.fsf@osv.gnss.ru>

On Fri, Apr 23, 2021 at 05:40:19PM +0300, Sergey Organov wrote:
> Walter Harms <wharms@bfs.de> writes:
> 
> > as indepentent observer,
> > i would go for Dans solution:
> >
> > ret = kfifo_to_user();
> > /* if an error occurs just return */
> > if (ret)
> >    return ret;
> >
> > /* otherwise return the copied number of bytes */
> >
> > return copied;
> >
> > there is no need for any deeper language knowledge,
> 
> Yep, but this is not idiomatic C, so one looking at this code would
> tend to convert it back to ternary, and the actual problem here is that
> the type of 'copied' does not match the return type of the function.
>

I help maintain drivers/staging.  I would hope that no one would send us
a patch like this because it's not a checkpatch or CodingStyle violation.
But people have sent us these before and Greg NAKs them because he
doesn't like ternaries.  I NAK them because I like my success path kept
separate from the failure path.  I want the success path indented one
tab and the failure path indented two tabs.  I like when code is written
ploddingly, without fanciness, or combining multiple things on one line.

Using a ternary in this context seems to me like it falls under the
anti-pattern of "making the last call in a function weird".  A lot of
times people change from failure handling to success handling for the
last function call.

	err = one();
	if (err)
		goto fail;
	err = two();
	if (err)
		goto fail;
	err = three();
	if (!err)
		return 0;
goto fail:
	print("failed!\n");

It seems crazy, but people do this all the time!  It's fine to do:

	return three();

There are some maintainers who insist that it should be:

	err = three();
	if (err)
		return err;
	return 0;

I don't go as far as that.  But I also do like when I can glance at the
function and there is a giant "return 0;" at the bottom.

Anyway, if people change it back to ternary then the kbuild bot will
send them a warning message and they'll learn about an odd quirk in C's
type promotion rules.

regards,
dan carpenter

  parent reply	other threads:[~2021-04-23 15:25 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-22  9:11 [PATCH] soc: aspeed: fix a ternary sign expansion bug Dan Carpenter
2021-04-22  9:11 ` Dan Carpenter
2021-04-22  9:24 ` Al Viro
2021-04-22  9:24   ` Al Viro
2021-04-22  9:26   ` Al Viro
2021-04-22  9:26     ` Al Viro
2021-04-22 14:56 ` Patrick Venture
2021-04-22 14:56   ` Patrick Venture
2021-04-22 16:21 ` David Laight
2021-04-22 16:21   ` David Laight
2021-04-23  0:07   ` Joel Stanley
2021-04-23  0:07     ` Joel Stanley
2021-04-23  7:43   ` Dan Carpenter
2021-04-23  7:43     ` Dan Carpenter
2021-04-23 10:45   ` Sergey Organov
2021-04-23 10:45     ` Sergey Organov
2021-04-23 10:54     ` David Laight
2021-04-23 10:54       ` David Laight
2021-04-23 11:03       ` AW: " Walter Harms
2021-04-23 11:03         ` Walter Harms
2021-04-23 14:40         ` Sergey Organov
2021-04-23 14:40           ` Sergey Organov
2021-04-23 14:55           ` David Laight
2021-04-23 14:55             ` David Laight
2021-04-23 15:24           ` Dan Carpenter [this message]
2021-04-23 15:24             ` Dan Carpenter
2021-04-23 11:14     ` Dan Carpenter
2021-04-23 11:14       ` Dan Carpenter

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=20210423152459.GU1959@kadam \
    --to=dan.carpenter@oracle.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=andrew@aj.id.au \
    --cc=benjaminfair@google.com \
    --cc=bradleyb@fuzziesquirrel.com \
    --cc=chiawei_wang@aspeedtech.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jae.hyun.yoo@intel.com \
    --cc=joel@jms.id.au \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-aspeed@lists.ozlabs.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rlippert@google.com \
    --cc=sorganov@gmail.com \
    --cc=venture@google.com \
    --cc=wangzhiqiang.bj@bytedance.com \
    --cc=wharms@bfs.de \
    /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.