arduino / arduino/ArduinoCore-API

Detecting errors in String operations

未关闭
#186 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C++
星标
306
派生
150
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

这个仓库没有索引到贡献指南

调研方向

该 issue 提供了 setup()、wasteAlmostAllMemory() 和 returnLongString() 中的 C++ 示例,但没有指明实现文件或测试。首先定位 Arduino String 的实现,以及用于拼接、赋值和返回字符串的路径。完成的标准是:所提供的失败检查能够稳定地检测错误,不产生部分输出或 controller 崩溃,并具有回归覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
cpp
领域
api, embedded-iot
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。