drivers/thermal/thermal_helpers.c:123 thermal_zone_get_temp() error: testing array offset 'tz->id' after use.
PATCH 66/69 drivers/thermal/thermal_helpers.c:123 thermal_zone_get_temp() error: testing array offset 'tz->id' after use.
tree: https://github.com/tobetter/linux odroid-5.9.y head: 2f810ed6f99dfcdb64b0df67d13f1632038ddf70 commit: 26ef3eed39da0a6d6d5d35c9e1d1da9d032a78bf [66/69] ODROID-XU4: Update hack avoiding the invalid temperature by TMU broken config: i386-randconfig-m021-20200902 (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
smatch warnings:drivers/thermal/thermal_helpers.c:123 thermal_zone_get_temp() error: testing array offset 'tz->id' after use.
# https://github.com/tobetter/linux/commit/26ef3eed39da0a6d6d5d35c9e1d1da9d032a78bf
git remote add tobetter-linux https://github.com/tobetter/linux
git fetch --no-tags tobetter-linux odroid-5.9.y
git checkout 26ef3eed39da0a6d6d5d35c9e1d1da9d032a78bf
vim +123 drivers/thermal/thermal_helpers.c
cd221c7b638f83 Eduardo Valentin 2016-11-07 82 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp)
cd221c7b638f83 Eduardo Valentin 2016-11-07 83 {
cd221c7b638f83 Eduardo Valentin 2016-11-07 84 int ret = -EINVAL;
cd221c7b638f83 Eduardo Valentin 2016-11-07 85 int count;
cd221c7b638f83 Eduardo Valentin 2016-11-07 86 int crit_temp = INT_MAX;
cd221c7b638f83 Eduardo Valentin 2016-11-07 87 enum thermal_trip_type type;
cd221c7b638f83 Eduardo Valentin 2016-11-07 88
cd221c7b638f83 Eduardo Valentin 2016-11-07 89 if (!tz || IS_ERR(tz) || !tz->ops->get_temp)
cd221c7b638f83 Eduardo Valentin 2016-11-07 90 goto exit;
cd221c7b638f83 Eduardo Valentin 2016-11-07 91
cd221c7b638f83 Eduardo Valentin 2016-11-07 92 mutex_lock(&tz->lock);
cd221c7b638f83 Eduardo Valentin 2016-11-07 93
cd221c7b638f83 Eduardo Valentin 2016-11-07 94 ret = tz->ops->get_temp(tz, temp);
cd221c7b638f83 Eduardo Valentin 2016-11-07 95
cd221c7b638f83 Eduardo Valentin 2016-11-07 96 if (IS_ENABLED(CONFIG_THERMAL_EMULATION) && tz->emul_temperature) {
cd221c7b638f83 Eduardo Valentin 2016-11-07 97 for (count = 0; count < tz->trips; count++) {
cd221c7b638f83 Eduardo Valentin 2016-11-07 98 ret = tz->ops->get_trip_type(tz, count, &type);
cd221c7b638f83 Eduardo Valentin 2016-11-07 99 if (!ret && type == THERMAL_TRIP_CRITICAL) {
cd221c7b638f83 Eduardo Valentin 2016-11-07 100 ret = tz->ops->get_trip_temp(tz, count,
cd221c7b638f83 Eduardo Valentin 2016-11-07 101 &crit_temp);
cd221c7b638f83 Eduardo Valentin 2016-11-07 102 break;
cd221c7b638f83 Eduardo Valentin 2016-11-07 103 }
cd221c7b638f83 Eduardo Valentin 2016-11-07 104 }
cd221c7b638f83 Eduardo Valentin 2016-11-07 105
cd221c7b638f83 Eduardo Valentin 2016-11-07 106 /*
cd221c7b638f83 Eduardo Valentin 2016-11-07 107 * Only allow emulating a temperature when the real temperature
cd221c7b638f83 Eduardo Valentin 2016-11-07 108 * is below the critical temperature so that the emulation code
cd221c7b638f83 Eduardo Valentin 2016-11-07 109 * cannot hide critical conditions.
cd221c7b638f83 Eduardo Valentin 2016-11-07 110 */
cd221c7b638f83 Eduardo Valentin 2016-11-07 111 if (!ret && *temp < crit_temp)
cd221c7b638f83 Eduardo Valentin 2016-11-07 112 *temp = tz->emul_temperature;
cd221c7b638f83 Eduardo Valentin 2016-11-07 113 }
cd221c7b638f83 Eduardo Valentin 2016-11-07 114
26ef3eed39da0a charles.park 2018-06-01 115 /* save thermal_zone data */
26ef3eed39da0a charles.park 2018-06-01 116 if (!ret)
26ef3eed39da0a charles.park 2018-06-01 117 thermal_zone_data[tz->id] = *temp;
^^^^^^^^^^^^^^^^^^^^^^^^^
26ef3eed39da0a charles.park 2018-06-01 118 /*
26ef3eed39da0a charles.park 2018-06-01 119 * This case is that the thermal sensor is broken.
26ef3eed39da0a charles.park 2018-06-01 120 * That's not real temperature. Set the fake temperature value in order to
26ef3eed39da0a charles.park 2018-06-01 121 * avoid reaching the ciritical temperature.
26ef3eed39da0a charles.park 2018-06-01 122 */
26ef3eed39da0a charles.park 2018-06-01 @123 if ((thermal_zone_data[tz->id] > CRITICAL_TEMP) && (tz->id != 4)) {
^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^If "tz->id" is 4 then we have already corrupted memory because that's one element off the end of the thermal_zone_data[] array.
26ef3eed39da0a charles.park 2018-06-01 124 int i, broken_sensor = 0, correct_temp = 0;
26ef3eed39da0a charles.park 2018-06-01 125 for (i = 0; i < 4; i++) {
26ef3eed39da0a charles.park 2018-06-01 126 if ((thermal_zone_data[i] <= CRITICAL_TEMP) &&
26ef3eed39da0a charles.park 2018-06-01 127 (correct_temp <= thermal_zone_data[i]))
26ef3eed39da0a charles.park 2018-06-01 128 correct_temp = thermal_zone_data[i];
26ef3eed39da0a charles.park 2018-06-01 129 if (thermal_zone_data[i] > CRITICAL_TEMP)
26ef3eed39da0a charles.park 2018-06-01 130 broken_sensor++;
26ef3eed39da0a charles.park 2018-06-01 131 }
26ef3eed39da0a charles.park 2018-06-01 132 /*
26ef3eed39da0a charles.park 2018-06-01 133 * if all thermal sensor broken then critical temperature data send
26ef3eed39da0a charles.park 2018-06-01 134 * for system poweroff.
26ef3eed39da0a charles.park 2018-06-01 135 */
26ef3eed39da0a charles.park 2018-06-01 136 *temp = (broken_sensor == 4) ? CRITICAL_TEMP : correct_temp;
26ef3eed39da0a charles.park 2018-06-01 137 }
26ef3eed39da0a charles.park 2018-06-01 138
cd221c7b638f83 Eduardo Valentin 2016-11-07 139 mutex_unlock(&tz->lock);
cd221c7b638f83 Eduardo Valentin 2016-11-07 140 exit:
cd221c7b638f83 Eduardo Valentin 2016-11-07 141 return ret;
cd221c7b638f83 Eduardo Valentin 2016-11-07 142 }---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org