From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: "Pradhan, Sanman" <sanman.pradhan@hpe.com>
Cc: "linux-hwmon@vger.kernel.org" <linux-hwmon@vger.kernel.org>,
"linux@roeck-us.net" <linux@roeck-us.net>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
Sanman Pradhan <psanman@juniper.net>
Subject: Re: [PATCH v2] hwmon: (tps53679) Fix device ID comparison and printing in tps53676_identify()
Date: Wed, 3 Jun 2026 10:12:02 +0200 [thread overview]
Message-ID: <ah_h0m1JTCzJuLlY@black.igk.intel.com> (raw)
In-Reply-To: <20260330155618.77403-1-sanman.pradhan@hpe.com>
On Mon, Mar 30, 2026 at 03:56:40PM +0000, Pradhan, Sanman wrote:
>
> tps53676_identify() uses strncmp() to compare the device ID buffer
> against a byte sequence containing embedded non-printable bytes
> (\x53\x67\x60). strncmp() is semantically wrong for binary data
> comparison; use memcmp() instead.
>
> Additionally, the buffer from i2c_smbus_read_block_data() is not
> NUL-terminated, so printing it with "%s" in the error path is
> undefined behavior and may read past the buffer. Use "%*ph" to
> hex-dump the actual bytes returned.
>
> Per the datasheet, the expected device ID is the 6-byte sequence
> 54 49 53 67 60 00 ("TI\x53\x67\x60\x00"), so compare all 6 bytes
> including the trailing NUL.
Your patch seems okay to me. But just for your information I would do it
differently. Id est the sequence you got for the comparison is the perfect
ASCII one, but this is a coincident as the actual ID is in FourCC BCD.
The better check is to compare first two bytes to TI and device ID separately
as FourCC. Do you want me to send a patch or you can do it yourself?
(Also note that there is no need to use *ph as we know the length and it's
always the same, so %6ph suffice, but again, you want to print the FourCC,
%p4c.)
...
> ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
> if (ret < 0)
> return ret;
> - if (strncmp("TI\x53\x67\x60", buf, 5)) {
> - dev_err(&client->dev, "Unexpected device ID: %s\n", buf);
> + if (ret != 6 || memcmp(buf, "TI\x53\x67\x60\x00", 6)) {
> + dev_err(&client->dev, "Unexpected device ID: %*ph\n", ret, buf);
> return -ENODEV;
> }
#define TPS53676_DEVICE_ID_MAGIC 0x53676000
if (ret != 6)
dev_err(&client->dev, "Malformed response\n");
return -EIO;
}
if (strncmp(buf, "TI", 2)) {
dev_err(&client->dev, "Unexpected vendor ID: %2pE\n", buf); // use %pE since it maybe unprintable
return -ENODEV;
}
if (get_unaligned_be32(buf + 2) != _DEVICE_ID_MAGIC) {
dev_err(&client->dev, "Unexpected device ID: %p4c\n", buf + 2);
return -ENODEV;
}
--
With Best Regards,
Andy Shevchenko
prev parent reply other threads:[~2026-06-03 8:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-30 15:56 [PATCH v2] hwmon: (tps53679) Fix device ID comparison and printing in tps53676_identify() Pradhan, Sanman
2026-03-30 17:47 ` Guenter Roeck
2026-06-03 8:12 ` Andy Shevchenko [this message]
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=ah_h0m1JTCzJuLlY@black.igk.intel.com \
--to=andriy.shevchenko@intel.com \
--cc=linux-hwmon@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=psanman@juniper.net \
--cc=sanman.pradhan@hpe.com \
/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