From 29578bf9e15463168476bc4af22dd5fca5298d57 Mon Sep 17 00:00:00 2001 From: mathis Date: Thu, 15 Sep 2022 21:49:09 +0200 Subject: [PATCH] some ASan fixes --- .../datachannel-wasm/wasm/js/websocket.js | 20 ++++++------- OpenGL/src/ui/Component.cpp | 10 ++++--- OpenGL/src/util/AnimationMixer.cpp | 29 ++++++++++--------- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/Dependencies/datachannel-wasm/wasm/js/websocket.js b/Dependencies/datachannel-wasm/wasm/js/websocket.js index 95fd570..becd994 100644 --- a/Dependencies/datachannel-wasm/wasm/js/websocket.js +++ b/Dependencies/datachannel-wasm/wasm/js/websocket.js @@ -59,8 +59,8 @@ wsSetOpenCallback: function(ws, openCallback) { var webSocket = WEBSOCKET.map[ws]; var cb = function() { - if(webSocket.rtcUserDeleted) return; - var userPointer = webSocket.rtcUserPointer || 0; + if(webSocket.wsUserDeleted) return; + var userPointer = webSocket.wsUserPointer || 0; Module['dynCall']('vi', openCallback, [userPointer]); }; webSocket.onopen = cb; @@ -70,8 +70,8 @@ wsSetErrorCallback: function(ws, errorCallback) { var webSocket = WEBSOCKET.map[ws]; var cb = function() { - if(webSocket.rtcUserDeleted) return; - var userPointer = webSocket.rtcUserPointer || 0; + if(webSocket.wsUserDeleted) return; + var userPointer = webSocket.wsUserPointer || 0; Module['dynCall']('vii', errorCallback, [0, userPointer]); }; webSocket.onerror = cb; @@ -80,10 +80,10 @@ wsSetMessageCallback: function(ws, messageCallback) { var webSocket = WEBSOCKET.map[ws]; webSocket.onmessage = function(evt) { - if(webSocket.rtcUserDeleted) return; + if(webSocket.wsUserDeleted) return; if(typeof evt.data == 'string') { var pStr = WEBSOCKET.allocUTF8FromString(evt.data); - var userPointer = webSocket.rtcUserPointer || 0; + var userPointer = webSocket.wsUserPointer || 0; Module['dynCall']('viii', messageCallback, [pStr, -1, userPointer]); _free(pStr); } else { @@ -92,14 +92,14 @@ var pBuffer = _malloc(size); var heapBytes = new Uint8Array(Module['HEAPU8'].buffer, pBuffer, size); heapBytes.set(byteArray); - var userPointer = webSocket.rtcUserPointer || 0; + var userPointer = webSocket.wsUserPointer || 0; Module['dynCall']('viii', messageCallback, [pBuffer, size, userPointer]); _free(pBuffer); } }; webSocket.onclose = function() { - if(webSocket.rtcUserDeleted) return; - var userPointer = webSocket.rtcUserPointer || 0; + if(webSocket.wsUserDeleted) return; + var userPointer = webSocket.wsUserPointer || 0; Module['dynCall']('viii', messageCallback, [0, 0, userPointer]); }; }, @@ -125,7 +125,7 @@ wsSetUserPointer: function(ws, ptr) { var webSocket = WEBSOCKET.map[ws]; - if(webSocket) webSocket.rtcUserPointer = ptr; + if(webSocket) webSocket.wsUserPointer = ptr; }, }; diff --git a/OpenGL/src/ui/Component.cpp b/OpenGL/src/ui/Component.cpp index 51d5588..1565219 100644 --- a/OpenGL/src/ui/Component.cpp +++ b/OpenGL/src/ui/Component.cpp @@ -338,7 +338,7 @@ Component* Component::findCommonAncestor(Component* other) { auto ancestorsB = other->findAncestors(); int i = 0; int count = std::min(ancestorsA.size(), ancestorsB.size()); - while (ancestorsA[i] == ancestorsB[i] && i < count) { + while (i < count && ancestorsA[i] == ancestorsB[i]) { i++; } if (i == 0) return nullptr; @@ -354,8 +354,8 @@ Component* Component::findRoot() { void Component::handleEvent(Event const& evt) { Component* target = findTargetComponent(evt.getPosition()); - Component* ancestor; if(!target) return; + Component* ancestor; // un-hover if(hoverComponent) { @@ -391,8 +391,10 @@ void Component::handleEvent(Event const& evt) { // bubble event (child-to-parent) bool stop = false; comp = target; - while(!stop && comp) { - stop = comp->applyEvent(evt); + // notice that applyEvent may result in the component being destroyed, in which case + // we absolutely don't want to call comp->parent. + // comp MUST return true (stop propagation) in that case. + while(comp && !comp->applyEvent(evt)) { comp = comp->parent; } } diff --git a/OpenGL/src/util/AnimationMixer.cpp b/OpenGL/src/util/AnimationMixer.cpp index 38fe394..bf5f3a1 100644 --- a/OpenGL/src/util/AnimationMixer.cpp +++ b/OpenGL/src/util/AnimationMixer.cpp @@ -11,16 +11,15 @@ void AnimationMixer::addAnim(Animation animType, Spline relatedAnimation) { glm::vec3 AnimationMixer::computeAnim(uint32_t dt) { glm::vec3 target; - while (dt > 0) - { - if(transition != nullptr) { + while (dt > 0) { + if(transition != nullptr) { target = transition->compute(dt); if(dt > 0) transition = nullptr; } - else { - target = animations.at(currentAnimation).compute(dt); - if(dt > 0) { - std::vector > keyframes; + else { + target = animations.at(currentAnimation).compute(dt); + if(dt > 0) { + std::vector > keyframes; Spline* current = &animations.at(currentAnimation); Spline* next = &animations.at(nextAnimation); @@ -33,7 +32,7 @@ glm::vec3 AnimationMixer::computeAnim(uint32_t dt) { current->reset(); currentAnimation = nextAnimation; nextAnimation = Animation::Idle; - } + } } } return target; @@ -44,24 +43,26 @@ void AnimationMixer::setAnimation(Animation anim) { if(currentAnimation != anim && currentAnimation != Animation::Dab && (currentAnimation != Animation::Break || anim == Animation::Break)) { std::vector > keyframes; Spline* current; - if(transition != nullptr) + if(transition) current = transition.get(); else current = &animations.at(currentAnimation); - Spline* next = &animations.at(anim); + Spline* next = &animations.at(anim); uint32_t transitionTime = (uint32_t)(distance(current->getCurrentPoint(), next->getCurrentPoint()) * 1000 / std::max(current->getCurrentSpeed(), next->getCurrentSpeed())); keyframes.emplace_back(0, current->getCurrentPoint()); keyframes.emplace_back(transitionTime, next->getCurrentPoint()); - transition = std::make_unique(keyframes, smoothing * current->getCurrentDerivative(), smoothing * -next->getCurrentDerivative()); + // two-step to prevent erasing transition with may be pointed to by current. + auto newTransition = std::make_unique(keyframes, smoothing * current->getCurrentDerivative(), smoothing * -next->getCurrentDerivative()); current->reset(); + transition = move(newTransition); + currentAnimation = anim; - }else - { + } + else { nextAnimation = anim; } - }