Skip to contentSkip to conversation

power: supply_core: Pass pointer to battery info

bug report Dan Carpenter 2 participants 2 messages Jan 6, 2022 – Jan 10, 2022

lore
Share & tools
lore
#0 lore raw

Hello Linus Walleij,

The patch 25fd330370ac: "power: supply_core: Pass pointer to battery info" from Dec 15, 2021, leads to the following Smatch static checker warning:

	drivers/power/supply/bq256xx_charger.c:1529 bq256xx_hw_init()
	error: potentially dereferencing uninitialized 'bat_info'.

drivers/power/supply/bq256xx_charger.c

    1505 static int bq256xx_hw_init(struct bq256xx_device *bq)
    1506 {
    1507         struct power_supply_battery_info *bat_info;
    1508         int wd_reg_val = BQ256XX_WATCHDOG_DIS;
    1509         int ret = 0;
    1510         int i;
    1511 
    1512         for (i = 0; i < BQ256XX_NUM_WD_VAL; i++) {
    1513                 if (bq->watchdog_timer == bq256xx_watchdog_time[i]) {
    1514                         wd_reg_val = i;
    1515                         break;
    1516                 }
    1517                 if (bq->watchdog_timer > bq256xx_watchdog_time[i] &&
    1518                     bq->watchdog_timer < bq256xx_watchdog_time[i + 1])
    1519                         wd_reg_val = i;
    1520         }
    1521         ret = regmap_update_bits(bq->regmap, BQ256XX_CHARGER_CONTROL_1,
    1522                                  BQ256XX_WATCHDOG_MASK, wd_reg_val <<
    1523                                                 BQ256XX_WDT_BIT_SHIFT);
    1524 
    1525         ret = power_supply_get_battery_info(bq->charger, &bat_info);

If the first allocation in power_supply_get_battery_info() fails then bat_info is uninitialized. (It's not really possible unless you do failure injection).

    1526         if (ret) {
    1527                 dev_warn(bq->dev, "battery info missing, default values will be applied\n");
    1528 
--> 1529                 bat_info->constant_charge_current_max_ua =
    1530                                 bq->chip_info->bq256xx_def_ichg;
    1531 
    1532                 bat_info->constant_charge_voltage_max_uv =
    1533                                 bq->chip_info->bq256xx_def_vbatreg;
    1534 
    1535                 bat_info->precharge_current_ua =
    1536                                 bq->chip_info->bq256xx_def_iprechg;
    1537 
    1538                 bat_info->charge_term_current_ua =
    1539                                 bq->chip_info->bq256xx_def_iterm;
    1540 
    1541                 bq->init_data.ichg_max =
    1542                                 bq->chip_info->bq256xx_max_ichg;
    1543 
    1544                 bq->init_data.vbatreg_max =
    1545                                 bq->chip_info->bq256xx_max_vbatreg;
    1546         } else {
    1547                 bq->init_data.ichg_max =
    1548                         bat_info->constant_charge_current_max_ua;
    1549 
    1550                 bq->init_data.vbatreg_max =
    1551                         bat_info->constant_charge_voltage_max_uv;
    1552         }
    1553 
    1554         ret = bq->chip_info->bq256xx_set_vindpm(bq, bq->init_data.vindpm);
    1555         if (ret)
    1556                 return ret;
    1557 
    1558         ret = bq->chip_info->bq256xx_set_iindpm(bq, bq->init_data.iindpm);
    1559         if (ret)
    1560                 return ret;
    1561 
    1562         ret = bq->chip_info->bq256xx_set_ichg(bq,
    1563                                 bat_info->constant_charge_current_max_ua);
    1564         if (ret)
    1565                 return ret;
    1566 
    1567         ret = bq->chip_info->bq256xx_set_iprechg(bq,
    1568                                 bat_info->precharge_current_ua);
    1569         if (ret)
    1570                 return ret;
    1571 
    1572         ret = bq->chip_info->bq256xx_set_vbatreg(bq,
    1573                                 bat_info->constant_charge_voltage_max_uv);
    1574         if (ret)
    1575                 return ret;
    1576 
    1577         ret = bq->chip_info->bq256xx_set_iterm(bq,
    1578                                 bat_info->charge_term_current_ua);
    1579         if (ret)
    1580                 return ret;
    1581 
    1582         power_supply_put_battery_info(bq->charger, bat_info);
    1583 
    1584         return 0;
    1585 }

regards,
dan carpenter

#1 lore raw
Quoted 6 lines · Dan Carpenter

The patch 25fd330370ac: "power: supply_core: Pass pointer to battery info" from Dec 15, 2021, leads to the following Smatch static checker warning:

drivers/power/supply/bq256xx_charger.c:1529 bq256xx_hw_init()
error: potentially dereferencing uninitialized 'bat_info'.

(...)

Quoted 10 lines
    1525         ret = power_supply_get_battery_info(bq->charger, &bat_info);

If the first allocation in power_supply_get_battery_info() fails then bat_info is uninitialized. (It's not really possible unless you do failure injection).

    1526         if (ret) {
    1527                 dev_warn(bq->dev, "battery info missing, default values will be applied\n");
    1528

--> 1529 bat_info->constant_charge_current_max_ua =

Ouch, that one was easy to miss! I sent a fixup patch.

Yours,
Linus Walleij