hwmon (ina2xx) Fix NULL id pointer in probe()
authorNicolin Chen <nicoleotsuka@gmail.com>
Sat, 10 Nov 2018 00:42:14 +0000 (16:42 -0800)
committerGuenter Roeck <linux@roeck-us.net>
Tue, 13 Nov 2018 17:33:32 +0000 (09:33 -0800)
commit70df9ebbd82c794ddfbb49d45b337f18d5588dc2
treebd888cd7c31d16d40ac98b2c8227ef0f673fe7d0
parentccda4af0f4b92f7b4c308d3acc262f4a7e3affad
hwmon (ina2xx) Fix NULL id pointer in probe()

When using DT configurations, the id pointer might turn out to
be NULL. Then the driver encounters NULL pointer access:

  Unable to handle kernel read from unreadable memory at vaddr 00000018
  [...]
  PC is at ina2xx_probe+0x114/0x200
  LR is at ina2xx_probe+0x10c/0x200
  [...]
  Kernel panic - not syncing: Attempted to kill init! exitcode=0x0000000b

The reason is that i2c core returns the id pointer by matching
id_table with client->name, while the client->name is actually
using the name from the first string in the DT compatible list,
not the best one. So i2c core would fail to match the id_table
if the best matched compatible string isn't the first one, and
then would return a NULL id pointer.

This probably should be fixed in i2c core. But it doesn't hurt
to make the driver robust. So this patch fixes it by using the
"chip" that's added to unify both DT and non-DT configurations.

Additionally, since id pointer could be null, so as id->name:
  ina2xx 10-0047: power monitor (null) (Rshunt = 1000 uOhm)
  ina2xx 10-0048: power monitor (null) (Rshunt = 10000 uOhm)

So this patch also fixes NULL name pointer, using client->name
to play safe and to align with hwmon->name.

Fixes: bd0ddd4d0883 ("hwmon: (ina2xx) Add OF device ID table")
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
drivers/hwmon/ina2xx.c