arduino / arduino/ArduinoCore-API
Detecting errors in String operations
- Vorherrschende Sprache
- C++
- Sterne
- 306
- Forks
- 150
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
Hello,
If a String creation fails it is possible to detect this error in the code, like:
```C++
String s = "abc";
if (!s) ... // error, out of heap or something ...
```
However the error detection doesn't work with String expressions:
```C++
#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:
```C++
String s = "abc";
for (int i = 1; i < 1000; i++)
s += s.substring (1, 1);
if (!s) ... // error
```
Thank you.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Das Issue enthält C++-Beispiele in setup(), wasteAlmostAllMemory() und returnLongString(), nennt aber keine Implementierungsdateien oder Tests. Beginne damit, die Arduino-String-Implementierung und die Pfade zu finden, die für die Verkettung, Zuweisung und zurückgegebenen Strings verwendet werden. Erledigt ist die Aufgabe, wenn die bereitgestellten Fehlerprüfungen Fehler zuverlässig erkennen, ohne Teilausgabe oder einen Absturz des Controllers, und eine Regressionstestabdeckung vorhanden ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- cpp
- Bereich
- api, embedded-iot
- Issue-Typ
- Feature
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100