arduino / arduino/ArduinoCore-API
Detecting errors in String operations
- Ngôn ngữ chính
- C++
- Star
- 306
- Fork
- 150
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Issue cung cấp các ví dụ C++ trong setup(), wasteAlmostAllMemory() và returnLongString(), nhưng không nêu tên các tệp triển khai hoặc các bài kiểm thử. Hãy bắt đầu bằng việc xác định phần triển khai Arduino String và các đường dẫn được sử dụng cho phép nối, phép gán và các chuỗi được trả về. Hoàn thành có nghĩa là các kiểm tra lỗi được cung cấp luôn phát hiện lỗi một cách nhất quán mà không có đầu ra một phần hoặc controller bị crash, kèm theo độ bao phủ hồi quy.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- cpp
- Lĩnh vực
- api, embedded-iot
- Loại issue
- Tính năng
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 38/100