Skip to contentSkip to conversation

iio: temperature: ltc2983: avoid -Wstring-compare warning

PATCH In review Arnd Bergmann 4 participants 5 messages Oct 2 – Oct 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: temperature: ltc2983: avoid -Wstring-compare warning

From: Arnd Bergmann <[email protected]>

When __ltc2983_custom_sensor_new() is inlined into a caller, gcc notices that the string compare can never succeed and warns about this being a possible mistake:

In function '__ltc2983_custom_sensor_new',

    inlined from 'ltc2983_thermistor_new' at drivers/iio/temperature/ltc2983.c:1079:24:
drivers/iio/temperature/ltc2983.c:559:29: error: 'strcmp' of a string of length 24 and an array of size 22 evaluates to nonzero [-Werror=string-compare]
  559 |    if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector"))
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is a false positive warning as the code always works correctly, though the strcmp() is a somewhat elaborate way of identifying the caller.

Rework this by replacing both the strcmp() and the is_steinhart flag with the sensor->type that is always available anyway and identifies with code path gets taken.

Fixes 3dd0c048409e ("iio: temperature: ltc2983: Add support for ADT7604")
---
 drivers/iio/temperature/ltc2983.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
drivers/iio/temperature/ltc2983.c +12 -10
index 6655453c9abf..efad747c9e8d 100644
@@ -482,11 +482,12 @@ static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
 
 static struct ltc2983_custom_sensor *
 __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
-			    const char *propname, const bool is_steinhart,
+			    const char *propname, int type,
 			    const u32 resolution, const bool has_signed)
 {
 	struct ltc2983_custom_sensor *new_custom;
 	struct device *dev = &st->spi->dev;
+	bool is_steinhart = type == LTC2983_SENSOR_THERMISTOR_STEINHART;
 	/*
 	 * For custom steinhart, the full u32 is taken. For all the others
 	 * the MSB is discarded.
@@ -556,7 +557,7 @@ __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle
 			 * to µK so __convert_to_raw() produces the correct hardware
 			 * encoding: P + 273.15 K.
 			 */
-			if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector"))
+			if ((index % 2) != 0 && type == LTC2983_SENSOR_LEAK_DETECTOR)
 				temp = temp * 1000000 + 273150000;
 
 			if ((index % 2) != 0)
@@ -846,7 +847,8 @@ ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data
 		const char *propname = "adi,custom-thermocouple";
 
 		thermo->custom = __ltc2983_custom_sensor_new(st, child,
-							     propname, false,
+							     propname,
+							     sensor->type,
 							     16384, true);
 		if (IS_ERR(thermo->custom))
 			return ERR_CAST(thermo->custom);
@@ -961,7 +963,8 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
 	if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
 		rtd->custom = __ltc2983_custom_sensor_new(st, child,
 							  "adi,custom-rtd",
-							  false, 2048, false);
+							  sensor->type,
+							  2048, false);
 		if (IS_ERR(rtd->custom))
 			return ERR_CAST(rtd->custom);
 	}
@@ -1066,11 +1069,9 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s
 
 	/* check custom sensor */
 	if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
-		bool steinhart = false;
 		const char *propname;
 
 		if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
-			steinhart = true;
 			propname = "adi,custom-steinhart";
 		} else {
 			propname = "adi,custom-thermistor";
@@ -1078,7 +1079,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s
 
 		thermistor->custom = __ltc2983_custom_sensor_new(st, child,
 								 propname,
-								 steinhart,
+								 sensor->type,
 								 64, false);
 		if (IS_ERR(thermistor->custom))
 			return ERR_CAST(thermistor->custom);
@@ -1195,7 +1196,7 @@ ltc2983_copper_trace_new(const struct fwnode_handle *child, struct ltc2983_data
 						 "adi,custom-copper-trace is required for >1 ohm copper trace\n");
 
 		ct->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-copper-trace",
-							 false, 2048, false);
+							 sensor->type, 2048, false);
 		if (IS_ERR(ct->custom))
 			return ERR_CAST(ct->custom);
 
@@ -1286,7 +1287,7 @@ ltc2983_leak_detector_new(const struct fwnode_handle *child, struct ltc2983_data
 					 "adi,custom-leak-detector is required for leak detectors\n");
 
 	ld->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-leak-detector",
-						 false, 16, false);
+						 sensor->type, 16, false);
 	if (IS_ERR(ld->custom))
 		return ERR_CAST(ld->custom);
 
@@ -1499,7 +1500,8 @@ static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
 					 sensor->chan);
 
 	temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
-						   false, 4096, true);
+						   sensor->type,
+						   4096, true);
 	if (IS_ERR(temp->custom))
 		return ERR_CAST(temp->custom);
 
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · On Fri, 2 Oct 2026 14:48:51 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

When __ltc2983_custom_sensor_new() is inlined into a caller, gcc notices that the string compare can never succeed and warns about this being a possible mistake:

In function '__ltc2983_custom_sensor_new',

    inlined from 'ltc2983_thermistor_new' at drivers/iio/temperature/ltc2983.c:1079:24:
drivers/iio/temperature/ltc2983.c:559:29: error: 'strcmp' of a string of length 24 and an array of size 22 evaluates to nonzero [-Werror=string-compare]
  559 |    if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector"))
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is a false positive warning as the code always works correctly, though the strcmp() is a somewhat elaborate way of identifying the caller.

Quoted 7 lines

Rework this by replacing both the strcmp() and the is_steinhart flag with the sensor->type that is always available anyway and identifies with code path gets taken.

