arduino / ArduinoCore-API

Hardware independent layer of the Arduino cores defining the official API
https://www.arduino.cc/reference/en/
GNU Lesser General Public License v2.1
216 stars 120 forks source link

Detecting errors in String operations #186

Open BojanJurca opened 1 year ago

BojanJurca commented 1 year ago

Hello,

If a String creation fails it is possible to detect this error in the code, like:

String s = "abc";
if (!s) ... // error, out of heap or something ...

However the error detection doesn't work with String expressions:

#define howLarge 5000
String *strArray = new String [howLarge]; 

void wasteAlmostAllMemory () {
  for (int i = 0; i < howLarge; i++) strArray [i] = " wasted memory ";
  while (true)
    for (int i = 0; i < howLarge; i++)
      if (!strArray [i].concat (" more wasted memory "))
        return;
} 

String returnLongString () {
  return "This is a long string, much longer than fits into free memory, although not right now.";
}

void setup () {
    Serial.begin (115200);

    String longString = returnLongString ();

    Serial.printf ("Wasting memory, please wait ... ");
    wasteAlmostAllMemory ();
    Serial.printf ("memory successfuly wasted\n");

    String resultString;

    // 1.
      resultString = longString;
      if (!resultString)
        Serial.printf ("Could not create a long String s.\n"); // error detected successfuly
      else { Serial.print ("'"); Serial.print (resultString); Serial.println ("'"); }

    // 2.
      resultString = "ABC " + longString + " DEF";
      if (!resultString)
        Serial.printf ("Could not calculate a String s.\n"); // error goes by undetected
      else { Serial.print ("'"); Serial.print (resultString); Serial.println ("'"); } // the output is: ' DEF' 

    // 3.
      resultString = returnLongString (); // failure to create the return String crashes the controller
}

void loop () {

}

My proposal is that each string expression that contains string in "error state" result in string also in "error state", so success could be tested only once even after several String operations. Something like this:

    String s = "abc";
    for (int i = 1; i < 1000; i++)
      s += s.substring (1, 1);
    if (!s) ... // error

Thank you.

BojanJurca commented 1 month ago

Hello, I just wanted to know the status of this issue or is it going to be addressed at all or whether I was not clear enough?

The problem I described occurs when the controller is running low on memory so it can't construct a String correctly. It would be nice if this situation could be checked and handled by the code rather than producing incorrect Strings.