[PATCH v3] iio: chemical: scd30: Cleanup initializations and fix sign-extension bug

Maxwell Doose posted 1 patch 1 week, 5 days ago
drivers/iio/chemical/scd30_core.c | 19 +++++++++++++++----
1 file changed, 15 insertions(+), 4 deletions(-)
[PATCH v3] iio: chemical: scd30: Cleanup initializations and fix sign-extension bug
Posted by Maxwell Doose 1 week, 5 days ago
Include linux/bitfield.h for FIELD_GET().

Create new macros for bit manipulation in combination with manual bit
manipulation being replaced with FIELD_GET().

The current variable declaration and initializations are barely readable
and use comma separations across multiple lines. Refactor the
initializations so that mantissa and exp have separate declarations and
sign gets initialized later.

In addition (and due to the nature of the cleanup), fix a sign-extension
bug where, float32 would get bitwise anded with ~BIT(31)
(which is 0xFFFFFFFF7FFFFFFF) which corrupted the exponent.

Fixes: 64b3d8b1b0f5c ("iio: chemical: scd30: add core driver")
Reported-by: sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260524020309.18618-1-m32285159%40gmail.com
Signed-off-by: Maxwell Doose <m32285159@gmail.com>
---
v2:
 - Added new floating point macro constants per Jonathan's suggestion.
 - Included linux/bitfield.h to use FIELD_GET() since Jonathan also
   recommended its use.
v3:
 - Added fixes, reported-by, and closes tags per Jonathan's suggestion.
 - Added explaination for fix per Jonathan's suggestion.
 - Removed unneeded comment.
 - Changed commit subject.

 drivers/iio/chemical/scd30_core.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/chemical/scd30_core.c b/drivers/iio/chemical/scd30_core.c
index a665fcb78806..79124f4d7b9b 100644
--- a/drivers/iio/chemical/scd30_core.c
+++ b/drivers/iio/chemical/scd30_core.c
@@ -4,6 +4,8 @@
  *
  * Copyright (c) 2020 Tomasz Duszynski <tomasz.duszynski@octakon.com>
  */
+
+#include <linux/bitfield.h>
 #include <linux/bits.h>
 #include <linux/cleanup.h>
 #include <linux/completion.h>
@@ -43,6 +45,11 @@
 #define SCD30_TEMP_OFFSET_MAX 655360
 #define SCD30_EXTRA_TIMEOUT_PER_S 250
 
