Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Detecting errors in String operations

Aperta
#186 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
38/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
api, embedded-iot

Direzione di ricerca

La issue fornisce esempi C++ in setup(), wasteAlmostAllMemory() e returnLongString(), ma non indica file di implementazione né test. Inizia individuando l’implementazione di Arduino String e i percorsi utilizzati per la concatenazione, l’assegnazione e le stringhe restituite. Il lavoro è completato quando i controlli di errore forniti rilevano gli errori in modo coerente, senza output parziale né crash del controller, con copertura di regressione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C++
Stelle
306
Fork
150
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di arduino/ArduinoCore-API

Tutte le issue di arduino/ArduinoCore-API

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.