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

SoftwareSerial bug - local variable, dangling pointer #19

Closed
heX16 opened this issue Feb 28, 2021 · 4 comments
Closed

SoftwareSerial bug - local variable, dangling pointer #19

heX16 opened this issue Feb 28, 2021 · 4 comments
Labels

Comments

@heX16
Copy link
Contributor

heX16 commented Feb 28, 2021

I look at this code and see an error (explain in cppreference).
The variable serial is local (in stack), its lifetime is limited to the area of the constructor. After exiting the constructor, the variable serial will be destroyed, and the variable uart will contain dangling pointer. Copying its address should not prolong the lifetime.

I tried looking for some special case that affects the lifetime of a variable and which will not be "undefined behavior" (0, 1, 2, 3, 4, 5, 6 ). But I couldn't find anything suitable for this code...

If my fears are in vain, please give me a link to a description of this case in the standard or explain how it works.

Problem code:

class SerialPM
{
protected:
  Stream *uart;  // hardware/software serial
....

  SerialPM(PMS sensor, uint8_t rx, uint8_t tx) : pms(sensor)
  {
    SoftwareSerial serial(rx, tx);
    uart = &serial;
    hwSerial = false;
  }
@avaldebe avaldebe added the bug label Mar 1, 2021
@avaldebe
Copy link
Owner

avaldebe commented Mar 1, 2021

I think you're, right. If I remember correctly, I tested this feature with actual HW when I implemented this feature, but I probably hit an undefined behavior of the uC tool-chain.

Will you be interested on submitting a patch/PR?

@heX16
Copy link
Contributor Author

heX16 commented Mar 2, 2021

Will you be interested on submitting a patch/PR?

Yes! I have an idea to declare "Serial" outside and pass it to pointer initialization (it will be completely analogous to "HardwareSerial"). And make the incoming pointer type "Stream". This will allow you to use any "Serial" and even their non-standard implementations.
I'll add code and correct examples of working with "SoftwareSerial". Work with "HardwareSerial" will not change.

@avaldebe
Copy link
Owner

avaldebe commented Mar 2, 2021

Will you be interested on submitting a patch/PR?

Yes! I have an idea to declare "Serial" outside and pass it to pointer initialization (it will be completely analogous to "HardwareSerial"). And make the incoming pointer type "Stream". This will allow you to use any "Serial" and even their non-standard implementations.
I'll add code and correct examples of working with "SoftwareSerial". Work with "HardwareSerial" will not change.

Awesome!

@avaldebe
Copy link
Owner

fixed on #20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants