Yeah, using malloc itself is iffy. If you use it, it'll pull in malloc and everything it needs (adding a few kB flash usage) and will take the data it needs out of stack. Allocate too much and you get stack overflow, which might be the instability.
That's the main difference between PC and embedded (apart from total stack size) so I'd say it's good thing to look into.
If you look at other Espruino functions we tend to use alloca to allocate data on the stack if it's just for use within as function call. In fact, as pkeystr/outputbuf are fixed size, I'd say you should just define them as an array? char outputbuf[80]. Much faster and safer
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Yeah, using
malloc
itself is iffy. If you use it, it'll pull inmalloc
and everything it needs (adding a few kB flash usage) and will take the data it needs out of stack. Allocate too much and you get stack overflow, which might be the instability.For
mbedtls
we actually usejsvMalloc
andjsvFree
which allocate memory like malloc/free, but out of JsVars instead: https://github.com/espruino/Espruino/blob/master/libs/crypto/mbedtls/config.h#L115-L116That's the main difference between PC and embedded (apart from total stack size) so I'd say it's good thing to look into.
If you look at other Espruino functions we tend to use
alloca
to allocate data on the stack if it's just for use within as function call. In fact, aspkeystr
/outputbuf
are fixed size, I'd say you should just define them as an array?char outputbuf[80]
. Much faster and safer