Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

Detecting errors in String operations

Open
#186 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
api, embedded-iot

Research direction

The issue provides C++ examples in setup(), wasteAlmostAllMemory(), and returnLongString(), but names no implementation files or tests. Start by locating the Arduino String implementation and the paths used for concatenation, assignment, and returned strings. Done means the supplied failure checks consistently detect errors without partial output or a controller crash, with regression coverage.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C++
Stars
306
Forks
150
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from arduino/ArduinoCore-API

All issues in arduino/ArduinoCore-API

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.