monome / crow

Crow speaks and listens and remembers bits of text. A scriptable USB-CV-II machine
GNU General Public License v3.0
167 stars 34 forks source link

Fix buffer allocation when reading a userscript #366

Closed trentgill closed 3 years ago

trentgill commented 3 years ago

Fixes #365 (cc @dndrks )

Tracking down why adding an additional new-line to a script could cause it to trigger the 'out of memory' error. Turns out it had nothing to do with the line-endings, but rather was an issue caused by writing over the end of a malloc'd char array.

Essentially we were allocating an exact number of bytes to hold the userscript in memory, but were then filling it with whole-words (4 bytes) rounded-up to the nearest word. This caused 0 to 3 bytes to be written past the end of the buffer, trampling something in the heap.

I think in this specific script it was the precise length that caused "out of memory" error to occur, but the specific error that it caused was totally dependent on the size of the script (which would change where in memory the buffer would be allocated). Obviously, most of the time this didn't matter (because the heap is generally sparse after boot).

I have a sneaking suspicion this has also been causing the 'won't boot & need to 'erase_userscript' bug -- In that situation the system is booting, but the USB fails to enumerate, making it impossible to communicate with the device. I propose that the reason USB wouldn't enumerate is the Lua environment hangs because some state variable in memory is corrupted.