1
0
Fork 0
mirror of https://github.com/opentx/opentx.git synced 2025-07-23 00:05:17 +03:00

Out of bounds protection for telemetryItems[] array access and some code refactoring for telemetry value precision

This commit is contained in:
Damjan Adamic 2015-10-18 11:46:25 +02:00
parent 4ef9913f76
commit 082ba0893d
8 changed files with 49 additions and 28 deletions

View file

@ -367,8 +367,10 @@ void evalFunctions()
}
#if defined(CPUARM)
if (CFN_PARAM(cfn)>=FUNC_RESET_PARAM_FIRST_TELEM) {
TelemetryItem * telemetryItem = & telemetryItems[CFN_PARAM(cfn)-FUNC_RESET_PARAM_FIRST_TELEM];
telemetryItem->clear();
uint8_t item = CFN_PARAM(cfn)-FUNC_RESET_PARAM_FIRST_TELEM;
if (item < MAX_SENSORS) {
telemetryItems[item].clear();
}
}
#endif
break;

View file

@ -881,6 +881,7 @@ void displayGpsCoords(coord_t x, coord_t y, TelemetryItem & telemetryItem, LcdFl
void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t value, LcdFlags att)
{
if (channel >= MAX_SENSORS) return;
TelemetryItem & telemetryItem = telemetryItems[channel];
TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel];
if (telemetrySensor.unit == UNIT_DATETIME) {

View file

@ -952,6 +952,7 @@ void displayGpsCoords(coord_t x, coord_t y, TelemetryItem & telemetryItem, LcdFl
void putsTelemetryChannelValue(coord_t x, coord_t y, uint8_t channel, lcdint_t value, LcdFlags att)
{
if (channel >= MAX_SENSORS) return; //Lua luaLcdDrawChannel() can call us with a bad value
TelemetryItem & telemetryItem = telemetryItems[channel];
TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel];
if (telemetrySensor.unit == UNIT_DATETIME) {

View file

@ -236,25 +236,29 @@ void displayTopBar()
/* Rx voltage */
altitude_icon_x = batt_icon_x+7*FW+3;
if (g_model.frsky.voltsSource) {
TelemetryItem & voltsItem = telemetryItems[g_model.frsky.voltsSource-1];
uint8_t item = g_model.frsky.voltsSource-1;
if (item < MAX_SENSORS) {
TelemetryItem & voltsItem = telemetryItems[item];
if (voltsItem.isAvailable()) {
putsTelemetryChannelValue(batt_icon_x+7*FW+2, BAR_Y+1, g_model.frsky.voltsSource-1, voltsItem.value, LEFT);
putsTelemetryChannelValue(batt_icon_x+7*FW+2, BAR_Y+1, item, voltsItem.value, LEFT);
altitude_icon_x = lcdLastPos+1;
}
}
}
/* Altitude */
if (g_model.frsky.altitudeSource) {
TelemetryItem & altitudeItem = telemetryItems[g_model.frsky.altitudeSource-1];
uint8_t item = g_model.frsky.altitudeSource-1;
if (item < MAX_SENSORS) {
TelemetryItem & altitudeItem = telemetryItems[item];
if (altitudeItem.isAvailable()) {
LCD_ICON(altitude_icon_x, BAR_Y, ICON_ALTITUDE);
int32_t value = altitudeItem.value;
TelemetrySensor & sensor = g_model.telemetrySensors[g_model.frsky.altitudeSource-1];
if (sensor.prec) value /= sensor.prec == 2 ? 100 : 10;
int32_t value = altitudeItem.value / g_model.telemetrySensors[item].getPrecDivisor();
putsValueWithUnit(altitude_icon_x+2*FW-1, BAR_Y+1, value, UNIT_METERS, LEFT);
}
}
}
}
/* Notifs icons */
coord_t x = BAR_NOTIFS_X;

View file

@ -196,7 +196,7 @@ static void luaGetValueAndPush(int src)
break;
default:
if (telemetrySensor.prec > 0)
lua_pushnumber(L, float(value)/(telemetrySensor.prec == 2 ? 100.0 : 10.0));
lua_pushnumber(L, float(value)/telemetrySensor.getPrecDivisor());
else
lua_pushinteger(L, value);
break;

View file

@ -1313,10 +1313,12 @@ PACK(typedef struct {
};
void init(const char *label, uint8_t unit=UNIT_RAW, uint8_t prec=0);
void init(uint16_t id);
bool isAvailable();
bool isAvailable() const;
int32_t getValue(int32_t value, uint8_t unit, uint8_t prec) const;
bool isConfigurable();
bool isPrecConfigurable();
bool isConfigurable() const;
bool isPrecConfigurable() const;
uint32_t getPrecMultiplier() const;
uint32_t getPrecDivisor() const;
}) TelemetrySensor;
#endif

View file

@ -366,10 +366,7 @@ void TelemetryItem::eval(const TelemetrySensor & sensor)
result += dist*dist;
if (altItem) {
dist = abs(altItem->value);
uint8_t prec = g_model.telemetrySensors[sensor.dist.alt-1].prec;
if (prec > 0)
dist /= (prec==2 ? 100 : 10);
dist = abs(altItem->value) / g_model.telemetrySensors[sensor.dist.alt-1].getPrecDivisor();
result += dist*dist;
}
@ -545,7 +542,7 @@ void TelemetrySensor::init(uint16_t id)
init(label);
}
bool TelemetrySensor::isAvailable()
bool TelemetrySensor::isAvailable() const
{
return ZLEN(label) > 0;
}
@ -622,7 +619,7 @@ int32_t TelemetrySensor::getValue(int32_t value, uint8_t unit, uint8_t prec) con
return value;
}
bool TelemetrySensor::isConfigurable()
bool TelemetrySensor::isConfigurable() const
{
if (type == TELEM_TYPE_CALCULATED) {
if (formula >= TELEM_FORMULA_CELL) {
@ -637,7 +634,7 @@ bool TelemetrySensor::isConfigurable()
return true;
}
bool TelemetrySensor::isPrecConfigurable()
bool TelemetrySensor::isPrecConfigurable() const
{
if (isConfigurable()) {
return true;
@ -649,3 +646,17 @@ bool TelemetrySensor::isPrecConfigurable()
return false;
}
}
uint32_t TelemetrySensor::getPrecMultiplier() const
{
if (prec == 2) return 1;
if (prec == 1) return 10;
return 100;
}
uint32_t TelemetrySensor::getPrecDivisor() const
{
if (prec == 2) return 100;
if (prec == 1) return 10;
return 1;
}

View file

@ -47,10 +47,10 @@ void varioWakeup()
int verticalSpeed = 0;
if (g_model.frsky.varioSource) {
TelemetryItem & varioItem = telemetryItems[g_model.frsky.varioSource-1];
verticalSpeed = varioItem.value;
TelemetrySensor & sensor = g_model.telemetrySensors[g_model.frsky.varioSource-1];
if (sensor.prec != 2) verticalSpeed *= sensor.prec == 0 ? 100 : 10;
uint8_t item = g_model.frsky.varioSource-1;
if (item < MAX_SENSORS) {
verticalSpeed = telemetryItems[item].value * g_model.telemetrySensors[item].getPrecMultiplier();
}
}
int varioCenterMin = (int)g_model.frsky.varioCenterMin * 10 - 50;