Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String::replace buggy in certain cases #240

Closed
rspierce opened this issue Nov 30, 2024 · 1 comment
Closed

String::replace buggy in certain cases #240

rspierce opened this issue Nov 30, 2024 · 1 comment
Assignees

Comments

@rspierce
Copy link

rspierce commented Nov 30, 2024

Description

replacing multiple occurrences of a string with a smaller string only works for an odd number of occurrences

Environment

Boards platform name

"Raspberry Pi Pico/RP2040" boards platform from Earle F. Philhower, III

Boards platform version

Original report

Not specified

Last verified with

4.0.3

Code demonstrating problem

void setup() {
  Serial.begin(9600);
  while (!Serial) {}
  // this code should remove all occurrences of "narf" from the string
  String s = "narf";
  Serial.println();
  Serial.print("input: (");
  Serial.print(s);
  Serial.println(")");
  s.replace("narf", "");
  Serial.print("result: (");
  Serial.print(s);
  Serial.println(")");

  s = "narf narf";
  Serial.println();
  Serial.print("input: (");
  Serial.print(s);
  Serial.println(")");
  s.replace("narf", "");
  Serial.print("result: (");
  Serial.print(s);
  Serial.println(")");
}

void loop() {}

Serial monitor output:

input: (narf)
result: ()

input: (narf narf)
result: (narf narf)

Additional information

In this code:

https://github.com/earlephilhower/ArduinoCore-API/blob/ece6e68f29c6f406a4434659bcbcfe558baaa3a9/api/String.cpp#L667-L671

		while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
			readFrom = foundAt + find.len;
			diff = 0 - diff;
			size -= diff;
		}

line 669 (diff = 0 - diff;) should be above the while loop.

@rspierce rspierce added the bug label Nov 30, 2024
@per1234 per1234 self-assigned this Nov 30, 2024
@per1234 per1234 changed the title string.cpp remove() buggy in certain cases String::replace buggy in certain cases Nov 30, 2024
@per1234
Copy link
Collaborator

per1234 commented Nov 30, 2024

Closing as duplicate of #199, which has already been fixed by #200.

@rspierce the reason you are still affected by #199 is because the bug fix was made after the version of arduino/ArduinoCore-API that is in use by the "Raspberry Pi Pico/RP2040" boards platform. You can submit a request for the platform to update its version of arduino/ArduinoCore-API by submitting an issue to the tracker in that repository:

https://github.com/earlephilhower/arduino-pico/issues

@per1234 per1234 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants