Skip to contentSkip to conversation

drivers/devfreq/devfreq.c:1255 devfreq_add_governor() warn: variable dereferenced before check 'devfreq->governor' (see line 1252)

PATCH New Dan Carpenter 1 participant 1 message Oct 27, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 10/11 drivers/devfreq/devfreq.c:1255 devfreq_add_governor() warn: variable dereferenced before check 'devfreq->governor' (see line 1252)

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/chanwoo/linux.git devfreq-testing-sysfs
head:   39383545c0e470caa41717dcde9b208a2436a4d3
commit: 20aff4c41da16a69a8a80670a2892e08b51dd4aa [10/11] PM / devfreq: Remove redundant governor_name from struct devfreq
config: x86_64-randconfig-m001-20201020 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0

If you fix the issue, kindly add following tag as appropriate

New smatch warnings:
drivers/devfreq/devfreq.c:1255 devfreq_add_governor() warn: variable dereferenced before check 'devfreq->governor' (see line 1252) drivers/devfreq/devfreq.c:1317 devfreq_remove_governor() warn: variable dereferenced before check 'devfreq->governor' (see line 1314)

vim +1255 drivers/devfreq/devfreq.c

3aa173b8db200b Nishanth Menon 2012-10-29  1226  int devfreq_add_governor(struct devfreq_governor *governor)
3aa173b8db200b Nishanth Menon 2012-10-29  1227  {
3aa173b8db200b Nishanth Menon 2012-10-29  1228  	struct devfreq_governor *g;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1229  	struct devfreq *devfreq;
3aa173b8db200b Nishanth Menon 2012-10-29  1230  	int err = 0;
3aa173b8db200b Nishanth Menon 2012-10-29  1231  
3aa173b8db200b Nishanth Menon 2012-10-29  1232  	if (!governor) {
3aa173b8db200b Nishanth Menon 2012-10-29  1233  		pr_err("%s: Invalid parameters.\n", __func__);
3aa173b8db200b Nishanth Menon 2012-10-29  1234  		return -EINVAL;
3aa173b8db200b Nishanth Menon 2012-10-29  1235  	}
9f3bdd4f937a75 Axel Lin       2011-11-14  1236  
a3c98b8b2ede1f MyungJoo Ham   2011-10-02  1237  	mutex_lock(&devfreq_list_lock);
3aa173b8db200b Nishanth Menon 2012-10-29  1238  	g = find_devfreq_governor(governor->name);
3aa173b8db200b Nishanth Menon 2012-10-29  1239  	if (!IS_ERR(g)) {
3aa173b8db200b Nishanth Menon 2012-10-29  1240  		pr_err("%s: governor %s already registered\n", __func__,
3aa173b8db200b Nishanth Menon 2012-10-29  1241  		       g->name);
3aa173b8db200b Nishanth Menon 2012-10-29  1242  		err = -EINVAL;
3aa173b8db200b Nishanth Menon 2012-10-29  1243  		goto err_out;
3aa173b8db200b Nishanth Menon 2012-10-29  1244  	}
3aa173b8db200b Nishanth Menon 2012-10-29  1245  
3aa173b8db200b Nishanth Menon 2012-10-29  1246  	list_add(&governor->node, &devfreq_governor_list);
3aa173b8db200b Nishanth Menon 2012-10-29  1247  
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1248  	list_for_each_entry(devfreq, &devfreq_list, node) {
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1249  		int ret = 0;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1250  		struct device *dev = devfreq->dev.parent;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1251  
20aff4c41da16a Chanwoo Choi   2020-10-20 @1252  		if (!strncmp(devfreq->governor->name, governor->name,
                                                                             ^^^^^^^^^^^^^^^^^^^^^^^

Dereferenced here.

1b5c1be2c88e84 Nishanth Menon 2012-10-29  1253  			     DEVFREQ_NAME_LEN)) {
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1254  			/* The following should never occur */
1b5c1be2c88e84 Nishanth Menon 2012-10-29 @1255  			if (devfreq->governor) {
                                                                            ^^^^^^^^^^^^^^^^^

Checked too late. The comment say that this can't happen so hopefully this whole if statement can be deleted.

1b5c1be2c88e84 Nishanth Menon 2012-10-29  1256  				dev_warn(dev,
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1257  					 "%s: Governor %s already present\n",
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1258  					 __func__, devfreq->governor->name);
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1259  				ret = devfreq->governor->event_handler(devfreq,
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1260  							DEVFREQ_GOV_STOP, NULL);
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1261  				if (ret) {
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1262  					dev_warn(dev,
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1263  						 "%s: Governor %s stop = %d\n",
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1264  						 __func__,
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1265  						 devfreq->governor->name, ret);
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1266  				}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1267  				/* Fall through */
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1268  			}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1269  			devfreq->governor = governor;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1270  			ret = devfreq->governor->event_handler(devfreq,
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1271  						DEVFREQ_GOV_START, NULL);
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1272  			if (ret) {
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1273  				dev_warn(dev, "%s: Governor %s start=%d\n",
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1274  					 __func__, devfreq->governor->name,
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1275  					 ret);
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1276  			}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1277  		}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1278  	}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1279  
3aa173b8db200b Nishanth Menon 2012-10-29  1280  err_out:
a3c98b8b2ede1f MyungJoo Ham   2011-10-02  1281  	mutex_unlock(&devfreq_list_lock);
3aa173b8db200b Nishanth Menon 2012-10-29  1282  
3aa173b8db200b Nishanth Menon 2012-10-29  1283  	return err;
a3c98b8b2ede1f MyungJoo Ham   2011-10-02  1284  }
3aa173b8db200b Nishanth Menon 2012-10-29  1285  EXPORT_SYMBOL(devfreq_add_governor);
3aa173b8db200b Nishanth Menon 2012-10-29  1286  
3aa173b8db200b Nishanth Menon 2012-10-29  1287  /**
bafeb42bd80fb2 MyungJoo Ham   2016-11-09  1288   * devfreq_remove_governor() - Remove devfreq feature from a device.
3aa173b8db200b Nishanth Menon 2012-10-29  1289   * @governor:	the devfreq governor to be removed
3aa173b8db200b Nishanth Menon 2012-10-29  1290   */
3aa173b8db200b Nishanth Menon 2012-10-29  1291  int devfreq_remove_governor(struct devfreq_governor *governor)
3aa173b8db200b Nishanth Menon 2012-10-29  1292  {
3aa173b8db200b Nishanth Menon 2012-10-29  1293  	struct devfreq_governor *g;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1294  	struct devfreq *devfreq;
3aa173b8db200b Nishanth Menon 2012-10-29  1295  	int err = 0;
3aa173b8db200b Nishanth Menon 2012-10-29  1296  
3aa173b8db200b Nishanth Menon 2012-10-29  1297  	if (!governor) {
3aa173b8db200b Nishanth Menon 2012-10-29  1298  		pr_err("%s: Invalid parameters.\n", __func__);
3aa173b8db200b Nishanth Menon 2012-10-29  1299  		return -EINVAL;
a3c98b8b2ede1f MyungJoo Ham   2011-10-02  1300  	}
a3c98b8b2ede1f MyungJoo Ham   2011-10-02  1301  
3aa173b8db200b Nishanth Menon 2012-10-29  1302  	mutex_lock(&devfreq_list_lock);
3aa173b8db200b Nishanth Menon 2012-10-29  1303  	g = find_devfreq_governor(governor->name);
3aa173b8db200b Nishanth Menon 2012-10-29  1304  	if (IS_ERR(g)) {
3aa173b8db200b Nishanth Menon 2012-10-29  1305  		pr_err("%s: governor %s not registered\n", __func__,
b9e1c8e821e776 Sachin Kamat   2012-11-21  1306  		       governor->name);
f9c08e2acbbed1 Sachin Kamat   2012-11-21  1307  		err = PTR_ERR(g);
3aa173b8db200b Nishanth Menon 2012-10-29  1308  		goto err_out;
3aa173b8db200b Nishanth Menon 2012-10-29  1309  	}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1310  	list_for_each_entry(devfreq, &devfreq_list, node) {
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1311  		int ret;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1312  		struct device *dev = devfreq->dev.parent;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1313  
20aff4c41da16a Chanwoo Choi   2020-10-20 @1314  		if (!strncmp(devfreq->governor->name, governor->name,
                                                                             ^^^^^^^^^^^^^^^^^^^^^^^

Dereference

1b5c1be2c88e84 Nishanth Menon 2012-10-29  1315  			     DEVFREQ_NAME_LEN)) {
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1316  			/* we should have a devfreq governor! */
1b5c1be2c88e84 Nishanth Menon 2012-10-29 @1317  			if (!devfreq->governor) {
                                                                             ^^^^^^^^^^^^^^^^^

Too late!

1b5c1be2c88e84 Nishanth Menon 2012-10-29  1318  				dev_warn(dev, "%s: Governor %s NOT present\n",
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1319  					 __func__, governor->name);
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1320  				continue;
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1321  				/* Fall through */
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1322  			}
1b5c1be2c88e84 Nishanth Menon 2012-10-29  1323  			ret = devfreq->governor->event_handler(devfreq,

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org