Skip to content

Commit aea3be7

Browse files
committed
Fix potential overflow of locks - favour small memory leak over accidental free if this ever happens (fix #2616)
1 parent cdab124 commit aea3be7

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

ChangeLog

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Bangle.js2: Added new Renaissance fonts and g.findFont function for selecting the best font
2323
Bangle.js2: Fix issue when an onchange callback from E.showMenu submenu changes the menu immediately
2424
Bangle.js: Added 'bpmMin/bpmMax' and 'activity' to 'health' event and 'Bangle.getHealthStatus'
25+
Fix potential overflow of locks - favour small memory leak over accidental free if this ever happens (fix #2616)
2526

2627
2v25 : ESP32C3: Get analogRead working correctly
2728
Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535)

src/jsvar.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,8 @@ JsVarRef jsvGetRef(JsVar *var) {
812812
JsVar *jsvLock(JsVarRef ref) {
813813
JsVar *var = jsvGetAddressOf(ref);
814814
//var->locks++;
815-
assert(jsvGetLocks(var) < JSV_LOCK_MAX);
816-
var->flags += JSV_LOCK_ONE;
815+
if ((var->flags & JSV_LOCK_MASK)!=JSV_LOCK_MASK) // if we hit the max amount of locks, don't exceed it (see https://github.com/espruino/Espruino/issues/2616)
816+
var->flags += JSV_LOCK_ONE;
817817
#ifdef DEBUG
818818
if (jsvGetLocks(var)==0) {
819819
jsError("Too many locks to Variable!");
@@ -832,8 +832,8 @@ JsVar *jsvLockSafe(JsVarRef ref) {
832832
/// Lock this pointer and return a pointer - UNSAFE for null pointer
833833
JsVar *jsvLockAgain(JsVar *var) {
834834
assert(var);
835-
assert(jsvGetLocks(var) < JSV_LOCK_MAX);
836-
var->flags += JSV_LOCK_ONE;
835+
if ((var->flags & JSV_LOCK_MASK)!=JSV_LOCK_MASK) // if we hit the max amount of locks, don't exceed it (see https://github.com/espruino/Espruino/issues/2616)
836+
var->flags += JSV_LOCK_ONE;
837837
return var;
838838
}
839839

@@ -865,6 +865,7 @@ static ALWAYS_INLINE void jsvUnLockInline(JsVar *var) {
865865
/* Reduce lock count. Since ->flags is volatile
866866
* it helps to explicitly save it to a var to avoid a
867867
* load-store-load */
868+
if ((var->flags & JSV_LOCK_MASK)==JSV_LOCK_MASK) return; // if we had the max number of locks, don't unlock as we probably didn't lock enough (see https://github.com/espruino/Espruino/issues/2616)
868869
JsVarFlags f = var->flags -= JSV_LOCK_ONE;
869870
// Now see if we can properly free the data
870871
// Note: we check locks first as they are already in a register

0 commit comments

Comments
 (0)