public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Thorsten Blum <thorsten.blum@linux.dev>
To: David Laight <david.laight.linux@gmail.com>
Cc: Krzysztof Kozlowski <krzk@kernel.org>,
	Huisong Li <lihuisong@huawei.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] w1: therm: Use clamp_t to simplify int_to_short helper
Date: Sun, 9 Nov 2025 21:30:00 +0100	[thread overview]
Message-ID: <28C4DAF0-91B9-4175-AC2C-D3B5AC283439@linux.dev> (raw)
In-Reply-To: <20251109162056.0a9cbd52@pumpkin>

On 9. Nov 2025, at 17:20, David Laight wrote:
> On Sun,  9 Nov 2025 13:59:55 +0100
> Thorsten Blum <thorsten.blum@linux.dev> wrote:
> 
>> Use clamp_t() instead of manually casting the return value.
>> 
>> Replace sprintf() with sysfs_emit() to improve sysfs show functions
>> while we're at it.
>> 
>> ...
>> +	/* Cast to short by eliminating out of range values */
>                  ^^^^^ no shorts here...

It's even shorter than short. I didn't even notice...

>> +	return clamp_t(s8, i, MIN_TEMP, MAX_TEMP);
> 
> That is just plain broken.
> clamp_t() really shouldn't have been allowed to exist.
> That is a typical example of how it gets misused.
> (min_t() and max_t() get misused the same way.)
> 
> Think what happens when i is 256.
> The code should just be:
> 
> 	return clamp(i, MIN_TEMP, MAX_TEMP);
> 
> No casts anywhere.

Ok, yeah 256 would be 0 when cast to s8 even though it should be clamped
to MAX_TEMP. Never thought about this side effect of clamp_t(). Will
change it to just clamp() in v2, thanks!

> I'm not even sure the return type (s8) makes any sense.
> It is quite likely that the code will be better if it is 'int'.
> The fact that the domain in inside -128..127 doesn't mean that
> the correct type for a variable isn't 'int'.

The low and high temperatures (s8) are only written to the u8 array
'new_config_register' for which s8 seems fine. What made you think int
might be better?

Thanks,
Thorsten


  reply	other threads:[~2025-11-09 20:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-09 12:59 [PATCH] w1: therm: Use clamp_t to simplify int_to_short helper Thorsten Blum
2025-11-09 16:20 ` David Laight
2025-11-09 20:30   ` Thorsten Blum [this message]
2025-11-09 23:07     ` David Laight

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=28C4DAF0-91B9-4175-AC2C-D3B5AC283439@linux.dev \
    --to=thorsten.blum@linux.dev \
    --cc=david.laight.linux@gmail.com \
    --cc=krzk@kernel.org \
    --cc=lihuisong@huawei.com \
    --cc=linux-kernel@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox