Fix issue 20809 - return statement might access memory from destructed temporary#11215
Fix issue 20809 - return statement might access memory from destructed temporary#11215welkam wants to merge 7 commits intodlang:masterfrom
Conversation
|
|
Since no on said anything I decided to pass additional variable to toElemDtor(). With this patch if function returns ref then it will use a fix from #10577 aka current behavior. Otherwise it uses old behavior. |
The current approach looks better. I would've preferred a default parameter as
Any idea why? |
Its not. I view this pull request as a first aid to stop the hemorrhaging(bleeding).
There are 5 calls to appendDtors() in a visit(ReturnStatement s) function. The example in issue 20809 triggers call #5. The example in issue 20401 triggers call #4. And the example from timon.gehr triggers call #3. If you try to fix one issue then you break the others. If you would to asked my opinion then the whole compound statement needs to go. It caused bugs, is causing bugs and I believe it will cause more bugs. There is just too much complexity that is leaving semantic analysis. |
Fair enough.
Thanks for the detailed explanation. Can't comment on the details because it don't know much about the backend but your approach sounds reasonable. Had a short look at that function anyways and found some additional test cases that might help: |
|
(I do not know, if this belongs here) It look like this PR solve my issue #20809 in my project too. I have tested this PR with the latest dmd version (2.093) and all my unittest pass. Thanks for your work. |
|
Hi @welkam ! Sorry for being late to the party. Could you maybe rebase this? I will try to get a review on this from @WalterBright |
|
@welkam could you maybe add a commit that contains "Fix Issue 20809" in the commit message so that dlang-bot picks it up and links it to bugzilla? |
|
so we can see why it returns 0. |
|
With this PR, this code is generated instead: which is correct. What disturbs me, however, is if In order to fix this properly, the desired value should be copyable with a bit copy, and should not have a destructor, i.e. is POD (Plain Old Data). Otherwise, the compiler should emit an error. |
|
On further reflection, the rewrite should be in ReturnStatement's semantic routine: should be, if f() returns a ref: and the rest of the semantic code will take care of copy construction, destructors, etc. |
This is an attempt at fixing this issue. I realized that I bit more than I can chew on this one.
What this patch provides:
1.An idea where the bug is and what kind of change is needed to fix it. (I copy pasted function and commented out code so that the peace of code that I want to run gets to run. To my surprise it worked and it passes all the tests. ¯_(ツ)_/¯)
2.A test case that is combination of issue 20401, issue 20809 and ldc-developers/ldc#3426
The problems:
Solutions:
A. Some one uses this to kickstart a new pull request.
B. Some one helps with guidance because im not confident that I can make required changes and think of all possible cases to make sure it doesnt break something else.