Title: SoftwareSerial bug - local variable, dangling pointer · Issue #19 · avaldebe/PMserial · GitHub
Open Graph Title: SoftwareSerial bug - local variable, dangling pointer · Issue #19 · avaldebe/PMserial
X Title: SoftwareSerial bug - local variable, dangling pointer · Issue #19 · avaldebe/PMserial
Description: 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 th...
Open Graph Description: 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 v...
X Description: 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 v...
Opengraph URL: https://github.com/avaldebe/PMserial/issues/19
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"SoftwareSerial bug - local variable, dangling pointer","articleBody":"I look at [this code](https://github.com/avaldebe/PMserial/blob/98ccdffcfd50219e522fd0830552d082bae824e7/src/PMserial.h#L88) and see an error ([explain in cppreference](https://en.cppreference.com/w/cpp/language/reference#Dangling_references)).\r\nThe 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](https://en.wikipedia.org/wiki/Dangling_pointer). Copying its address should not prolong the lifetime.\r\n\r\nI tried looking for some special case that affects the lifetime of a variable and which will not be \"undefined behavior\" ([0](https://en.cppreference.com/w/cpp/language/lifetime), [1](https://en.cppreference.com/w/cpp/language/reference#Dangling_references), [2](https://en.cppreference.com/w/cpp/language/reference_initialization#Lifetime_of_a_temporary), [3](https://stackoverflow.com/questions/22114751/returning-static-pointer-to-local-variable-from-function), [4](https://qna.habr.com/q/246744), [5](https://habr.com/ru/company/jugru/blog/469465/), [6](https://habr.com/ru/post/186790/) ). But I couldn't find anything suitable for this code...\r\n\r\nIf my fears are in vain, please give me a link to a description of this case in the standard or explain how it works.\r\n\r\nProblem code:\r\n```\r\nclass SerialPM\r\n{\r\nprotected:\r\n Stream *uart; // hardware/software serial\r\n....\r\n\r\n SerialPM(PMS sensor, uint8_t rx, uint8_t tx) : pms(sensor)\r\n {\r\n SoftwareSerial serial(rx, tx);\r\n uart = \u0026serial;\r\n hwSerial = false;\r\n }\r\n```\r\n","author":{"url":"https://github.com/heX16","@type":"Person","name":"heX16"},"datePublished":"2021-02-28T23:36:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/19/PMserial/issues/19"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:451d1ab3-533e-3be0-f5d8-5ccbca04cffb |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A912:2EFA45:59A8BE:7FE69A:6A4BCC86 |
| html-safe-nonce | 4c173f5923a9b2a4f068a772b064e9d2750b00b10d3b398fd6fae40accad03a9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOTEyOjJFRkE0NTo1OUE4QkU6N0ZFNjlBOjZBNEJDQzg2IiwidmlzaXRvcl9pZCI6IjM3NTQzNTk5ODc1NzQxMzE4NDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3902a1a8886448b120360625ae166a91c9a5dffc74c2a96c89ae31405ddcbe60 |
| hovercard-subject-tag | issue:818343704 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/avaldebe/PMserial/19/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ea82843067115e65f483b141799d64c49d5443fba25369e541523f2cf0695780/avaldebe/PMserial/issues/19 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ea82843067115e65f483b141799d64c49d5443fba25369e541523f2cf0695780/avaldebe/PMserial/issues/19 |
| og:image:alt | 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 v... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | heX16 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 14aa00ce5bdb34d0eefb5facbffd7de9e144c688d8a93ef8df902d5f94b51dd7 |
| turbo-cache-control | no-preview |
| go-import | github.com/avaldebe/PMserial git https://github.com/avaldebe/PMserial.git |
| octolytics-dimension-user_id | 8268218 |
| octolytics-dimension-user_login | avaldebe |
| octolytics-dimension-repository_id | 108874855 |
| octolytics-dimension-repository_nwo | avaldebe/PMserial |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 108874855 |
| octolytics-dimension-repository_network_root_nwo | avaldebe/PMserial |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 58b8f89190447502561829f30862aa0a99d53367 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width