Skip to contentSkip to conversation

w1: ds2482: Fix signedness bug in ds2482_w1_triplet()

PATCH New Babanpreet Singh 1 participant 1 message Jul 14 W1 dallas's 1-wire bus

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 w1: ds2482: Fix signedness bug in ds2482_w1_triplet()

ds2482_wait_1wire_idle() returns the status register value (0..255) on success, or a negative value on I2C failure: -1 when selecting the status register fails, or a negative errno from i2c_smbus_read_byte().

ds2482_w1_triplet() feeds that result into "return (status >> 5);" without checking for errors, and the function returns u8. For a negative status the arithmetic shift keeps the sign and the u8 truncation fabricates a triplet result whose meaning depends on the errno value: -1 and -EIO happen to become 0xff, whose set low bits make w1_search() abort, but -ETIMEDOUT (-110 >> 5 = -4) becomes 0xfc - "devices responded on both branches, wrote 1" - and -EOPNOTSUPP (-95 >> 5 = -3) becomes 0xfd - "only the zero branch responded". w1_search() then continues the ROM search with a fabricated direction bit instead of aborting, and the corrupted id is either rejected by the ROM CRC (existing device missed) or registers a phantom slave.

The function already defines an in-band error value: status is initialized to (3 << 5), which decodes to 3 (both branch bits set, "no device responded") and makes w1_search() terminate the search when sending the triplet command fails. Decode a negative status to the same value.

Found by smatch:
drivers/w1/masters/ds2482.c:314 ds2482_w1_triplet() warn: signedness bug returning '(-67108864)'

Fixes baf12ae29ab4 ("[PATCH] W1: Add the DS2482 I2C-to-w1 bridge driver.")
Assisted-by Claude:claude-sonnet-5

---
Note: ds2482_w1_touch_bit() has a related imperfection (it masks a possibly negative status with DS2482_REG_STS_SBR), but its u8 0/1 ops contract has no in-band error value at all, so a correct fix there needs a w1 ops-interface change - deliberately not part of this minimal fix. Happy to look into that as a follow-up if there is interest.

 drivers/w1/masters/ds2482.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/w1/masters/ds2482.c +4 -0
index 0069e6f854d7..7622c8782844 100644
@@ -310,6 +310,10 @@ static u8 ds2482_w1_triplet(void *data, u8 dbit)
 
 	mutex_unlock(&pdev->access_lock);
 
+	/* On bus error, decode to 3 (no device responded) to abort the search */
+	if (status < 0)
+		status = 3 << 5;
+
 	/* Decode the status */
 	return (status >> 5);
 }

base-commit: 3b029c035b34bbc693405ddf759f0e9b920c27f1

Signature · 1 line
2.43.0