Skip to content

Commit 04dba1e

Browse files
Fix off-by-one in String::substring
When checking the `left` argument, it previously allowed having left == len. However, this means the substring starts one past the last character in the string and should return the empty string. In practice, this already worked correctly, because buffer[len] contains the trailing nul, so it would (re)assign the empty string to `out`. However, fixing this check makes it a bit more logical, and prevents a fairly unlikely out-of-buffer write (to address 0x0) when calling substring on an invalidated String: String bar = (char*)NULL; bar.substring(0, 0);
1 parent 86015f4 commit 04dba1e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

Diff for: hardware/arduino/avr/cores/arduino/WString.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ String String::substring(unsigned int left, unsigned int right) const
619619
left = temp;
620620
}
621621
String out;
622-
if (left > len) return out;
622+
if (left >= len) return out;
623623
if (right > len) right = len;
624624
char temp = buffer[right]; // save the replaced character
625625
buffer[right] = '\0';

0 commit comments

Comments
 (0)