espruino / espruino/BangleApps

[Boot] Variables are leaked outside scope

Open
#4,341 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
571
Forks
1.3k
Avg merge
17h 38m
Merged PRs (30d)
17

Description

### Affected hardware version

Bangle 2

### Your firmware version

2v29

### The bug

Noticed this, but I've been using BlueWatch and Calories for a bit, and when I looked at `calories.json`, it had also saved the blueWatch data in that same file, causing a bunch of errors, like `Uncaught Error: Unhandled promise rejection: Error: Can't read property 'unshift' of undefined
at onNewDay (.boot0:72:33)
savedData.prevData.unshift({activeCals:calData.activeCalorie...
^
at .boot0:74:78
...Data.dayLastUpdated){onNewDay();}let cd=calModule.calcCalories(...`

When I looked in boot.js a few things caught my eye:
- Both apps used the variable `savedData` to store the data that ended up in `calories.json`
- There's no block statements `{....]` enclosing the boot.js for each app
- `var` keywords are used copiously in both.

It's my understanding that the keyword `var` leaks the variable in block scopes, while `let` and `const` doesn't, which is probably causing the error. Since the vars are declared, my theory is that it leaks out and ends up in the other json saved due to the same variable name used.

I'll change the vars to `let` and see if it fixes it, but is it possible to do the same when making boot.js, and replacing each `var` with `let` to avoid this issue, as well as separating each boot.js file by a block in .boot0:
```
{
// code for xxx.boot.js
}
```
I'll also put in a PR to fix each of these apps.

Edit:
This started happening when I had added a third .boot.js script to test something new that also had the same `savedData` pattern
( really predictable programming lol) .
### Installed apps

messageicons (0.17), clock_info (0.27), messages (0.70), mylocation (0.11), smpltmr (0.13), widbtstates (0.03), locale (0.20), calculator (0.08), stacker (0.03), ios (0.25), clkinfosunrise (0.06), kineticscroll (0.05), sleeplog (0.26), myprofile (0.03), kbmulti (0.09), setting (0.84), widlock (0.09), widmsggrid (0.07), alarm (0.55), workouts (0.01), calories (0.02), messagegui (0.93), health (0.44), widapplebatt (0.01), phoneremind (0.01), clockbg (0.11), ultrainfo (0.03), clkinfomsg (0.01), boot (0.69), notify (0.15), sched (0.43), bluewatch (0.04), gridlaunch (0.02), imc (0.02), weather (0.34), dailycolorclk (0.03)
--

Also ^ the bold apps installed is caused by an extra `--` after the apps, and we should probably fix that in issue template

### Author

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by inspecting how boot.js files are combined into .boot0, then reproduce the collision with apps that use the same savedData variable. Check whether each app has its own scope and whether the generated code prevents data from leaking between apps; also review the issue template for the extra -- after installed apps.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
build-system, embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.