[Issue 22277] removing strongly pure function calls is an incorrect optimization

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Oct 28 16:30:15 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22277

--- Comment #12 from Ate Eskola <Ajieskola at gmail.com> ---
Currently, the compiler is allowed to elide such a `free` call if it can prove
that `free` has been called before with a similar value in the pointed object.
In that case, any potential crash or infinite loop would have happened in the
prior call.

This is directly not a problem. The compiler cannot judge whether two void
pointers have similar values in the object, since it does not know the type of
the object. The exception to that rule might be a double free, but that is
undefined behaviour anyway.

However, this one is still a problem:

------
pure immutable void* mallocSomething(int arg);

auto a = mallocSomething(8), b = mallocSomething(8);
free(a);
free(b);
------

Even without the knowledge of what the pointers point to, the compiler is free
to assume the objects are similar since they are returned by a strongly pure
function with similar arguments. Thus it may skip the latter `free` call.

I'm not sure how to handle this. I guess the short-term solution is to simply
forbid a pure `free` with an immutable pointer.

--


More information about the Digitalmars-d-bugs mailing list