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

Concat operator + with random() value leads to crash #142

Closed
sebromero opened this issue Mar 11, 2021 · 2 comments
Closed

Concat operator + with random() value leads to crash #142

sebromero opened this issue Mar 11, 2021 · 2 comments

Comments

@sebromero
Copy link

I found that when I concatenate a string directly (without wrapping it in a String object) with a number obtained from random() I get strange results.
When random() is used with a parameter, the resulting string contains garbage obtained from memory where it probably shouldn't read from.
When using random() without parameter the program crashes. On Portenta I get the red S.O.S flash, on the MKR1000 for example it just stops responding. I started to investigate but haven't found the culprit yet. It crashes somewhere in concat() when reading the string length using strlen().

void setup() {  
  Serial.begin(9600);
  while(!Serial);  

  //String newMessage = "Hello World " + random(1024); // Prints garbage:�����������3�������m���c���⸮�������Envie M7
  String newMessage = "Hello World " + random(); // Crashes
  //String newMessage = "Hello World " + String(random()); // Works
  //String newMessage = "Hello World " + String(random(1024)); // Works
  
  Serial.println(newMessage);
}
  
void loop() {}
@matthijskooijman
Copy link
Collaborator

Seems you reported this twice, closing this one as duplicate of #143.

@sebromero
Copy link
Author

Oh wow, one of the rare cases of messing up the state machine :-) Connection stalled while the browser was sending the request so I switched the WiFi which caused double posting. I'm glad it didn't happen during an e-banking transfer 😅

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