Here we go with another post on the hated SOS mode.
I would like to understand the reason for the photon to go in such mode.
Once entered it, it’s virtually impossible for me to get the photon not to start flashing red (3 long 3 short) by flashing a version of the code 100% functional.
The photon enters SOS mode only once completed the setup() and enetered loop()
Why is this? and what causes this mode?
SOS mode is entered when something in your program causes it to crash. This can be an out-of-memory or programming error or many other things depending on which code you are seeing (the number of flashes after the SOS part). If your Photon was a PC, SOS would be a blue screen type error or similar crash.
Normally you can get out of SOS mode by putting the device in safe mode and then flashing known-good code.
The way how dynamic parameter lists (e.g. for sprintf()) are handled this would be very hard for the compiler to warn you about, especially since there is no special treatment for literal format strings (as in your use case) vs. a run-time created format string.
To warn you if d0 will fit the bill for the firmat string would require the compiler to interpret the format string (which it might not even be able to at compile time).
And for the reason why it crashes: sprintf() will try to feed all the bytes into %s till it finds a zero terminator, but obviously at that address you passed via d0 there was none close enough not to breach the limits of buff causing a hard fault or a stack corruption.