Schwabe/fix curve overflow (#4951)

* Fix curve editing function not checking if there is space for the curve

* Fix compile error
This commit is contained in:
Arne Schwabe
2017-05-24 18:42:56 +02:00
committed by Bertrand Songis
parent c79994d812
commit 4880c029b7
4 changed files with 59 additions and 38 deletions
+16 -1
View File
@@ -26,6 +26,7 @@ uint8_t s_curveChan;
int8_t * curveEnd[MAX_CURVES]; int8_t * curveEnd[MAX_CURVES];
void loadCurves() void loadCurves()
{ {
bool showWarning= false;
int8_t * tmp = g_model.points; int8_t * tmp = g_model.points;
for (int i=0; i<MAX_CURVES; i++) { for (int i=0; i<MAX_CURVES; i++) {
switch (g_model.curves[i].type) { switch (g_model.curves[i].type) {
@@ -41,7 +42,21 @@ void loadCurves()
tmp += 5+g_model.curves[i].points; tmp += 5+g_model.curves[i].points;
break; break;
} }
// Older version did not check if we exceeded the array
int8_t * maxend = &g_model.points[MAX_CURVE_POINTS - 2*(MAX_CURVES-i-1)];
if (tmp > maxend) {
tmp = maxend;
g_model.curves[i].type=CURVE_TYPE_STANDARD;
g_model.curves[i].points=-3;
showWarning=true;
}
curveEnd[i] = tmp; curveEnd[i] = tmp;
}
if (showWarning) {
POPUP_WARNING("Invalid curve data repaired");
const char * w = "check your curves, logic switches";
SET_WARNING_INFO(w, strlen(w), 0);
} }
} }
@@ -50,7 +65,7 @@ int8_t * curveAddress(uint8_t idx)
return idx==0 ? g_model.points : curveEnd[idx-1]; return idx==0 ? g_model.points : curveEnd[idx-1];
} }
bool moveCurve(uint8_t index, int8_t shift) // TODO bool? bool moveCurve(uint8_t index, int8_t shift)
{ {
if (curveEnd[MAX_CURVES-1] + shift > g_model.points + sizeof(g_model.points)) { if (curveEnd[MAX_CURVES-1] + shift > g_model.points + sizeof(g_model.points)) {
AUDIO_WARNING2(); AUDIO_WARNING2();
+15 -13
View File
@@ -87,11 +87,12 @@ void menuModelCurveOne(event_t event)
for (int i = 1; i < 4 + crv.points; i++) { for (int i = 1; i < 4 + crv.points; i++) {
points[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5 + crv.points, i)), s_curveChan)); points[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5 + crv.points, i)), s_curveChan));
} }
moveCurve(s_curveChan, checkIncDec_Ret > 0 ? 3 + crv.points : -3 - crv.points); if (moveCurve(s_curveChan, checkIncDec_Ret > 0 ? 3 + crv.points : -3 - crv.points)) {
if (newType == CURVE_TYPE_CUSTOM) { if (newType == CURVE_TYPE_CUSTOM) {
resetCustomCurveX(points, 5 + crv.points); resetCustomCurveX(points, 5 + crv.points);
}
crv.type = newType;
} }
crv.type = newType;
} }
} }
@@ -105,16 +106,17 @@ void menuModelCurveOne(event_t event)
if (checkIncDec_Ret) { if (checkIncDec_Ret) {
int8_t newPoints[MAX_POINTS_PER_CURVE]; int8_t newPoints[MAX_POINTS_PER_CURVE];
newPoints[0] = points[0]; newPoints[0] = points[0];
newPoints[4+count] = points[4+crv.points]; newPoints[4 + count] = points[4 + crv.points];
for (int i=1; i<4+count; i++) for (int i = 1; i < 4 + count; i++)
newPoints[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5+count, i)), s_curveChan)); newPoints[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5 + count, i)), s_curveChan));
moveCurve(s_curveChan, checkIncDec_Ret*(crv.type==CURVE_TYPE_CUSTOM?2:1)); if (moveCurve(s_curveChan, checkIncDec_Ret * (crv.type == CURVE_TYPE_CUSTOM ? 2 : 1))) {
for (int i=0; i<5+count; i++) { for (int i = 0; i < 5 + count; i++) {
points[i] = newPoints[i]; points[i] = newPoints[i];
if (crv.type == CURVE_TYPE_CUSTOM && i!=0 && i!=4+count) if (crv.type == CURVE_TYPE_CUSTOM && i != 0 && i != 4 + count)
points[5+count+i-1] = getCurveX(5+count, i); points[5 + count + i - 1] = getCurveX(5 + count, i);
}
crv.points = count;
} }
crv.points = count;
} }
} }
+15 -13
View File
@@ -91,11 +91,12 @@ void menuModelCurveOne(event_t event)
for (int i = 1; i < 4 + crv.points; i++) { for (int i = 1; i < 4 + crv.points; i++) {
points[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5 + crv.points, i)), s_curveChan)); points[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5 + crv.points, i)), s_curveChan));
} }
moveCurve(s_curveChan, checkIncDec_Ret > 0 ? 3 + crv.points : -3 - crv.points); if (moveCurve(s_curveChan, checkIncDec_Ret > 0 ? 3 + crv.points : -3 - crv.points)) {
if (newType == CURVE_TYPE_CUSTOM) { if (newType == CURVE_TYPE_CUSTOM) {
resetCustomCurveX(points, 5 + crv.points); resetCustomCurveX(points, 5 + crv.points);
}
crv.type = newType;
} }
crv.type = newType;
} }
} }
@@ -109,16 +110,17 @@ void menuModelCurveOne(event_t event)
if (checkIncDec_Ret) { if (checkIncDec_Ret) {
int8_t newPoints[MAX_POINTS_PER_CURVE]; int8_t newPoints[MAX_POINTS_PER_CURVE];
newPoints[0] = points[0]; newPoints[0] = points[0];
newPoints[4+count] = points[4+crv.points]; newPoints[4 + count] = points[4 + crv.points];
for (int i=1; i<4+count; i++) for (int i = 1; i < 4 + count; i++)
newPoints[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5+count, i)), s_curveChan)); newPoints[i] = calcRESXto100(applyCustomCurve(calc100toRESX(getCurveX(5 + count, i)), s_curveChan));
moveCurve(s_curveChan, checkIncDec_Ret*(crv.type==CURVE_TYPE_CUSTOM?2:1)); if (moveCurve(s_curveChan, checkIncDec_Ret * (crv.type == CURVE_TYPE_CUSTOM ? 2 : 1))) {
for (int i=0; i<5+count; i++) { for (int i = 0; i < 5 + count; i++) {
points[i] = newPoints[i]; points[i] = newPoints[i];
if (crv.type == CURVE_TYPE_CUSTOM && i!=0 && i!=4+count) if (crv.type == CURVE_TYPE_CUSTOM && i != 0 && i != 4 + count)
points[5+count+i-1] = getCurveX(5+count, i); points[5 + count + i - 1] = getCurveX(5 + count, i);
}
crv.points = count;
} }
crv.points = count;
} }
} }
+13 -11
View File
@@ -117,14 +117,15 @@ bool menuModelCurveOne(event_t event)
if (attr) { if (attr) {
uint8_t newType = checkIncDecModelZero(event, crv.type, CURVE_TYPE_LAST); uint8_t newType = checkIncDecModelZero(event, crv.type, CURVE_TYPE_LAST);
if (newType != crv.type) { if (newType != crv.type) {
for (int i=1; i<4+crv.points; i++) { for (int i = 1; i < 4 + crv.points; i++) {
points[i] = calcRESXto100(applyCustomCurve(calc100toRESX(-100 + i * 200 / (4 + crv.points)), s_curveChan)); points[i] = calcRESXto100(applyCustomCurve(calc100toRESX(-100 + i * 200 / (4 + crv.points)), s_curveChan));
} }
moveCurve(s_curveChan, checkIncDec_Ret > 0 ? 3+crv.points : -3-crv.points); if (moveCurve(s_curveChan, checkIncDec_Ret > 0 ? 3 + crv.points : -3 - crv.points)) {
if (newType == CURVE_TYPE_CUSTOM) { if (newType == CURVE_TYPE_CUSTOM) {
resetCustomCurveX(points, 5+crv.points); resetCustomCurveX(points, 5 + crv.points);
}
crv.type = newType;
} }
crv.type = newType;
} }
} }
@@ -140,13 +141,14 @@ bool menuModelCurveOne(event_t event)
newPoints[4+count] = points[4+crv.points]; newPoints[4+count] = points[4+crv.points];
for (int i=1; i<4+count; i++) for (int i=1; i<4+count; i++)
newPoints[i] = calcRESXto100(applyCustomCurve(calc100toRESX(-100 + (i*200) / (4+count)), s_curveChan)); newPoints[i] = calcRESXto100(applyCustomCurve(calc100toRESX(-100 + (i*200) / (4+count)), s_curveChan));
moveCurve(s_curveChan, checkIncDec_Ret*(crv.type==CURVE_TYPE_CUSTOM?2:1)); if (moveCurve(s_curveChan, checkIncDec_Ret*(crv.type==CURVE_TYPE_CUSTOM?2:1))) {
for (int i=0; i<5+count; i++) { for (int i = 0; i < 5 + count; i++) {
points[i] = newPoints[i]; points[i] = newPoints[i];
if (crv.type == CURVE_TYPE_CUSTOM && i!=0 && i!=4+count) if (crv.type == CURVE_TYPE_CUSTOM && i != 0 && i != 4 + count)
points[5+count+i-1] = -100 + (i*200) / (4+count); points[5 + count + i - 1] = -100 + (i * 200) / (4 + count);
}
crv.points = count;
} }
crv.points = count;
} }
} }