arduino / arduino/Arduino

problem of copying a String with nul byte in the middle

Open
#4,796 3 comments 0 reactions 0 assignees View on GitHub
Component: Core Type: Bug
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

After uploading my sketch (using Arduino 1.6.8 and Arduino Uno),
```arduino
void loop()
{

String str1 = "ACDEFG";
String str2 = "HIP";

String str3 = str1 + '\0' + str2;

Serial.println(str1);
Serial.println(str1.length());
Serial.println(str2);
Serial.println(str2.length());
Serial.println(str3);
Serial.println(str3.length());
Serial.println();
delay(10000);
}
```

I’m expecting to get as a result something like this:

ACDEFG
6
HIP
3
ACDEFG0HIP
10

But the result is:

ACDEFG
6
HIP
3
ACDEFG `ah
10

I.e. if I understand the problem correctly the String object keeps the right length of str3, but while copying the ‘\0’ it decides the string is over, but it’s not. So in str3 I get right symbols before ‘\0’ and some garbage after.

Contributor guide

Open the contributing guide

Research direction

Start with the Arduino sketch's loop() example and reproduce it on an Arduino Uno using Arduino 1.6.8. Trace String concatenation and copying when str1, '\0', and str2 form str3. Done means the embedded nul byte is handled consistently, str3 retains the reported length, and its output matches the expected result without garbage.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.