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 defined(CPUARM)
if (CFN_PARAM(cfn)>=FUNC_RESET_PARAM_FIRST_TELEM) { if (CFN_PARAM(cfn)>=FUNC_RESET_PARAM_FIRST_TELEM) {
TelemetryItem * telemetryItem = & telemetryItems[CFN_PARAM(cfn)-FUNC_RESET_PARAM_FIRST_TELEM]; uint8_t item = CFN_PARAM(cfn)-FUNC_RESET_PARAM_FIRST_TELEM;
telemetryItem->clear(); if (item < MAX_SENSORS) {
telemetryItems[item].clear();
}
} }
#endif #endif
break; 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) 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]; TelemetryItem & telemetryItem = telemetryItems[channel];
TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel]; TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel];
if (telemetrySensor.unit == UNIT_DATETIME) { 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) 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]; TelemetryItem & telemetryItem = telemetryItems[channel];
TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel]; TelemetrySensor & telemetrySensor = g_model.telemetrySensors[channel];
if (telemetrySensor.unit == UNIT_DATETIME) { if (telemetrySensor.unit == UNIT_DATETIME) {

View file

@ -236,22 +236,26 @@ void displayTopBar()
/* Rx voltage */ /* Rx voltage */
altitude_icon_x = batt_icon_x+7*FW+3; altitude_icon_x = batt_icon_x+7*FW+3;
if (g_model.frsky.voltsSource) { if (g_model.frsky.voltsSource) {
TelemetryItem & voltsItem = telemetryItems[g_model.frsky.voltsSource-1]; uint8_t item = g_model.frsky.voltsSource-1;
if (voltsItem.isAvailable()) { if (item < MAX_SENSORS) {
putsTelemetryChannelValue(batt_icon_x+7*FW+2, BAR_Y+1, g_model.frsky.voltsSource-1, voltsItem.value, LEFT); TelemetryItem & voltsItem = telemetryItems[item];
altitude_icon_x = lcdLastPos+1; if (voltsItem.isAvailable()) {
putsTelemetryChannelValue(batt_icon_x+7*FW+2, BAR_Y+1, item, voltsItem.value, LEFT);
altitude_icon_x = lcdLastPos+1;
}
} }
} }
/* Altitude */ /* Altitude */
if (g_model.frsky.altitudeSource) { if (g_model.frsky.altitudeSource) {
TelemetryItem & altitudeItem = telemetryItems[g_model.frsky.altitudeSource-1]; uint8_t item = g_model.frsky.altitudeSource-1;
if (altitudeItem.isAvailable()) { if (item < MAX_SENSORS) {
LCD_ICON(altitude_icon_x, BAR_Y, ICON_ALTITUDE); TelemetryItem & altitudeItem = telemetryItems[item];
int32_t value = altitudeItem.value; if (altitudeItem.isAvailable()) {
TelemetrySensor & sensor = g_model.telemetrySensors[g_model.frsky.altitudeSource-1]; LCD_ICON(altitude_icon_x, BAR_Y, ICON_ALTITUDE);
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); putsValueWithUnit(altitude_icon_x+2*FW-1, BAR_Y+1, value, UNIT_METERS, LEFT);
}
} }
} }
} }

View file

@ -196,7 +196,7 @@ static void luaGetValueAndPush(int src)
break; break;
default: default:
if (telemetrySensor.prec > 0) if (telemetrySensor.prec > 0)
lua_pushnumber(L, float(value)/(telemetrySensor.prec == 2 ? 100.0 : 10.0)); lua_pushnumber(L, float(value)/telemetrySensor.getPrecDivisor());
else else
lua_pushinteger(L, value); lua_pushinteger(L, value);
break; 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(const char *label, uint8_t unit=UNIT_RAW, uint8_t prec=0);
void init(uint16_t id); void init(uint16_t id);
bool isAvailable(); bool isAvailable() const;
int32_t getValue(int32_t value, uint8_t unit, uint8_t prec) const; int32_t getValue(int32_t value, uint8_t unit, uint8_t prec) const;
bool isConfigurable(); bool isConfigurable() const;
bool isPrecConfigurable(); bool isPrecConfigurable() const;
uint32_t getPrecMultiplier() const;
uint32_t getPrecDivisor() const;
}) TelemetrySensor; }) TelemetrySensor;
#endif #endif

View file

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