In the overall computer-world with so many and high-complex software-parts that interact with each other I can agree.
In case of using SafeStrings on microcontrollers I am convinced that it is much less complicated.
Each SafeString has a defined maximum-length.
If you try to assign more characters than the SafeString can hold this assigning is just not done.
Of course if you start doing "dirty" things like manipulating the underlying array-of-char you can make the code crash. But as long as you use the standard assigning the characters are stored into the SafeString-variable in case they fit into the variable-size
or
the characters were simply not assigned
Here is the demo-code that shows this
// SafeStrings are based on array of chars
// SafeStrings offer almost the same comfort as Strings
// but avoid some dis-advantages of variable-type String
// the name SafeString is PROGRAM They are safe to use
// with the alternatives "String" must not always but CAN
// eat up memory over time which will make the code crash
// with zero-terminated array of chars (c_string)
// you have to take care of boundary checking yourself
// otherwise your code will start to behave weird and this kind of bug is very hard to find
// you can read more about this here https://www.forward.com.au/pfod/ArduinoProgramming/ArduinoStrings/index.html#safestring
// and here https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/
// very basic demo-code to show how to declare a SafeString-variable
// and how to assign text to them
// as a personal convention I use the suffix "_SS" to indicate
// THIS IS A SAFESTRING-VARIABLE
// but you can name it whatever you like
#include "SafeString.h"
createSafeString(myTitle_SS, 64); // reserve 64 bytes for the SafeString-variable
createSafeString(myString_SS, 64); // reserve 64 bytes for the SafeString-variable
createSafeString(myExtraloongString_SS, 128); // reserve 128 bytes for the SafeString-variable
int myInteger = -1234;
float myFloat = -987.009;
float myFloat2 = -12345.6789;
float myFloat3 = -1234.567;
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
Serial.println();
myString_SS = F("fixed text directly assigned");
Serial.print( F(" #") ); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println(F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myTitle_SS = F("content of an integer:");
myString_SS = myInteger;
Serial.println(myTitle_SS);
Serial.print( F(" #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myTitle_SS = F("content of a float:");
myString_SS = myFloat;
Serial.println(myTitle_SS);
Serial.print( F(" #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myTitle_SS = F("you can append more text with the +=-operator ");
myString_SS = F("text ");
myString_SS += myInteger;
myString_SS += F(" ,");
myString_SS += myFloat;
Serial.println(myTitle_SS);
Serial.print( F("result: #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
myString_SS += F(" ,");
myString_SS += myFloat2;
myString_SS += F(" ,");
myString_SS += myFloat3;
Serial.println(myTitle_SS);
Serial.print( F("result: #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
Serial.println( F(" now let's try to assign wayyyyyy too many characters 012345678900123456789001234567890012345678900123456789001234567890") );
myString_SS = "";
myString_SS += F(" now let's try to assign wayyyyyy too many characters 012345678900123456789001234567890012345678900123456789001234567890");
Serial.print( F("result: #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
Serial.println( F(" now let's try to add more and more characters at the end of the SafeString") );
myString_SS = "";
for (byte i = 10; i < 64; i++) {
myString_SS += i;
Serial.print( F("result: #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
}
Serial.println( F(" assigning a number of chars the SafeString can really hold") );
myExtraloongString_SS = F("less than 64 characters");
myString_SS = myExtraloongString_SS;
Serial.print( F("result: #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
Serial.println( F(" assigning too many chars than the SafeString can really hold") );
Serial.println( F("more than 64 characters 1234567890123456789012345678901234567890123456789012345678901234567890") );
// 10 20 30 40 50 60 70
// 1234567890123456789012345678901234567890123456789012345678901234567890
myExtraloongString_SS = F("more than 64 characters 1234567890123456789012345678901234567890123456789012345678901234567890");
myString_SS = myExtraloongString_SS;
Serial.print( F("result: #" )); // leading double-cross "#" to show where the string starts
Serial.print(myString_SS);
Serial.println( F("#") ); // trailing double-cross "#" to show where the string REALLY ends
}
void loop() {
}
an this is the serial output
09:51:35.999 -> Setup-Start
09:51:35.999 ->
09:51:35.999 -> #fixed text directly assigned#
09:51:35.999 -> content of an integer:
09:51:35.999 -> #-1234#
09:51:35.999 -> content of a float:
09:51:36.047 -> #-987.01#
09:51:36.047 -> you can append more text with the +=-operator
09:51:36.047 -> result: #text -1234 ,-987.01#
09:51:36.047 -> you can append more text with the +=-operator
09:51:36.047 -> result: #text -1234 ,-987.01 ,-12345.68 ,-1234.57#
09:51:36.047 -> now let's try to assign wayyyyyy too many characters 012345678900123456789001234567890012345678900123456789001234567890
09:51:36.047 -> result: ##
09:51:36.047 -> now let's try to add more and more characters at the end of the SafeString
09:51:36.047 -> result: #10#
09:51:36.047 -> result: #1011#
09:51:36.047 -> result: #101112#
09:51:36.047 -> result: #10111213#
09:51:36.047 -> result: #1011121314#
09:51:36.047 -> result: #101112131415#
09:51:36.047 -> result: #10111213141516#
09:51:36.047 -> result: #1011121314151617#
09:51:36.092 -> result: #101112131415161718#
09:51:36.092 -> result: #10111213141516171819#
09:51:36.092 -> result: #1011121314151617181920#
09:51:36.092 -> result: #101112131415161718192021#
09:51:36.092 -> result: #10111213141516171819202122#
09:51:36.092 -> result: #1011121314151617181920212223#
09:51:36.092 -> result: #101112131415161718192021222324#
09:51:36.092 -> result: #10111213141516171819202122232425#
09:51:36.092 -> result: #1011121314151617181920212223242526#
09:51:36.092 -> result: #101112131415161718192021222324252627#
09:51:36.092 -> result: #10111213141516171819202122232425262728#
09:51:36.092 -> result: #1011121314151617181920212223242526272829#
09:51:36.092 -> result: #101112131415161718192021222324252627282930#
09:51:36.139 -> result: #10111213141516171819202122232425262728293031#
09:51:36.139 -> result: #1011121314151617181920212223242526272829303132#
09:51:36.139 -> result: #101112131415161718192021222324252627282930313233#
09:51:36.139 -> result: #10111213141516171819202122232425262728293031323334#
09:51:36.139 -> result: #1011121314151617181920212223242526272829303132333435#
09:51:36.139 -> result: #101112131415161718192021222324252627282930313233343536#
09:51:36.139 -> result: #10111213141516171819202122232425262728293031323334353637#
09:51:36.139 -> result: #1011121314151617181920212223242526272829303132333435363738#
09:51:36.139 -> result: #101112131415161718192021222324252627282930313233343536373839#
09:51:36.189 -> result: #10111213141516171819202122232425262728293031323334353637383940#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.189 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.234 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.281 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.281 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.281 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.281 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.281 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.281 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.327 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.327 -> result: #1011121314151617181920212223242526272829303132333435363738394041#
09:51:36.327 -> assigning a number of chars the SafeString can really hold
09:51:36.327 -> result: #less than 64 characters#
09:51:36.327 -> assigning too many chars than the SafeString can really hold
09:51:36.327 -> more than 64 characters 1234567890123456789012345678901234567890123456789012345678901234567890
09:51:36.327 -> result: ##
prove me wrong by presenting a demo-code that
- uses only the standard assigning to SafeStrings
- makes the code crash
- caused through the SafeString ITSELF
I am pretty sure if you handover a too long SafeString or a an empty SafeString to some array-of-char-expecting function-call that this can make the code crash.
But then the cause it is the use of the array-of-chars in the function call with not enough error-checking inside this other function