Fixes 3dd0c048409e ("iio: temperature: ltc2983: Add support for ADT7604")

---

Makes sense.

Signature · 2 lines
Kind regards,
Joshua Crofts
#3 lore raw
Quoted 4 lines · On Fri, 2 Oct 2026 16:29:22 +0300 Andy Shevchenko…
Quoted 2 lines · Joshua Crofts

On Fri, 2 Oct 2026 14:48:51 +0200
Arnd Bergmann <[email protected]> wrote:

Quoted 9 lines

...

Quoted 3 lines

Makes sense.

There is an ongoing discussion about this
https://lore.kernel.org/linux-iio/20260929-iio-ltc2983-fix-string-compare-v1-1-d67d612d6ddd@gmail.com/

Ah, good point!

Signature · 2 lines
Kind regards,
Joshua Crofts
#4 lore raw
In reply to Arnd Bergmann
Quoted 5 lines · On Fri, 2 Oct 2026 14:48:51 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

When __ltc2983_custom_sensor_new() is inlined into a caller, gcc notices that the string compare can never succeed and warns about this being a possible mistake:

That function looks like it should be marked __noinline_for_bloat :-)

Although doing a strcmp() like that is just silly.

David

Quoted 116 lines

In function '__ltc2983_custom_sensor_new',

    inlined from 'ltc2983_thermistor_new' at drivers/iio/temperature/ltc2983.c:1079:24:
drivers/iio/temperature/ltc2983.c:559:29: error: 'strcmp' of a string of length 24 and an array of size 22 evaluates to nonzero [-Werror=string-compare]
  559 |    if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector"))
      |                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is a false positive warning as the code always works correctly, though the strcmp() is a somewhat elaborate way of identifying the caller.

Rework this by replacing both the strcmp() and the is_steinhart flag with the sensor->type that is always available anyway and identifies with code path gets taken.

Fixes 3dd0c048409e ("iio: temperature: ltc2983: Add support for ADT7604")
---
 drivers/iio/temperature/ltc2983.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
drivers/iio/temperature/ltc2983.c +12 -10
index 6655453c9abf..efad747c9e8d 100644
@@ -482,11 +482,12 @@ static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
 
 static struct ltc2983_custom_sensor *
 __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
-			    const char *propname, const bool is_steinhart,
+			    const char *propname, int type,
 			    const u32 resolution, const bool has_signed)
 {
 	struct ltc2983_custom_sensor *new_custom;
 	struct device *dev = &st->spi->dev;
+	bool is_steinhart = type == LTC2983_SENSOR_THERMISTOR_STEINHART;
 	/*
 	 * For custom steinhart, the full u32 is taken. For all the others
 	 * the MSB is discarded.
@@ -556,7 +557,7 @@ __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle
 			 * to µK so __convert_to_raw() produces the correct hardware
 			 * encoding: P + 273.15 K.
 			 */
-			if ((index % 2) != 0 && !strcmp(propname, "adi,custom-leak-detector"))
+			if ((index % 2) != 0 && type == LTC2983_SENSOR_LEAK_DETECTOR)
 				temp = temp * 1000000 + 273150000;
 
 			if ((index % 2) != 0)
@@ -846,7 +847,8 @@ ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data
 		const char *propname = "adi,custom-thermocouple";
 
 		thermo->custom = __ltc2983_custom_sensor_new(st, child,
-							     propname, false,
+							     propname,
+							     sensor->type,
 							     16384, true);
 		if (IS_ERR(thermo->custom))
 			return ERR_CAST(thermo->custom);
@@ -961,7 +963,8 @@ ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
 	if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
 		rtd->custom = __ltc2983_custom_sensor_new(st, child,
 							  "adi,custom-rtd",
-							  false, 2048, false);
+							  sensor->type,
+							  2048, false);
 		if (IS_ERR(rtd->custom))
 			return ERR_CAST(rtd->custom);
 	}
@@ -1066,11 +1069,9 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s
 
 	/* check custom sensor */
 	if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
-		bool steinhart = false;
 		const char *propname;
 
 		if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
-			steinhart = true;
 			propname = "adi,custom-steinhart";
 		} else {
 			propname = "adi,custom-thermistor";
@@ -1078,7 +1079,7 @@ ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *s
 
 		thermistor->custom = __ltc2983_custom_sensor_new(st, child,
 								 propname,
-								 steinhart,
+								 sensor->type,
 								 64, false);
 		if (IS_ERR(thermistor->custom))
 			return ERR_CAST(thermistor->custom);
@@ -1195,7 +1196,7 @@ ltc2983_copper_trace_new(const struct fwnode_handle *child, struct ltc2983_data
 						 "adi,custom-copper-trace is required for >1 ohm copper trace\n");
 
 		ct->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-copper-trace",
-							 false, 2048, false);
+							 sensor->type, 2048, false);
 		if (IS_ERR(ct->custom))
 			return ERR_CAST(ct->custom);
 
@@ -1286,7 +1287,7 @@ ltc2983_leak_detector_new(const struct fwnode_handle *child, struct ltc2983_data
 					 "adi,custom-leak-detector is required for leak detectors\n");
 
 	ld->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-leak-detector",
-						 false, 16, false);
+						 sensor->type, 16, false);
 	if (IS_ERR(ld->custom))
 		return ERR_CAST(ld->custom);
 
@@ -1499,7 +1500,8 @@ static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
 					 sensor->chan);
 
 	temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
-						   false, 4096, true);
+						   sensor->type,
+						   4096, true);
 	if (IS_ERR(temp->custom))
 		return ERR_CAST(temp->custom);