+/* Floating point arithmetic macros */
+#define SCD30_FLOAT_MANTISSA_MSK GENMASK(22, 0)
+#define SCD30_FLOAT_EXP_MSK GENMASK(30, 23)
+#define SCD30_FLOAT_SIGN_MSK BIT(31)
+
 enum {
 	SCD30_CONC,
 	SCD30_TEMP,
@@ -89,10 +96,14 @@ static int scd30_reset(struct scd30_state *state)
 /* simplified float to fixed point conversion with a scaling factor of 0.01 */
 static int scd30_float_to_fp(int float32)
 {
-	int fraction, shift,
-	    mantissa = float32 & GENMASK(22, 0),
-	    sign = (float32 & BIT(31)) ? -1 : 1,
-	    exp = (float32 & ~BIT(31)) >> 23;
+	int fraction, shift, sign;
+	int mantissa = FIELD_GET(SCD30_FLOAT_MANTISSA_MSK, float32);
+	int exp = FIELD_GET(SCD30_FLOAT_EXP_MSK, float32);
+
+	if (float32 & SCD30_FLOAT_SIGN_MSK)
+		sign = -1;
+	else
+		sign = 1;
 
 	/* special case 0 */
 	if (!exp && !mantissa)
-- 
2.54.0
Re: [PATCH v3] iio: chemical: scd30: Cleanup initializations and fix sign-extension bug
Posted by Jonathan Cameron 1 week, 5 days ago
On Tue, 26 May 2026 17:55:24 -0500
Maxwell Doose <m32285159@gmail.com> wrote:

> Include linux/bitfield.h for FIELD_GET().
> 
> Create new macros for bit manipulation in combination with manual bit
> manipulation being replaced with FIELD_GET().
> 
> The current variable declaration and initializations are barely readable
> and use comma separations across multiple lines. Refactor the
> initializations so that mantissa and exp have separate declarations and
> sign gets initialized later.
> 
> In addition (and due to the nature of the cleanup), fix a sign-extension
> bug where, float32 would get bitwise anded with ~BIT(31)
> (which is 0xFFFFFFFF7FFFFFFF) which corrupted the exponent.
> 
> Fixes: 64b3d8b1b0f5c ("iio: chemical: scd30: add core driver")
> Reported-by: sashiko <sashiko-bot@kernel.org>
> Closes: https://sashiko.dev/#/patchset/20260524020309.18618-1-m32285159%40gmail.com
> Signed-off-by: Maxwell Doose <m32285159@gmail.com>

I've applied this to the fixes-togreg branch of iio.git and marked
it for stable. 

For the record I also messed around in compiler explorer and it seems
that for at least some values sashiko was correct in that the current
code doesn't give the same intermediate values on 32bit and 64bit
(I tested x86 but should apply elsewhere).  

In at least some cases it was actually fine (via undefined behaviour
from a massive shift getting masked) so this may well not have been
visible in testing. We should not be relying on that though so good
to have the fix in place.  Note I only checked a few values, so it was
not exhaustive.

Thanks,

Jonathan 
> ---
> v2:
>  - Added new floating point macro constants per Jonathan's suggestion.
>  - Included linux/bitfield.h to use FIELD_GET() since Jonathan also
>    recommended its use.
> v3:
>  - Added fixes, reported-by, and closes tags per Jonathan's suggestion.
>  - Added explaination for fix per Jonathan's suggestion.
>  - Removed unneeded comment.
>  - Changed commit subject.
> 
>  drivers/iio/chemical/scd30_core.c | 19 +++++++++++++++----
>  1 file changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/chemical/scd30_core.c b/drivers/iio/chemical/scd30_core.c
> index a665fcb78806..79124f4d7b9b 100644
> --- a/drivers/iio/chemical/scd30_core.c
> +++ b/drivers/iio/chemical/scd30_core.c
> @@ -4,6 +4,8 @@
>   *
>   * Copyright (c) 2020 Tomasz Duszynski <tomasz.duszynski@octakon.com>
>   */
> +
> +#include <linux/bitfield.h>
>  #include <linux/bits.h>
>  #include <linux/cleanup.h>
>  #include <linux/completion.h>
> @@ -43,6 +45,11 @@
>  #define SCD30_TEMP_OFFSET_MAX 655360
>  #define SCD30_EXTRA_TIMEOUT_PER_S 250
>  
> +/* Floating point arithmetic macros */
> +#define SCD30_FLOAT_MANTISSA_MSK GENMASK(22, 0)
> +#define SCD30_FLOAT_EXP_MSK GENMASK(30, 23)
> +#define SCD30_FLOAT_SIGN_MSK BIT(31)
> +
>  enum {
>  	SCD30_CONC,
>  	SCD30_TEMP,
> @@ -89,10 +96,14 @@ static int scd30_reset(struct scd30_state *state)
>  /* simplified float to fixed point conversion with a scaling factor of 0.01 */
>  static int scd30_float_to_fp(int float32)
>  {
> -	int fraction, shift,
> -	    mantissa = float32 & GENMASK(22, 0),
> -	    sign = (float32 & BIT(31)) ? -1 : 1,
> -	    exp = (float32 & ~BIT(31)) >> 23;
> +	int fraction, shift, sign;
> +	int mantissa = FIELD_GET(SCD30_FLOAT_MANTISSA_MSK, float32);
> +	int exp = FIELD_GET(SCD30_FLOAT_EXP_MSK, float32);
> +
> +	if (float32 & SCD30_FLOAT_SIGN_MSK)
> +		sign = -1;
> +	else
> +		sign = 1;
>  
>  	/* special case 0 */
>  	if (!exp && !mantissa)