From 7de7ef8e8c80fa8bf54739d613038a982564c4df Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Mon, 3 Jul 2023 17:00:43 +0200 Subject: [PATCH] fix some crashes when changing presets This fixes some of the crashes I had when changing presets. still not a full solution ... --- wled00/FX_fcn.cpp | 5 +++-- wled00/json.cpp | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 2550853cb..a3383640b 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -90,7 +90,7 @@ Segment::Segment(const Segment &orig) { if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); } if (orig._t) { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); } - if (orig.leds && !Segment::_globalLeds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } + if (orig.leds && !Segment::_globalLeds && length() > 0) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } } // move constructor @@ -125,7 +125,7 @@ Segment& Segment::operator= (const Segment &orig) { if (orig.name) { name = new char[strlen(orig.name)+1]; if (name) strcpy(name, orig.name); } if (orig.data) { if (allocateData(orig._dataLen)) memcpy(data, orig.data, orig._dataLen); } if (orig._t) { _t = new Transition(orig._t->_dur, orig._t->_briT, orig._t->_cctT, orig._t->_colorT); } - if (orig.leds && !Segment::_globalLeds) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } + if (orig.leds && !Segment::_globalLeds && length() > 0) { leds = (CRGB*)malloc(sizeof(CRGB)*length()); if (leds) memcpy(leds, orig.leds, sizeof(CRGB)*length()); } } return *this; } @@ -547,6 +547,7 @@ uint16_t Segment::virtualLength() const { } #endif uint16_t groupLen = groupLength(); + if (groupLen < 1) groupLen = 1; // prevent division by zero - better safe than sorry ... uint16_t vLength = (length() + groupLen - 1) / groupLen; if (mirror) vLength = (vLength + 1) /2; // divide by 2 if mirror, leave at least a single LED return vLength; diff --git a/wled00/json.cpp b/wled00/json.cpp index ef74267d4..0d9dd7268 100644 --- a/wled00/json.cpp +++ b/wled00/json.cpp @@ -34,7 +34,7 @@ bool deserializeSegment(JsonObject elem, byte it, byte presetId) uint16_t start = elem["start"] | seg.start; if (stop < 0) { - uint16_t len = elem["len"]; + int len = elem["len"]; stop = (len > 0) ? start + len : seg.stop; } // 2D segments @@ -473,7 +473,7 @@ void serializeSegment(JsonObject& root, Segment& seg, byte id, bool forPreset, b root[F("stopY")] = seg.stopY; } } - if (!forPreset) root["len"] = seg.stop - seg.start; + if (!forPreset) root["len"] = (seg.stop >= seg.start) ? (seg.stop - seg.start) : 0; root["grp"] = seg.grouping; root[F("spc")] = seg.spacing; root[F("of")] = seg.offset;